Top Banner
CRYPTOGRAPHY AND NETWORK SECURITY INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Department of Computer Engineering Laboratory Manual CRYPTOGRAPHY & NETWORK SECURITY Academic Year: Jan-2015 Semester: VI
26

CNS_LABMANUAL.docx

Feb 19, 2016

Download

Documents

Akira

cryptography practicals
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERINGDepartment of Computer Engineering

Laboratory Manual

CRYPTOGRAPHY & NETWORK

SECURITY

Academic Year: Jan-2015

Semester: VI

ROSHNI PATEL

Page 2: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL-LISTPRACTICAL-1To implement Caesar Cipher Encryption - Decryption.

PRACTICAL-2To implement Mono-alphabetic Cipher Encryption - Decryption.

PRACTICAL-3To implement Hill Cipher Encryption.

PRACTICAL-4To implement Poly-alphabetic Cipher (Vigener Cipher) Technique.

PRACTICAL-5To implement Play-Fair Cipher Technique.

PRACTICAL-6Write a program to implement Rail-Fence Encryption Technique

PRACTICAL-7 To implement S-DES algorithm for data encryption.

PRACTICAL-8 Write a program to implement RSA asymmetric (public key and private key)-Encryption

PRACTICAL-9Write a program to generate digital signature using Hash code.

PRACTICAL-10Case Study on Kerberos.

LABWORK BEYOND CURRICULAPRACTICAL-11Study of MD5 hash function and implement the hash code using MD5PRACTICAL-12

Study of SHA-1 hash function and implement the hash code using SHA-1

Page 3: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

Sample Practical

AIM: Implement Play Fair Cipher Encryption

Procedure: By analyzing the problem I found required two basic steps for implementing the data encryption using Play Fair cipher 1) Generate Key matrix 2) Encrypt the data using encryption rule and key matrix

1) Generating Key matrix To Generate the key matrix take any random key of any length and form a 5X5 matrix. Go on filling the rows of the matrix with the key characters ( if repeating character occurs then ignore it). Fill the remaining matrix with alphabets from A to Z (except those already occurred in the key). For example for the key “monarchy” we have the matrix as follow

2) Encrypt the data using encryption rule and key matrix To Encrypt the data take two characters at time from plain text file and encrypt it using one of the following rules.

Encryption rules 1) Repeating plain text letters that would fall in the same pair are separated with filler letter, such as x.( i.e. Balloon becomes Ba, lx, lo, on) 2) If both the characters are in the same raw then replace each with the character to its right, with the last character followed by the first, in the matrix. 3) If both the characters are in the same column then replace each with the character below it, with the bottom character followed by the top, in the matrix. 4) Otherwise each plain text letter is replaced by the letter that lies in its own row and the column occupied by the other plain text letter

Example: Using key as “monarchy” we have - Encryption of AR as RM - Encryption of MU as CM - Encryption of BP as IM

3.2 Designing the Solution: Solution implementation is given below:- For this solution we have to implement the following functions given below. 1) Input function for key & Plain Text. 2) Matrix generation. 3) Encryption function for generating Cipher Text. 4) Print function for printing Cipher Text Output.

Page 4: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

Source Code //**************************************// Play Fair Cipher Encryptionimport java.awt.*;import java.awt.event.*;import java.lang.*;import java.applet.Applet; /*//**************************************

OUTPUT:-

Conclusions By this PRACTICAL; we can conclude that basic working of play fair cipher encryption methodology is working properly.

Page 5: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 1

AIM: To implement Caesar Cipher Encryption - Decryption.

THEORY: In cryptography, a Caesar cipher, also known as a Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals. If anyone wishes to decipher these, and get at their meaning, he must substitute the fourth letter of the alphabet, namely D, for A, and so with the others.

The action of a Caesar cipher is to replace each plaintext letter with one fixed number of places down the alphabet. This example is with a shift of three, so that a B in the plaintext becomes E in the ciphertext.

Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC Ciphertext: WKH TXLFN EURZQ IRA MXPSV RYHU WKH ODCB GRJ Plaintext: the quick brown fox jumps over the lazy dog

The encryption can also be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A = 0, B = 1,..., Z = 25. [1] Encryption of a letter x by a shift n can be described mathematically as,[2]

Decryption is performed similarly,

Page 6: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 2

AIM: To implement Monoalphabetic Cipher Encryption.

THEORY: The ciphers in this substitution section replace each letter with another letter according to the cipher alphabet. Ciphers in which the cipher alphabet remains unchanged throughout the message are called Monoalphabetic Substitution Ciphers.

If we permit the cipher alphabet to be any rearrangement of the plain alphabet, then we can generate an enormous number of distinct modes of encryption. There are over 400,000,000,000,000,000,000,000,000 such rearrangements, which gives rise to an equivalent number of distinct cipher alphabets. Each cipher alphabet is known as a key.

Start by creating a key that maps each letter of the alphabet to a (possibly the same) letter of the alphabet. A sample key might be:

Plaintext letter a b c d e f g h i j k l m n o p q r s t u v w x y zCiphertext letter y n l k x b s h m i w d p j r o q v f e a u g t z c

To encrypt the message "meet me at nine", start by taking the first letter of the message, 'm', and look up the corresponding ciphertext letter 'p'. Repeat by looking up the next plaintext letter 'e', and noting it becomes 'x'. Continue this process for the rest of the message. Typically spaces, numbers, and punctuation are left alone. In this case "meet me at nine." would become "pxxe px ye jmjx."

Decryption is similar. Start with the first ciphertext letter 'p', and look at the table to find the corresponding plaintext letter 'm'. Continue with the next letter 'x', and find it maps to 'e.

Page 7: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

Page 8: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 3

AIM: To implement Hill Cipher Encryption.

THEORY: In classical cryptography, the Hill cipher is a polygraphic substitution cipher based on linear algebra. Invented by Lester S. Hill in 1929.

A block of n letters is then considered as a vector of n dimensions, and multiplied by an n × n matrix, modulo 26. (If one uses a larger number than 26 for the modular base, then a different number scheme can be used to encode the letters, and spaces or punctuation can also be used.) The whole matrix is considered the cipher key, and should be random provided that the matrix is invertible in (to ensure decryption is possible). A Hill cipher is another way of working out the equation of a matrix.

Consider the message 'ACT', and the key below (or GYBNQKURP in letters):

Since 'A' is 0, 'C' is 2 and 'T' is 19, the message is the vector:

Thus the enciphered vector is given by:

which corresponds to a ciphertext of 'POH'. Now, suppose that our message is instead 'CAT', or:

This time, the enciphered vector is given by:

Page 9: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

which corresponds to a ciphertext of 'FIN'. Every letter has changed. The Hill cipher has achieved Shannon's diffusion, and an n-dimensional Hill cipher can diffuse fully across n symbols at once.

Decryption

In order to decrypt, we turn the ciphertext back into a vector, then simply multiply by the inverse matrix of the key matrix (IFKVIVVMI in letters). (There are standard methods to calculate the inverse matrix; see matrix inversion for details.) We find that in the inverse matrix of the one in the previous example is:

Taking the previous example ciphertext of 'POH', we get:

which gets us back to 'ACT', just as we hoped.

For our example key matrix:

So, modulo 26, the determinant is 25. Since this has no common factors with 26, this matrix can be used for the Hill cipher.

The risk of the determinant having common factors with the modulus can be eliminated by making the modulus prime. Consequently a useful variant of the Hill cipher adds 3 extra symbols (such as a space, a period and a question mark) to increase the modulus to 29.

Page 10: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

Page 11: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 4AIM: To implement Polyalphabetic Cipher Encryption.

THEORY:

A polyalphabetic cipher is any cipher based on substitution, using multiple substitution alphabets. The Vigenère cipher is probably the best-known example of a polyalphabetic cipher, though it is a simplified special case.

In a polyalphabetic cipher, multiple cipher alphabets are used. To facilitate encryption, all the alphabets are usually written out in a large table, traditionally called a tableau. The tableau is usually 26×26, so that 26 full ciphertext alphabets are available. The method of filling the tableau, and of choosing which alphabet to use next, defines the particular polyalphabetic cipher.

The Polyalphabetic Cipher (often referred to as a Vigenère) uses a KEY which determines which letter in a cipher alphabet is used to create the ciphertext message. Normally, the cipher alphabet is 26 letters arranged alphabetically (a-z). This program can use a cipher alphabet of up to 39 characters and they can be arranged in any sequence desired. There is no limit to the number of characters in the KEY. It also allows a ‘codemaker’ to create a polyalphabetic cipher system of his own design and has a crib dragging function for cracking polyalphabetic ciphertext. The PolyAlphabetic Crypto Program is extremely flexible and a very useful crypto tool.

Page 12: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

Below is the Vigenere table.

Page 13: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 5

AIM: To implement Play-Fair Cipher Encryption.

THEORY:

The Playfair cipher or Playfair square is a manual symmetric encryption technique and was the first literal digraph substitution cipher. The scheme was invented in 1854 by Charles Wheatstone, but bears the name of Lord Playfair who promoted the use of the cipher.

The Playfair cipher uses a 5 by 5 table containing a key word or phrase. Memorization of the keyword and 4 simple rules was all that was required to create the 5 by 5 table and use the cipher.

To generate the key table, one would first fill in the spaces in the table with the letters of the keyword (dropping any duplicate letters), then fill the remaining spaces with the rest of the letters of the alphabet in order (usually omitting "Q" to reduce the alphabet to fit, other versions put both "I" and "J" in the same space). The key can be written in the top rows of the table, from left to right, or in some other pattern, such as a spiral beginning in the upper-left-hand corner and ending in the center. The keyword together with the conventions for filling in the 5 by 5 table constitute the cipher key.

Plaintext is encrypted two letters at a time, according to the following rules:

1. If both letters are the same (or only one letter is left), add an "X" after the first letter. Encrypt the new pair and continue. Some variants of Playfair use "Q" instead of "X", but any uncommon monograph will do.

2. If the letters appear on the same row of your table, replace them with the letters to their immediate right respectively (wrapping around to the left side of the row if a letter in the original pair was on the right side of the row).

3. If the letters appear on the same column of your table, replace them with the letters immediately below respectively (wrapping around to the top side of the column if a letter in the original pair was on the bottom side of the column).

4. If the letters are not on the same row or column, replace them with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. The order is important – the first letter of the encrypted pair is the one that lies on the same row as the first letter of the plaintext pair.

Example

Using "playfair example" as the key, (assuming I and J are interchangeable) the table becomes:

Page 14: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

Encrypting the message "Hide the gold in the tree stump":

HI DE TH EG OL DI NT HE TR EX ES TU MP

1. The pair HI forms a rectangle, replace it with BM

The pair DE is in a column, replace it with OD

The pair TH forms a rectangle, replace it with ZB

The pair EG forms a rectangle,replace it with XD

The pair OL forms a rectangle, replace it with NA

The pair DI forms a rectangle, replace it with BE

Page 15: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

The pair NT forms a rectangle, replace it with U The pair HE forms a rectangle,replace it withDM The pair TR forms a rectangle, replace it with UI

The pair EX (X inserted to split EE) is in a row, replace it with XM

The pair ES forms a rectangle, replace it with MO

The pair TU is in a row, replace it with UV

The pair MP forms a rectangle, replace it with IF

BM OD ZB XD NA BE KU DM UI XM MO UV IF

Thus the message "Hide the gold in the tree stump" becomes

"BMODZBXDNABEKUDMUIXMMOUVIF".

Page 16: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 6

AIM: Write a program to implement Rail-Fence Encryption Technique.

THEORY:

In cryptography, a transposition cipher is a method of encryption by which the positions held by units of plaintext (which are commonly characters or groups of characters) are shifted according to a regular system, so that the ciphertext constitutes a permutation of the plaintext. That is, the order of the units is changed. Mathematically a bijective function is used on the characters' positions to encrypt and an inverse function to decrypt.

Page 17: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 7AIM: To implement S-DES algorithm for data encryption.

Page 18: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

Page 19: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 8AIM: To implement RSA asymmetric (public key and private key)-Encryption

Page 20: CNS_LABMANUAL.docx

CRYPTOGRAPHY AND NETWORK SECURITY

PRACTICAL NO : 9

AIM: Generate Digital Signature