Cryptography Lab 1 PDF
Cryptography Lab 1 PDF
(SCOPE)
LAB ASSESSMENT 1
Concept to be Applied:
• The Playfair cipher was the first practical digraph substitution cipher.
• The scheme was invented in 1854 by Charles Wheatstone but was named after Lord
Playfair who promoted the use of the cipher.
• In Playfair cipher unlike traditional cipher we encrypt a pair of alphabets (digraphs)
instead of a single alphabet.
• Playfair is reasonably fast to use and requires no special equipment
• Generate the key Square Matrix(5×5):
• The key square is a 5×5 grid of alphabets that acts as the key for encrypting the
plaintext. Each of the 25 alphabets must be unique and one letter of the alphabet
(usually J) is omitted from the table (as the table can hold only 25 alphabets). If the
plaintext contains J, then it is replaced by I.
• The initial alphabets in the key square are the unique alphabets of the key in the order
in which they appear followed by the remaining letters of the alphabet in order.
• Algorithm to encrypt the plain text: The plaintext is split into pairs of two letters
(digraphs). If there is an odd number of letters, a Z is added to the last letter.
• Pair cannot be made with same letter. Break the letter in single and add a bogus letter
to the previous letter
• If the letter is standing alone in the process of pairing, then add an extra bogus letter
with the alone letter
• If both the letters are in the same column: Take the letter below each one (going back
to the top if at the bottom).
• If both the letters are in the same row: Take the letter to the right of each one (going
back to the leftmost if at the rightmost position).
• If neither of the above rules is true: Form a rectangle with the two letters and take the
letters on the horizontal opposite corner of the rectangle.
Algorithm
CODE:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter keyword: ");
String key=in.nextLine();
System.out.print("21BKT0023 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 (keyword.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:
1.
2.
TASK 2:
Aim: To implement the Caesar Cipher algorithm for secure communication by encrypting and decrypting
messages using a fixed shift value.
Concept to Be Applied:
The Caesar Cipher is a substitution cipher where each letter in the plaintext is shifted a certain number of
places down or up the alphabet. The concept relies on modular arithmetic to wrap around the alphabet and
support both encryption and decryption:
1. Encryption:
Each character in the plaintext is shifted by a fixed value (key).
C=(P+K)mod 26C = (P + K) \mod 26
Where:
o CC = Ciphertext letter (encrypted letter)
o PP = Plaintext letter
o KK = Shift value (key)
2. Decryption:
The reverse of encryption is performed by shifting the characters in the ciphertext back by the same
key.
P=(C−K+26)mod 26P = (C - K + 26) \mod 26
3. Non-Alphabet Characters:
non-alphabetic characters are not shifted but retained as-is.
The Caesar Cipher is easy to implement and understand but provides minimal security as the key space is
limited.
CODE:
import java.util.Scanner;
1.
2.