Excersise 1
Excersise 1
: 1a CAESAR CIPHER
Date :
AIM:
ALGORITHM:
3. To encrypt a plaintext letter, the first set of plaintext letters and slides it to LEFT by the
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
6. Then replaces the ciphertext letter by the plaintext letter on the sliding ruler underneath.
PROGRAM:
import java.util.Scanner;
plainText=plainText.toLowerCase();
String cipherText="";
int charPosition=ALPHABET.indexOf(plainText.charAt(i));
int keyVal=(shiftKey+charPosition)%26;
char replaceVal=ALPHABET.charAt(keyVal);
cipherText+=replaceVal;
return cipherText;
cipherText = cipherText.toLowerCase();
for(int i=0;i<cipherText.length();i++)
int keyVal=(charPosition-shiftKey)%26;
if (keyVal< 0)
keyVal=ALPHABET.length()+keyVal;
char replaceVal=ALPHABET.charAt(keyVal);
plainText+=replaceVal;
return plainText;
message=sc.next();
int n=sc.nextInt();
message,n),n));
sc.close();
Output:
RESULT:
Thus the Caesar cipher substitution technique was implemented and executed
successfully
Ex.No. : 1b PLAYFAIR CIPHER
Date :
AIM:
ALGORITHM:
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
PROGRAM:
import java.util.Scanner;
String key=in.nextLine();
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("=============================");
pfEncryption.doPlayFair(en, "Decrypt");
pfEncryption.getDecrypted());
class PFEncryption
keyword=keyword.toUpperCase().replace("J","I");
int val=0;
int uniqueLen;
{
present=false;
uniqueLen=0;
if (Character.toString(uniqueChar[k])==null)
break;
uniqueLen++;
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;
alphabets[i][j]=uniqueChar[val];
val++;
System.out.print(alphabets[i][j] + "\t");
System.out.println();
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))
msg=msg.replaceFirst(intermediate, newTxt);
len++;
len-=2;
val+=2;
if (msg.length()%2!=0)
msg=msg+'x';
int val=0;
while (val<msg.length())
searchAndEncryptOrDecrypt(msg.substring(val,val+2),tag);
val+=2;
}}
char ch1=doubblyCh.charAt(0);
char ch2=doubblyCh.charAt(1);
if (alphabets[i][j]==ch1)
row1=i;
col1=j;
else if (alphabets[i][j]==ch2)
row2=i;
col2=j;
}}}
if (tag=="Encrypt")
else if(tag=="Decrypt")
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]));
}}
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:
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. ..
=============================
Decrypting...
RESULT:
Thus the Playfair cipher substitution technique was implemented and executed
Successfully
Ex.No. : 1c HILL CIPHER
Date :
AIM:
ALGORITHM:
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.
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
PROGRAM:
import java.util.Scanner;
import javax.swing.JOptionPane;
{ 1, 2, 1 },
{ 2, 3, 2 },
{ 2, 2, 1 },
};
{ -1, 0, 1 },
{ 2, -1, 0 },
{ -2, 2, -1},
};
int ch, n;
text=sc.next();
text = text.toUpperCase();
n = text.length() % 3;
if(n!=0)
text+= 'X';
outtext += encrypt(ptextchars[i],ptextchars[i+1],ptextchars[i+2]);
outtext1 += decrypt(ptextchars1[i],ptextchars1[i+1],ptextchars1[i+2]);
int x,y, z;
a = key.charAt(x%26);
b = key.charAt(y%26);
c = key.charAt(z%26);
ret = "" + a + b + c;
return ret;
int x,y,z;
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:
Padded Text:MOTHERTHERESAXX
RESULT:
Thus the Hill cipher substitution technique was implemented and executed
successfully.