How to resolve the algorithm Playfair cipher step by step in the Java programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Playfair cipher step by step in the Java programming language
Table of Contents
Problem Statement
Implement a Playfair cipher for encryption and decryption.
The user must be able to choose J = I or no Q in the alphabet. The output of the encrypted and decrypted message must be in capitalized digraphs, separated by spaces.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Playfair cipher step by step in the Java programming language
Playfair Cipher
Java Code 1:
Description: This Java code implements the Playfair cipher, a classic encryption algorithm.
Key Features:
- Generates a 5x5 matrix based on a keyword.
- Prepares the plaintext by replacing 'J' with 'I' and removing non-alphabetic characters.
- Encodes the text by applying a series of transformations to pairs of characters.
- Decodes the text by inverting these transformations.
Detailed Breakdown:
- Constants:
charTable
: 5x5 character matrix used for encryption and decryption.positions
: Stores the positions of characters incharTable
.
main
Method:- Prompts the user for a key, message, and whether to replace 'J' with 'I'.
- Creates the character table based on the key.
- Encodes and decodes the message using the
encode()
anddecode()
methods.
prompt
Method:- Prompts the user for input and verifies that it meets a minimum length requirement.
prepareText
Method:- Uppercases, removes non-alphabetic characters, and optionally replaces 'J' with 'I'.
createTable
Method:- Fills the character table with characters from the prepared key and assigns positions to each character.
encode
Method:- Pads the message with 'X' if it is an odd length.
- Encodes pairs of characters by applying specific transformations based on their positions in the character table.
decode
Method:- Inverts the transformations applied in the
encode
method to recover the original plaintext.
- Inverts the transformations applied in the
codec
Method:- Implements the core encoding or decoding logic by updating the characters in a StringBuilder based on their positions in the character table.
Java Code 2:
Description: This Java code also implements the Playfair cipher.
Key Features:
- Requires the user to enter a keyword and message.
- Formats the message by pairing characters and inserting 'X' if necessary.
- Encrypts the message by applying a series of transformations to pairs of characters.
Detailed Breakdown:
- Class Properties:
KeyWord
: User-entered keyword.Key
: Generated key used for encryption.matrix_arr
: 5x5 character matrix used for encryption.
setKey
Method:- Assigns the user-entered keyword to the
KeyWord
property.
- Assigns the user-entered keyword to the
KeyGen
Method:- Generates the encryption key by appending unique characters from the keyword to the alphabet.
- Creates the character matrix
matrix_arr
based on the generated key.
format
Method:- Formats the plaintext by replacing 'J' with 'I' and pairing characters.
Divid2Pairs
Method:- Divides the formatted plaintext into pairs of characters.
GetDiminsions
Method:- Finds the position of a character in the character matrix.
encryptMessage
Method:- Encrypts the message by applying a series of transformations to pairs of characters based on their positions in the character matrix.
main
Method:- Prompts the user for a keyword and message.
- Creates an instance of the
PlayfairCipherEncryption
class. - Encrypts the message using the
encryptMessage
method.
Source code in the java programming language
import java.awt.Point;
import java.util.Scanner;
public class PlayfairCipher {
private static char[][] charTable;
private static Point[] positions;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String key = prompt("Enter an encryption key (min length 6): ", sc, 6);
String txt = prompt("Enter the message: ", sc, 1);
String jti = prompt("Replace J with I? y/n: ", sc, 1);
boolean changeJtoI = jti.equalsIgnoreCase("y");
createTable(key, changeJtoI);
String enc = encode(prepareText(txt, changeJtoI));
System.out.printf("%nEncoded message: %n%s%n", enc);
System.out.printf("%nDecoded message: %n%s%n", decode(enc));
}
private static String prompt(String promptText, Scanner sc, int minLen) {
String s;
do {
System.out.print(promptText);
s = sc.nextLine().trim();
} while (s.length() < minLen);
return s;
}
private static String prepareText(String s, boolean changeJtoI) {
s = s.toUpperCase().replaceAll("[^A-Z]", "");
return changeJtoI ? s.replace("J", "I") : s.replace("Q", "");
}
private static void createTable(String key, boolean changeJtoI) {
charTable = new char[5][5];
positions = new Point[26];
String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", changeJtoI);
int len = s.length();
for (int i = 0, k = 0; i < len; i++) {
char c = s.charAt(i);
if (positions[c - 'A'] == null) {
charTable[k / 5][k % 5] = c;
positions[c - 'A'] = new Point(k % 5, k / 5);
k++;
}
}
}
private static String encode(String s) {
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < sb.length(); i += 2) {
if (i == sb.length() - 1)
sb.append(sb.length() % 2 == 1 ? 'X' : "");
else if (sb.charAt(i) == sb.charAt(i + 1))
sb.insert(i + 1, 'X');
}
return codec(sb, 1);
}
private static String decode(String s) {
return codec(new StringBuilder(s), 4);
}
private static String codec(StringBuilder text, int direction) {
int len = text.length();
for (int i = 0; i < len; i += 2) {
char a = text.charAt(i);
char b = text.charAt(i + 1);
int row1 = positions[a - 'A'].y;
int row2 = positions[b - 'A'].y;
int col1 = positions[a - 'A'].x;
int col2 = positions[b - 'A'].x;
if (row1 == row2) {
col1 = (col1 + direction) % 5;
col2 = (col2 + direction) % 5;
} else if (col1 == col2) {
row1 = (row1 + direction) % 5;
row2 = (row2 + direction) % 5;
} else {
int tmp = col1;
col1 = col2;
col2 = tmp;
}
text.setCharAt(i, charTable[row1][col1]);
text.setCharAt(i + 1, charTable[row2][col2]);
}
return text.toString();
}
}
import java.util.Scanner;
public class PlayfairCipherEncryption
{
private String KeyWord = new String();
private String Key = new String();
private char matrix_arr[][] = new char[5][5];
public void setKey(String k)
{
String K_adjust = new String();
boolean flag = false;
K_adjust = K_adjust + k.charAt(0);
for (int i = 1; i < k.length(); i++)
{
for (int j = 0; j < K_adjust.length(); j++)
{
if (k.charAt(i) == K_adjust.charAt(j))
{
flag = true;
}
}
if (flag == false)
K_adjust = K_adjust + k.charAt(i);
flag = false;
}
KeyWord = K_adjust;
}
public void KeyGen()
{
boolean flag = true;
char current;
Key = KeyWord;
for (int i = 0; i < 26; i++)
{
current = (char) (i + 97);
if (current == 'j')
continue;
for (int j = 0; j < KeyWord.length(); j++)
{
if (current == KeyWord.charAt(j))
{
flag = false;
break;
}
}
if (flag)
Key = Key + current;
flag = true;
}
System.out.println(Key);
matrix();
}
private void matrix()
{
int counter = 0;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
matrix_arr[i][j] = Key.charAt(counter);
System.out.print(matrix_arr[i][j] + " ");
counter++;
}
System.out.println();
}
}
private String format(String old_text)
{
int i = 0;
int len = 0;
String text = new String();
len = old_text.length();
for (int tmp = 0; tmp < len; tmp++)
{
if (old_text.charAt(tmp) == 'j')
{
text = text + 'i';
}
else
text = text + old_text.charAt(tmp);
}
len = text.length();
for (i = 0; i < len; i = i + 2)
{
if (text.charAt(i + 1) == text.charAt(i))
{
text = text.substring(0, i + 1) + 'x' + text.substring(i + 1);
}
}
return text;
}
private String[] Divid2Pairs(String new_string)
{
String Original = format(new_string);
int size = Original.length();
if (size % 2 != 0)
{
size++;
Original = Original + 'x';
}
String x[] = new String[size / 2];
int counter = 0;
for (int i = 0; i < size / 2; i++)
{
x[i] = Original.substring(counter, counter + 2);
counter = counter + 2;
}
return x;
}
public int[] GetDiminsions(char letter)
{
int[] key = new int[2];
if (letter == 'j')
letter = 'i';
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (matrix_arr[i][j] == letter)
{
key[0] = i;
key[1] = j;
break;
}
}
}
return key;
}
public String encryptMessage(String Source)
{
String src_arr[] = Divid2Pairs(Source);
String Code = new String();
char one;
char two;
int part1[] = new int[2];
int part2[] = new int[2];
for (int i = 0; i < src_arr.length; i++)
{
one = src_arr[i].charAt(0);
two = src_arr[i].charAt(1);
part1 = GetDiminsions(one);
part2 = GetDiminsions(two);
if (part1[0] == part2[0])
{
if (part1[1] < 4)
part1[1]++;
else
part1[1] = 0;
if (part2[1] < 4)
part2[1]++;
else
part2[1] = 0;
}
else if (part1[1] == part2[1])
{
if (part1[0] < 4)
part1[0]++;
else
part1[0] = 0;
if (part2[0] < 4)
part2[0]++;
else
part2[0] = 0;
}
else
{
int temp = part1[1];
part1[1] = part2[1];
part2[1] = temp;
}
Code = Code + matrix_arr[part1[0]][part1[1]]
+ matrix_arr[part2[0]][part2[1]];
}
return Code;
}
public static void main(String[] args)
{
PlayfairCipherEncryption x = new PlayfairCipherEncryption();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a keyword:");
String keyword = sc.next();
x.setKey(keyword);
x.KeyGen();
System.out
.println("Enter word to encrypt: (Make sure length of message is even)");
String key_input = sc.next();
if (key_input.length() % 2 == 0)
{
System.out.println("Encryption: " + x.encryptMessage(key_input));
}
else
{
System.out.println("Message length should be even");
}
sc.close();
}
}
You may also check:How to resolve the algorithm Flow-control structures step by step in the Java programming language
You may also check:How to resolve the algorithm Go Fish step by step in the Aime programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Snake step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Number names step by step in the BlitzMax programming language