From b77076b0aac8bc913d1e8c69165e0b7ee3e50edc Mon Sep 17 00:00:00 2001 From: Govind Gupta <202152312@iiitvadodara.ac.in> Date: Tue, 2 Jan 2024 22:14:36 +0530 Subject: [PATCH 1/5] Added Playfair Cipher --- .../thealgorithms/ciphers/PlayfairCipher.java | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java diff --git a/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java new file mode 100644 index 000000000000..1a1896397bb5 --- /dev/null +++ b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java @@ -0,0 +1,151 @@ +import java.util.Scanner; + +public class PlayfairCipher { + + private char[][] matrix; + private String key; + + public PlayfairCipher(String key) { + this.key = key; + generateMatrix(); + } + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + System.out.print("\nEnter the key: "); + String key = sc.nextLine(); + + System.out.print("\nEnter the plaintext: "); + String plaintext = sc.nextLine(); + + PlayfairCipher cipher = new PlayfairCipher(key); + cipher.printMatrix(); + + // Encryption + String encryptedText = cipher.encrypt(plaintext); + System.out.println("\nPlaintext: " + plaintext); + System.out.println("\nEncrypted Text: " + encryptedText); + + // Decryption + String decryptedText = cipher.decrypt(encryptedText); + System.out.println("\nDecrypted Text: " + decryptedText); + } + + private void generateMatrix() { + String keyWithoutDuplicates = removeDuplicateChars(key + "ABCDEFGHIKLMNOPQRSTUVWXYZ"); + matrix = new char[5][5]; + int index = 0; + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + matrix[i][j] = keyWithoutDuplicates.charAt(index); + index++; + } + } + } + + private String removeDuplicateChars(String str) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + if (result.indexOf(String.valueOf(str.charAt(i))) == -1) { + result.append(str.charAt(i)); + } + } + return result.toString(); + } + + public String encrypt(String plaintext) { + plaintext = prepareText(plaintext.replace("J", "I")); + StringBuilder ciphertext = new StringBuilder(); + for (int i = 0; i < plaintext.length(); i += 2) { + char char1 = plaintext.charAt(i); + char char2 = plaintext.charAt(i + 1); + int[] pos1 = findPosition(char1); + int[] pos2 = findPosition(char2); + int row1 = pos1[0]; + int col1 = pos1[1]; + int row2 = pos2[0]; + int col2 = pos2[1]; + if (row1 == row2) { + ciphertext.append(matrix[row1][(col1 + 1) % 5]); + ciphertext.append(matrix[row2][(col2 + 1) % 5]); + } else if (col1 == col2) { + ciphertext.append(matrix[(row1 + 1) % 5][col1]); + ciphertext.append(matrix[(row2 + 1) % 5][col2]); + } else { + ciphertext.append(matrix[row1][col2]); + ciphertext.append(matrix[row2][col1]); + } + } + return ciphertext.toString(); + } + + public String decrypt(String ciphertext) { + StringBuilder plaintext = new StringBuilder(); + for (int i = 0; i < ciphertext.length(); i += 2) { + char char1 = ciphertext.charAt(i); + char char2 = ciphertext.charAt(i + 1); + int[] pos1 = findPosition(char1); + int[] pos2 = findPosition(char2); + int row1 = pos1[0]; + int col1 = pos1[1]; + int row2 = pos2[0]; + int col2 = pos2[1]; + if (row1 == row2) { + plaintext.append(matrix[row1][(col1 + 4) % 5]); + plaintext.append(matrix[row2][(col2 + 4) % 5]); + } else if (col1 == col2) { + plaintext.append(matrix[(row1 + 4) % 5][col1]); + plaintext.append(matrix[(row2 + 4) % 5][col2]); + } else { + plaintext.append(matrix[row1][col2]); + plaintext.append(matrix[row2][col1]); + } + } + return plaintext.toString(); + } + + private String prepareText(String text) { + text = text.toUpperCase().replaceAll("[^A-Z]", ""); + StringBuilder preparedText = new StringBuilder(); + char prevChar = '\0'; + for (char c : text.toCharArray()) { + if (c != prevChar) { + preparedText.append(c); + prevChar = c; + } else { + preparedText.append('X').append(c); + prevChar = '\0'; + } + } + if (preparedText.length() % 2 != 0) { + preparedText.append('X'); + } + System.out.println("\nPrepared Text : " + preparedText.toString()); + return preparedText.toString(); + } + + private int[] findPosition(char c) { + int[] pos = new int[2]; + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + if (matrix[i][j] == c) { + pos[0] = i; + pos[1] = j; + return pos; + } + } + } + return pos; + } + + public void printMatrix() { + System.out.println("\nPlayfair Cipher Matrix:"); + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + System.out.print(matrix[i][j] + " "); + } + System.out.println(); + } + } +} From 434f3cfe05d40b5282e696582ebc70ed5b1c19dc Mon Sep 17 00:00:00 2001 From: Govind Gupta <202152312@iiitvadodara.ac.in> Date: Wed, 3 Jan 2024 00:06:05 +0530 Subject: [PATCH 2/5] Added JUnit Test --- .../thealgorithms/ciphers/PlayfairCipher.java | 2 + .../thealgorithms/ciphers/PlayfairTest.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/test/java/com/thealgorithms/ciphers/PlayfairTest.java diff --git a/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java index 1a1896397bb5..970030ea6770 100644 --- a/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java @@ -1,3 +1,5 @@ +package com.thealgorithms.ciphers; + import java.util.Scanner; public class PlayfairCipher { diff --git a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java new file mode 100644 index 000000000000..2dc01f719bb9 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java @@ -0,0 +1,37 @@ +package com.thealgorithms.ciphers; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class PlayfairTest { + + @Test + public void testEncryption() { + PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); + + String plaintext = "HELLO"; + String encryptedText = playfairCipher.encrypt(plaintext); + assertEquals("GYIZSC", encryptedText); + } + + @Test + public void testDecryption() { + PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); + + String encryptedText = "UDRIYP"; + String decryptedText = playfairCipher.decrypt(encryptedText); + assertEquals("NEBFVH", decryptedText); + } + + @Test + public void testEncryptionAndDecryption() { + PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); + + String plaintext = "PLAYFAIR"; + String encryptedText = playfairCipher.encrypt(plaintext); + String decryptedText = playfairCipher.decrypt(encryptedText); + + assertEquals(plaintext, decryptedText); + } + +} From b7b79b26f98551e6fd6a9d64909e7433c0483e76 Mon Sep 17 00:00:00 2001 From: Govind Gupta <202152312@iiitvadodara.ac.in> Date: Wed, 3 Jan 2024 00:11:14 +0530 Subject: [PATCH 3/5] Added JUnit Test --- src/test/java/com/thealgorithms/ciphers/PlayfairTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java index 2dc01f719bb9..da00d7bf03d6 100644 --- a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java +++ b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java @@ -1,8 +1,9 @@ package com.thealgorithms.ciphers; -import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + public class PlayfairTest { @Test From bcf94a70f47a877924071e0f30745821f78d856f Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 2 Jan 2024 18:59:59 +0000 Subject: [PATCH 4/5] Update directory --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index fd50b2914cff..b621216da7f1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -27,6 +27,7 @@ * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) + * [SetKthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SetKthBit.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) * ciphers * a5 @@ -44,6 +45,7 @@ * [ColumnarTranspositionCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ColumnarTranspositionCipher.java) * [DES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/DES.java) * [HillCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/HillCipher.java) + * [PlayfairCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java) * [Polybius](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/Polybius.java) * [ProductCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ProductCipher.java) * [RSA](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/RSA.java) @@ -585,6 +587,7 @@ * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) + * [SetKthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SetKthBitTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) * ciphers * a5 @@ -592,6 +595,7 @@ * [BlowfishTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/BlowfishTest.java) * [CaesarTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/CaesarTest.java) * [DESTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/DESTest.java) + * [PlayfairTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java) * [PolybiusTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java) * [RSATest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/RSATest.java) * [SimpleSubCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/SimpleSubCipherTest.java) From 34f9af6371671e1a8a4a7d669d9ead7183494fce Mon Sep 17 00:00:00 2001 From: Govind Gupta <202152312@iiitvadodara.ac.in> Date: Wed, 3 Jan 2024 10:02:36 +0530 Subject: [PATCH 5/5] modified format --- .../thealgorithms/ciphers/PlayfairCipher.java | 69 ++++++------------- .../thealgorithms/ciphers/PlayfairTest.java | 9 ++- 2 files changed, 26 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java index 970030ea6770..76ceb6dbce31 100644 --- a/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java +++ b/src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java @@ -1,7 +1,5 @@ package com.thealgorithms.ciphers; -import java.util.Scanner; - public class PlayfairCipher { private char[][] matrix; @@ -12,50 +10,6 @@ public PlayfairCipher(String key) { generateMatrix(); } - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - - System.out.print("\nEnter the key: "); - String key = sc.nextLine(); - - System.out.print("\nEnter the plaintext: "); - String plaintext = sc.nextLine(); - - PlayfairCipher cipher = new PlayfairCipher(key); - cipher.printMatrix(); - - // Encryption - String encryptedText = cipher.encrypt(plaintext); - System.out.println("\nPlaintext: " + plaintext); - System.out.println("\nEncrypted Text: " + encryptedText); - - // Decryption - String decryptedText = cipher.decrypt(encryptedText); - System.out.println("\nDecrypted Text: " + decryptedText); - } - - private void generateMatrix() { - String keyWithoutDuplicates = removeDuplicateChars(key + "ABCDEFGHIKLMNOPQRSTUVWXYZ"); - matrix = new char[5][5]; - int index = 0; - for (int i = 0; i < 5; i++) { - for (int j = 0; j < 5; j++) { - matrix[i][j] = keyWithoutDuplicates.charAt(index); - index++; - } - } - } - - private String removeDuplicateChars(String str) { - StringBuilder result = new StringBuilder(); - for (int i = 0; i < str.length(); i++) { - if (result.indexOf(String.valueOf(str.charAt(i))) == -1) { - result.append(str.charAt(i)); - } - } - return result.toString(); - } - public String encrypt(String plaintext) { plaintext = prepareText(plaintext.replace("J", "I")); StringBuilder ciphertext = new StringBuilder(); @@ -107,6 +61,28 @@ public String decrypt(String ciphertext) { return plaintext.toString(); } + private void generateMatrix() { + String keyWithoutDuplicates = removeDuplicateChars(key + "ABCDEFGHIKLMNOPQRSTUVWXYZ"); + matrix = new char[5][5]; + int index = 0; + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + matrix[i][j] = keyWithoutDuplicates.charAt(index); + index++; + } + } + } + + private String removeDuplicateChars(String str) { + StringBuilder result = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + if (result.indexOf(String.valueOf(str.charAt(i))) == -1) { + result.append(str.charAt(i)); + } + } + return result.toString(); + } + private String prepareText(String text) { text = text.toUpperCase().replaceAll("[^A-Z]", ""); StringBuilder preparedText = new StringBuilder(); @@ -123,7 +99,6 @@ private String prepareText(String text) { if (preparedText.length() % 2 != 0) { preparedText.append('X'); } - System.out.println("\nPrepared Text : " + preparedText.toString()); return preparedText.toString(); } diff --git a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java index da00d7bf03d6..5562241b48db 100644 --- a/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java +++ b/src/test/java/com/thealgorithms/ciphers/PlayfairTest.java @@ -9,7 +9,7 @@ public class PlayfairTest { @Test public void testEncryption() { PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); - + String plaintext = "HELLO"; String encryptedText = playfairCipher.encrypt(plaintext); assertEquals("GYIZSC", encryptedText); @@ -18,7 +18,7 @@ public void testEncryption() { @Test public void testDecryption() { PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); - + String encryptedText = "UDRIYP"; String decryptedText = playfairCipher.decrypt(encryptedText); assertEquals("NEBFVH", decryptedText); @@ -27,12 +27,11 @@ public void testDecryption() { @Test public void testEncryptionAndDecryption() { PlayfairCipher playfairCipher = new PlayfairCipher("KEYWORD"); - + String plaintext = "PLAYFAIR"; String encryptedText = playfairCipher.encrypt(plaintext); String decryptedText = playfairCipher.decrypt(encryptedText); - + assertEquals(plaintext, decryptedText); } - }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: