EXPERIMENT NO:- 02
NAME:- Vanshika Nitin Bedmutha
CLASS:- TY-03
BATCH:- A
ROLL NO: 05
SUBJECT:-
CSS
AIM:-Design and implement a product cipher using Playfair and Transposition ciphers.
PROGRAM:-
import java.util.*;
public class ProductCipher {
// Method to create the Playfair cipher matrix
public static char[][] generatePlayfairMatrix(String key) {
key = key.toUpperCase().replaceAll("[^A-Z]", "").replace('J', 'I');
Set<Character> uniqueChars = new LinkedHashSet<>();
for (char c : key.toCharArray()) {
uniqueChars.add(c);
}
// Add remaining characters of the alphabet to the matrix
for (char c = 'A'; c <= 'Z'; c++) {
if (c != 'J' && !uniqueChars.contains(c)) {
uniqueChars.add(c);
}
}
// Convert to 5x5 matrix
char[][] matrix = new char[5][5];
Iterator<Character> iterator = uniqueChars.iterator();
int index = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
matrix[i][j] = iterator.next();
}
}
return matrix;
}
// Method to find the position of a character in the Playfair
matrix public static int[] findCharPosition(char[][] matrix, char
ch) {
int[] position = new int[2];
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (matrix[i][j] == ch) {
position[0] = i;
position[1] = j;
return position;
}
}
}
return position;
}
// Method to encrypt a pair of characters using Playfair cipher
public static String playfairEncryptPair(char[][] matrix, char first, char second) {
int[] firstPos = findCharPosition(matrix, first);
int[] secondPos = findCharPosition(matrix, second);
// Same row
if (firstPos[0] == secondPos[0]) {
return String.valueOf(matrix[firstPos[0]][(firstPos[1] + 1) % 5]) +
String.valueOf(matrix[secondPos[0]][(secondPos[1] + 1) % 5]);
}
// Same column
else if (firstPos[1] == secondPos[1]) {
return String.valueOf(matrix[(firstPos[0] + 1) % 5][firstPos[1]]) +
String.valueOf(matrix[(secondPos[0] + 1) % 5][secondPos[1]]);
}
// Rectangle case
else {
return String.valueOf(matrix[firstPos[0]][secondPos[1]]) +
String.valueOf(matrix[secondPos[0]][firstPos[1]]);
}
}
// Method to preprocess the message for Playfair cipher (pairwise)
public static String preprocessForPlayfair(String message) {
message = message.toUpperCase().replaceAll("[^A-Z]", "").replace('J', 'I');
StringBuilder sb = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
sb.append(message.charAt(i));
if (i + 1 < message.length() && message.charAt(i) == message.charAt(i + 1)) {
sb.append('X');
}
}
if (sb.length() % 2 != 0) {
sb.append('X');
}
return sb.toString();
}
// Playfair cipher encryption
public static String playfairCipher(String message, String key) {
char[][] matrix = generatePlayfairMatrix(key);
message = preprocessForPlayfair(message);
StringBuilder encryptedMessage = new StringBuilder();
for (int i = 0; i < message.length(); i += 2) {
char first = message.charAt(i);
char second = message.charAt(i + 1);
encryptedMessage.append(playfairEncryptPair(matrix, first, second));
}
return encryptedMessage.toString();
}
// Method to implement the Transposition Cipher
public static String transpositionCipher(String message, int key) {
char[] messageArray = message.toCharArray();
int length = messageArray.length;
int rows = (int) Math.ceil((double) length / key); char[]
[] grid = new char[rows][key];
// Fill the grid with characters
int idx = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < key; j++) {
if (idx < length) {
grid[i][j] = messageArray[idx++];
} else {
grid[i][j] = 'X'; // Padding character
}
}
}
// Read column-wise to generate ciphertext
StringBuilder encryptedMessage = new StringBuilder();
for (int j = 0; j < key; j++) {
for (int i = 0; i < rows; i++) {
encryptedMessage.append(grid[i][j]);
}
}
return encryptedMessage.toString();
}
// Main method to run the product cipher (Playfair + Transposition)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the message to encrypt: ");
String message = sc.nextLine();
System.out.print("Enter the Playfair cipher key: ");
String playfairKey = sc.nextLine();
System.out.print("Enter the Transposition cipher key (integer): ");
int transpositionKey = sc.nextInt();
// First apply the Playfair cipher
String playfairEncrypted = playfairCipher(message, playfairKey);
System.out.println("Playfair Encrypted Message: " + playfairEncrypted);
// Then apply the Transposition cipher
String finalEncrypted = transpositionCipher(playfairEncrypted, transpositionKey);
System.out.println("Final Encrypted Message after Transposition: " + finalEncrypted);
}
}
OUTPUT:-
Playfair and Transposition Ciphers used to Encrypt the message.
Review Questions:
Conclusion:In conclusion, we have successfully designed and implemented a product cipher
combining Playfair and Transposition ciphers to enhance encryption security. This approach
effectively improves data confidentiality and strengthens overall security.