0% found this document useful (0 votes)
62 views10 pages

Cryptography Lab 1 PDF

The document outlines two lab tasks for the BCSE309P course focusing on cryptography. Task 1 involves implementing the Playfair cipher in Java, detailing its algorithm and providing sample code for encryption and decryption. Task 2 covers the Caesar Cipher algorithm, explaining its encryption and decryption processes along with corresponding Java code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views10 pages

Cryptography Lab 1 PDF

The document outlines two lab tasks for the BCSE309P course focusing on cryptography. Task 1 involves implementing the Playfair cipher in Java, detailing its algorithm and providing sample code for encryption and decryption. Task 2 covers the Caesar Cipher algorithm, explaining its encryption and decryption processes along with corresponding Java code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

School of Computer Science and Engineering

(SCOPE)

Winter Semester 2024

BCSE309P: Cryptography and Network Security Lab

LAB ASSESSMENT 1

Faculty: Porf. Umamaheshwari M


Name: Pari Gawli
Registration No: 21BKT0023
TASK 1:

Aim: To implement a Playfair cipher substitution technique in Java

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

1. Read the key as input from the user.


2. Then create the key table of 5x5 grids of alphabets.
3. Get the plain text as an input.
4. Then split the plain text message into pairs of two letters(digraphs).
5. Pair cannot be made with same letter. Break the letter in single and add a bogus letter
z to the previous letter
6. If both the letters are in the same column, take the letter below each one.
7. If both 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

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.

Algorithm for Caesar Cipher


Encryption Algorithm:
1. Start.
2. Input the plaintext message (P) and the shift key (K).
3. For each character in the plaintext:
o If the character is an uppercase letter:
1. Shift its ASCII value by K.
2. Wrap around using modular arithmetic for letters exceeding 'Z'.
o If the character is a lowercase letter:
1. Shift its ASCII value by K.
2. Wrap around using modular arithmetic for letters exceeding 'z'.
o Retain non-alphabetic characters as-is.
4. Append the encrypted character to the ciphertext.
5. Output the ciphertext.
6. Stop.
Decryption Algorithm:
1. Start.
2. Input the ciphertext message (C) and the shift key (K).
3. For each character in the ciphertext:
o If the character is an uppercase letter:
1. Shift its ASCII value back by K.
2. Wrap around using modular arithmetic for letters below 'A'.
o If the character is a lowercase letter:
1. Shift its ASCII value back by K.
2. Wrap around using modular arithmetic for letters below 'a'.
o Retain non-alphabetic characters as-is.
4. Append the decrypted character to the plaintext.
5. Output the plaintext.
6. Stop.

CODE:

import java.util.Scanner;

public class Main {

public static String encrypt(String text, int shift) {


StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char character = text.charAt(i);
if (Character.isUpperCase(character)) {
char encryptedChar = (char) (((character - 'A' + shift) % 26) + 'A');
result.append(encryptedChar);
} else if (Character.isLowerCase(character)) {
char encryptedChar = (char) (((character - 'a' + shift) % 26) + 'a');
result.append(encryptedChar);
} else {
result.append(character);
}
}
return result.toString();
}

public static String decrypt(String text, int shift) {


return encrypt(text, 26 - (shift % 26));
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("21BKT0023 Enter the text to encrypt: ");
String plaintext = scanner.nextLine();
System.out.print("Enter the shift value (0-25): ");
int shift = scanner.nextInt();
String encrypted = encrypt(plaintext, shift);
System.out.println("Encrypted text: " + encrypted);
String decrypted = decrypt(encrypted, shift);
System.out.println("Decrypted text: " + decrypted);
scanner.close();
}
}
OUTPUT:

1.

2.

You might also like