0% found this document useful (0 votes)
14 views16 pages

Excersise 1

Uploaded by

gowri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Excersise 1

Uploaded by

gowri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Ex.No.

: 1a CAESAR CIPHER

Date :

AIM:

To implement a Caesar cipher substitution technique inJava.

ALGORITHM:

1. Assign the 26 letters in alphabet to the variable named ALPHABET.

2. Convert the plaintext letters into lowercase.

3. To encrypt a plaintext letter, the first set of plaintext letters and slides it to LEFT by the

number of positions of the secret shift.

4. The plaintext letter is then encrypted to the ciphertext letter on the sliding ruler

underneath.

5. On receiving the ciphertext, the receiver who also knows the secret shift, positions his

sliding ruler underneath the ciphertext alphabet and slides it to RIGHT by the agreed shift

number, 3 in this case.

6. Then replaces the ciphertext letter by the plaintext letter on the sliding ruler underneath.

PROGRAM:

import java.util.Scanner;

public class ceasercipher

public static final String ALPHABET="abcdefghijklmnopqrstuvwxyz";

public static String encrypt(String plainText,int shiftKey)

plainText=plainText.toLowerCase();

String cipherText="";

for (int i=0; i<plainText.length();i++)

int charPosition=ALPHABET.indexOf(plainText.charAt(i));
int keyVal=(shiftKey+charPosition)%26;

char replaceVal=ALPHABET.charAt(keyVal);

cipherText+=replaceVal;

return cipherText;

public static String decrypt(String cipherText,int shiftKey)

cipherText = cipherText.toLowerCase();

String plainText = "";

for(int i=0;i<cipherText.length();i++)

int charPosition= ALPHABET. indexOf(cipherText. charAt(i));

int keyVal=(charPosition-shiftKey)%26;

if (keyVal< 0)

keyVal=ALPHABET.length()+keyVal;

char replaceVal=ALPHABET.charAt(keyVal);

plainText+=replaceVal;

return plainText;

public static void main(String[] args)

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Plain text for Encryption: ");


String message=new String();

message=sc.next();

System.out.println("Enter key value");

int n=sc.nextInt();

System.out.println("Encrypted message:Cipher Text="+encrypt(message,n));

System.out.println("Decrypted message:Plain Text="+decrypt (encrypt(

message,n),n));

sc.close();

Output:

Enter the Plain text for Encryption: hello

Enter key value : 8

Encrypted message:Cipher Text=pmttw

Decrypted message:Plain Text=hello

RESULT:

Thus the Caesar cipher substitution technique was implemented and executed

successfully
Ex.No. : 1b PLAYFAIR CIPHER

Date :

AIM:

To implement a Playfair cipher substitution technique inJava.

ALGORITHM:

1. Read the keyword.

2. Then create the key table of 5x5 grid of alphabets.

3. Read the word to encrypt.

4. Ifthe input word should be even and then process it.

5. Then the plaintext message is split into pairs of two letters (digraphs).

6. Ifboth the letters are in the same column, take the letter below each one.

7. Ifboth letters are in the same row, take the letter to the right of each one.

8. If neither of the preceding two rules are true, form a rectangle with the two letters and

take the letters on the horizontal opposite corner of the rectangle.

PROGRAM:
import java.util.Scanner;

public class Playfair1

public static void main(String[] args)

Scanner in=new Scanner(System.in);

System.out.print("Enter keyword: ");

String key=in.nextLine();

System.out.print("Enter message to encrypt: ");

String msg=in.nextLine();
PFEncryption pfEncryption=new PFEncryption();

pfEncryption.makeArray(key);

msg=pfEncryption.manageMessage(msg);

pfEncryption.doPlayFair(msg, "Encrypt");

String en=pfEncryption.getEncrypted();

System.out.println("Encrypting. ..\n\nThe encrypted text is: " + en);

System.out.println("=============================");

pfEncryption.doPlayFair(en, "Decrypt");

System.out.print("\nDecrypting... \n\nThe encrypted text is: " +

pfEncryption.getDecrypted());

class PFEncryption

private char [][] alphabets= new char[5][5];

private char[] uniqueChar= new char[26];

private String ch="ABCDEFGHIKLMNOPQRSTUVWXYZ";

private String encrypted="";

private String decrypted="";

void makeArray(String keyword)

keyword=keyword.toUpperCase().replace("J","I");

boolean present, terminate=false;

int val=0;

int uniqueLen;

for (int i=0; i<keyword.length(); i++)

{
present=false;

uniqueLen=0;

if (keyword.charAt(i)!= ' ')

for (int k=0; k<uniqueChar.length; k++)

if (Character.toString(uniqueChar[k])==null)

break;

uniqueLen++;

for (int j=0; j<uniqueChar.length; j++)

if (key word.charAt(i)==uniqueChar[j])

present=true;

if (!present)

uniqueChar[val]=keyword.charAt(i);

val++;

ch=ch.replaceAll(Character.toString(keyword.charAt(i)), "");

}
for (int i=0; i<ch.length(); i++)

uniqueChar[val]=ch.charAt(i);

val++;

val=0;

for (int i=0; i<5; i++)

for (int j=0; j<5; j++)

alphabets[i][j]=uniqueChar[val];

val++;

System.out.print(alphabets[i][j] + "\t");

System.out.println();

String manageMessage(String msg)

int val=0;

int len=msg.length()-2;

String newTxt="";

String intermediate="";

while (len>=0)

{
intermediate=msg.substring(val, val+2);

if (intermediate.charAt(0)==intermediate.charAt(1))

newTxt=intermediate.charAt(0) + "x" + intermediate.charAt(1);

msg=msg.replaceFirst(intermediate, newTxt);

len++;

len-=2;

val+=2;

if (msg.length()%2!=0)

msg=msg+'x';

return msg.toUpperCase().replaceAll("J","I").replaceAll(" ","");

void doPlayFair(String msg, String tag)

int val=0;

while (val<msg.length())

searchAndEncryptOrDecrypt(msg.substring(val,val+2),tag);

val+=2;

}}

void searchAndEncryptOrDecrypt(String doubblyCh, String tag)

char ch1=doubblyCh.charAt(0);
char ch2=doubblyCh.charAt(1);

int row1=0, col1=0, row2=0, col2=0;

for (int i=0; i<5; i++)

for (int j=0; j<5; j++)

if (alphabets[i][j]==ch1)

row1=i;

col1=j;

else if (alphabets[i][j]==ch2)

row2=i;

col2=j;

}}}

if (tag=="Encrypt")

encrypt(row1, col1, row2, col2);

else if(tag=="Decrypt")

decrypt(row1, col1, row2, col2);

void encrypt(int row1, int col1, int row2, int col2)

if (row1==row2)

col1=col1+1;

col2=col2+1;
if (col1>4)

col1=0;

if (col2>4)

col2=0;

encrypted+=(Character.toString(alphabets[row1][col1])+

Character.toString(alphabets[row1][col2]));

else if(col1==col2)

row1=row1+1;

row2=row2+1;

if (row1>4)

row1=0;

if (row2>4)

row2=0;

encrypted+=(Character.toString(alphabets[row1][col1])+

Character.toString(alphabets[row2][col1]));

else

{ encrypted+=(Character.toString(alphabets[row1][col2])+

Character.toString(alphabets[row2][col1]));

}}

void decrypt(int row1, int col1, int row2, int col2)

if (row1==row2)

col1=col1-1;
col2=col2-1;

if (col1<0)

col1=4;

if (col2<0)

col2=4;

decrypted+=(Character.toString(alphabets[row1][col1])+

Character.toString(alphabets[row1][col2]));

else if(col1==col2)

row1=row1-1;

row2=row2-1;

if (row1<0)

row1=4;

if (row2<0)

row2=4;

decrypted+=(Character.toString(alphabets[row1][col1])+

Character.toString(alphabets[row2][col1]));

else

decrypted+=(Character.toString(alphabets[row1][col2])+

Character.toString(alphabets[row2][col1]));

}}

String getEncrypted( )

return encrypted;
}

String getDecrypted( )

return decrypted;

}}

Output:

Enter keyword: INFOSEC

Enter message to encrypt: cryptography

I N F O S

E C A B D

G H K L M

P Q R T U

V W X Y Z

Encrypting. ..

The encrypted text is: AQVTYBKPERLW

=============================

Decrypting...

The encrypted text is: CRYPTOGRAPHY

RESULT:

Thus the Playfair cipher substitution technique was implemented and executed

Successfully
Ex.No. : 1c HILL CIPHER

Date :

AIM:

To implement a Hill cipher substitution technique in Java.

ALGORITHM:

1. Obtain a plaintext message to encode in standard English with no spaces.

2. Split the plaintext into group of length three. To fill this, add X at the end.

3. Convert each group of letters with length three into plaintext vectors.

4. Replace each letter by the number corresponding to its position in the alphabet i.e.

A=1, B=2, C=3…Z=0.

5. Create the keyword in a 3*3 matrix.

6. Multiply the two matrices to obtain the cipher text of length three.

7. For decryption, convert each entry in the ciphertext vector into its plaintext vector by

multiplying the cipher text vector and inverse of a matrix.

8. Thus plain text is obtained fromcorresponding plaintext vector by corresponding

position in the alphabet.

PROGRAM:

import java.util.Scanner;

import javax.swing.JOptionPane;

public class hillcipher

//the 3x3 key matrix for 3 characters at once

public static int[][] keymat = new int[][]

{ 1, 2, 1 },

{ 2, 3, 2 },

{ 2, 2, 1 },
};

public static int[][] invkeymat = new int[][]

{ -1, 0, 1 },

{ 2, -1, 0 },

{ -2, 2, -1},

};

public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

public static void main(String[] args)

String text,outtext ="",outtext1="";

int ch, n;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Plain text for Encryption: ");

//String text=new String();

text=sc.next();

text = text.toUpperCase();

text = text.replaceAll("\\s",""); //removing spaces

n = text.length() % 3;

if(n!=0)

for(int i = 1; i<= (3-n);i++)

text+= 'X';

System.out.println("Padded Text:" + text);


char[] ptextchars = text.toCharArray();

for(int i=0;i< text.length(); i+=3)

outtext += encrypt(ptextchars[i],ptextchars[i+1],ptextchars[i+2]);

System.out.println("Encypted Message: " + outtext);

char[] ptextchars1 = outtext.toCharArray();

for(int i=0;i< outtext.length(); i+=3)

outtext1 += decrypt(ptextchars1[i],ptextchars1[i+1],ptextchars1[i+2]);

System.out.println("Decrypted Message: " + outtext1);

private static String encrypt(char a, char b, char c)

String ret = "";

int x,y, z;

int posa = (int)a - 65;

int posb = (int)b - 65;

int posc = (int)c - 65;

x = posa * keymat[0][0] + posb * keymat[1][0] + posc * keymat[2][0];

y = posa * keymat[0][1] + posb * keymat[1][1] + posc * keymat[2][1];

z = posa * keymat[0][2] + posb * keymat[1][2] + posc * keymat[2][2];

a = key.charAt(x%26);

b = key.charAt(y%26);

c = key.charAt(z%26);

ret = "" + a + b + c;
return ret;

private static String decrypt(char a, char b, char c)

String ret = "";

int x,y,z;

int posa = (int)a - 65;

int posb = (int)b - 65;

int posc = (int)c - 65;

x = posa * invkeymat[0][0]+ posb * invkeymat[1][0] + posc * invkeymat[2][0];

y= posa * invkeymat[0][1]+ posb * invkeymat[1][1] + posc * invkeymat[2][1];

z = posa * invkeymat[0][2]+ posb * invkeymat[1][2] + posc * invkeymat[2][2];

a = key.charAt((x%26<0)?(26+x%26):(x%26));

b = key.charAt((y%26<0)?(26+y%26):(y%26));

c = key.charAt((z%26<0)?(26+z%26):(z%26));

ret = "" + a + b + c;

return ret;

Output:

Enter the Plain text for Encryption: mothertheresa

Padded Text:MOTHERTHERESAXX

Encypted Message: AAHXIGPPLJEROLR

Decrypted Message: MOTHERTHERESAXX

RESULT:

Thus the Hill cipher substitution technique was implemented and executed

successfully.

You might also like