From defd98ee224b7b2675471cd2e6664032721cae83 Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 14:31:10 -0400 Subject: [PATCH 1/8] Added Autokey cipher and AutokeyTest.java --- .../com/thealgorithms/ciphers/Autokey.java | 57 +++++++++++++++++++ .../thealgorithms/ciphers/AutokeyTest.java | 36 ++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/main/java/com/thealgorithms/ciphers/Autokey.java create mode 100644 src/test/java/com/thealgorithms/ciphers/AutokeyTest.java diff --git a/src/main/java/com/thealgorithms/ciphers/Autokey.java b/src/main/java/com/thealgorithms/ciphers/Autokey.java new file mode 100644 index 000000000000..7b4ebef72733 --- /dev/null +++ b/src/main/java/com/thealgorithms/ciphers/Autokey.java @@ -0,0 +1,57 @@ +package com.thealgorithms.ciphers; + +import java.util.HashMap; +import java.util.Map; + +/** + * The Autokey Cipher is an interesting and historically significant encryption method, + * as it improves upon the classic Vigenère Cipher by using the plaintext itself to + * extend the key. This makes it harder to break using frequency analysis, as it + * doesn’t rely solely on a repeated key. + * + * @author bennybebo + */ +public class AutokeyCipher { + + // Encrypts the plaintext using the Autokey cipher + public String encrypt(String plaintext, String keyword) { + plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input + keyword = keyword.toUpperCase(); + + StringBuilder extendedKey = new StringBuilder(keyword); + extendedKey.append(plaintext); // Extend key with plaintext + + StringBuilder ciphertext = new StringBuilder(); + + for (int i = 0; i < plaintext.length(); i++) { + char plainChar = plaintext.charAt(i); + char keyChar = extendedKey.charAt(i); + + int encryptedChar = ((plainChar - 'A') + (keyChar - 'A')) % 26 + 'A'; + ciphertext.append((char) encryptedChar); + } + + return ciphertext.toString(); + } + + // Decrypts the ciphertext using the Autokey cipher + public String decrypt(String ciphertext, String keyword) { + ciphertext = ciphertext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input + keyword = keyword.toUpperCase(); + + StringBuilder plaintext = new StringBuilder(); + StringBuilder extendedKey = new StringBuilder(keyword); + + for (int i = 0; i < ciphertext.length(); i++) { + char cipherChar = ciphertext.charAt(i); + char keyChar = extendedKey.charAt(i); + + int decryptedChar = ((cipherChar - 'A') - (keyChar - 'A') + 26) % 26 + 'A'; + plaintext.append((char) decryptedChar); + + extendedKey.append((char) decryptedChar); // Extend key with each decrypted char + } + + return plaintext.toString(); + } +} diff --git a/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java b/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java new file mode 100644 index 000000000000..5d69a483d8c7 --- /dev/null +++ b/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java @@ -0,0 +1,36 @@ +package com.thealgorithms.ciphers; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class AutokeyCipherTest { + + AutokeyCipher autokeyCipher = new AutokeyCipher(); + + @Test + void autokeyEncryptTest() { + // given + String plaintext = "MEET AT DAWN"; + String keyword = "QUEEN"; + + // when + String cipherText = autokeyCipher.encrypt(plaintext, keyword); + + // then + assertEquals("QKUC YT DVZN", cipherText); + } + + @Test + void autokeyDecryptTest() { + // given + String ciphertext = "QKUC YT DVZN"; + String keyword = "QUEEN"; + + // when + String plainText = autokeyCipher.decrypt(ciphertext, keyword); + + // then + assertEquals("MEET AT DAWN", plainText); + } +} From 71677214d30952043753db23f7beb5738261e8e5 Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 14:33:13 -0400 Subject: [PATCH 2/8] Added wikipedia link to Autokey.java --- src/main/java/com/thealgorithms/ciphers/Autokey.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/ciphers/Autokey.java b/src/main/java/com/thealgorithms/ciphers/Autokey.java index 7b4ebef72733..d7b081fac8fb 100644 --- a/src/main/java/com/thealgorithms/ciphers/Autokey.java +++ b/src/main/java/com/thealgorithms/ciphers/Autokey.java @@ -8,6 +8,7 @@ * as it improves upon the classic Vigenère Cipher by using the plaintext itself to * extend the key. This makes it harder to break using frequency analysis, as it * doesn’t rely solely on a repeated key. + * https://en.wikipedia.org/wiki/Autokey_cipher * * @author bennybebo */ From 0600370bc1f1935c03de58eb9db6237e2b9fa28d Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 14:40:15 -0400 Subject: [PATCH 3/8] Fixed Clang formatting issue --- .../com/thealgorithms/ciphers/Autokey.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/Autokey.java b/src/main/java/com/thealgorithms/ciphers/Autokey.java index d7b081fac8fb..8c6d90928605 100644 --- a/src/main/java/com/thealgorithms/ciphers/Autokey.java +++ b/src/main/java/com/thealgorithms/ciphers/Autokey.java @@ -1,15 +1,12 @@ package com.thealgorithms.ciphers; -import java.util.HashMap; -import java.util.Map; - /** - * The Autokey Cipher is an interesting and historically significant encryption method, - * as it improves upon the classic Vigenère Cipher by using the plaintext itself to - * extend the key. This makes it harder to break using frequency analysis, as it + * The Autokey Cipher is an interesting and historically significant encryption method, + * as it improves upon the classic Vigenère Cipher by using the plaintext itself to + * extend the key. This makes it harder to break using frequency analysis, as it * doesn’t rely solely on a repeated key. * https://en.wikipedia.org/wiki/Autokey_cipher - * + * * @author bennybebo */ public class AutokeyCipher { @@ -18,20 +15,20 @@ public class AutokeyCipher { public String encrypt(String plaintext, String keyword) { plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input keyword = keyword.toUpperCase(); - + StringBuilder extendedKey = new StringBuilder(keyword); extendedKey.append(plaintext); // Extend key with plaintext - + StringBuilder ciphertext = new StringBuilder(); - + for (int i = 0; i < plaintext.length(); i++) { char plainChar = plaintext.charAt(i); char keyChar = extendedKey.charAt(i); - + int encryptedChar = ((plainChar - 'A') + (keyChar - 'A')) % 26 + 'A'; ciphertext.append((char) encryptedChar); } - + return ciphertext.toString(); } @@ -39,20 +36,20 @@ public String encrypt(String plaintext, String keyword) { public String decrypt(String ciphertext, String keyword) { ciphertext = ciphertext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input keyword = keyword.toUpperCase(); - + StringBuilder plaintext = new StringBuilder(); StringBuilder extendedKey = new StringBuilder(keyword); - + for (int i = 0; i < ciphertext.length(); i++) { char cipherChar = ciphertext.charAt(i); char keyChar = extendedKey.charAt(i); - + int decryptedChar = ((cipherChar - 'A') - (keyChar - 'A') + 26) % 26 + 'A'; plaintext.append((char) decryptedChar); - + extendedKey.append((char) decryptedChar); // Extend key with each decrypted char } - + return plaintext.toString(); } } From 3a5dce624c4867422e634689f7f3ba422f641746 Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 14:42:53 -0400 Subject: [PATCH 4/8] Fixed file name error --- src/main/java/com/thealgorithms/ciphers/Autokey.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/Autokey.java b/src/main/java/com/thealgorithms/ciphers/Autokey.java index 8c6d90928605..b78990aaff5c 100644 --- a/src/main/java/com/thealgorithms/ciphers/Autokey.java +++ b/src/main/java/com/thealgorithms/ciphers/Autokey.java @@ -9,7 +9,7 @@ * * @author bennybebo */ -public class AutokeyCipher { +public class Autokey { // Encrypts the plaintext using the Autokey cipher public String encrypt(String plaintext, String keyword) { From af89ffd8d88c92f4d3a98646adf1ddac152b13a6 Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 14:44:44 -0400 Subject: [PATCH 5/8] Fixed Autokey naming issue in AutokeyTest.java --- src/test/java/com/thealgorithms/ciphers/AutokeyTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java b/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java index 5d69a483d8c7..c9874840a03e 100644 --- a/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java +++ b/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java @@ -6,7 +6,7 @@ class AutokeyCipherTest { - AutokeyCipher autokeyCipher = new AutokeyCipher(); + Autokey autokeyCipher = new Autokey(); @Test void autokeyEncryptTest() { From 5e863070d511fe5f07d6a5ab0f641f1d401dd5cf Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 15:01:22 -0400 Subject: [PATCH 6/8] Fixed AutokeyTest.java to not expect spaces in output --- src/test/java/com/thealgorithms/ciphers/AutokeyTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java b/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java index c9874840a03e..52ecff7cdeee 100644 --- a/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java +++ b/src/test/java/com/thealgorithms/ciphers/AutokeyTest.java @@ -18,19 +18,19 @@ void autokeyEncryptTest() { String cipherText = autokeyCipher.encrypt(plaintext, keyword); // then - assertEquals("QKUC YT DVZN", cipherText); + assertEquals("CYIXNFHEPN", cipherText); } @Test void autokeyDecryptTest() { // given - String ciphertext = "QKUC YT DVZN"; + String ciphertext = "CYIX NF HEPN"; String keyword = "QUEEN"; // when String plainText = autokeyCipher.decrypt(ciphertext, keyword); // then - assertEquals("MEET AT DAWN", plainText); + assertEquals("MEETATDAWN", plainText); } } From 82bea9f0e47a5848c2a58f1f162e9f59df340881 Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 15:06:52 -0400 Subject: [PATCH 7/8] Removed unnecessary parentheses --- src/main/java/com/thealgorithms/ciphers/Autokey.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/ciphers/Autokey.java b/src/main/java/com/thealgorithms/ciphers/Autokey.java index b78990aaff5c..46c066bd3303 100644 --- a/src/main/java/com/thealgorithms/ciphers/Autokey.java +++ b/src/main/java/com/thealgorithms/ciphers/Autokey.java @@ -25,7 +25,7 @@ public String encrypt(String plaintext, String keyword) { char plainChar = plaintext.charAt(i); char keyChar = extendedKey.charAt(i); - int encryptedChar = ((plainChar - 'A') + (keyChar - 'A')) % 26 + 'A'; + int encryptedChar = (plainChar - 'A' + keyChar - 'A') % 26 + 'A'; ciphertext.append((char) encryptedChar); } @@ -44,7 +44,7 @@ public String decrypt(String ciphertext, String keyword) { char cipherChar = ciphertext.charAt(i); char keyChar = extendedKey.charAt(i); - int decryptedChar = ((cipherChar - 'A') - (keyChar - 'A') + 26) % 26 + 'A'; + int decryptedChar = (cipherChar - 'A' - keyChar - 'A' + 26) % 26 + 'A'; plaintext.append((char) decryptedChar); extendedKey.append((char) decryptedChar); // Extend key with each decrypted char From a8f7de890c0bb74ae6494342f375fe9001b96dd7 Mon Sep 17 00:00:00 2001 From: bennybebo Date: Fri, 4 Oct 2024 15:10:47 -0400 Subject: [PATCH 8/8] fix --- src/main/java/com/thealgorithms/ciphers/Autokey.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/ciphers/Autokey.java b/src/main/java/com/thealgorithms/ciphers/Autokey.java index 46c066bd3303..bb67f512accf 100644 --- a/src/main/java/com/thealgorithms/ciphers/Autokey.java +++ b/src/main/java/com/thealgorithms/ciphers/Autokey.java @@ -44,7 +44,7 @@ public String decrypt(String ciphertext, String keyword) { char cipherChar = ciphertext.charAt(i); char keyChar = extendedKey.charAt(i); - int decryptedChar = (cipherChar - 'A' - keyChar - 'A' + 26) % 26 + 'A'; + int decryptedChar = (cipherChar - 'A' - (keyChar - 'A') + 26) % 26 + 'A'; plaintext.append((char) decryptedChar); extendedKey.append((char) decryptedChar); // Extend key with each decrypted char