Skip to content

Commit defd98e

Browse files
committed
Added Autokey cipher and AutokeyTest.java
1 parent 042d458 commit defd98e

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* The Autokey Cipher is an interesting and historically significant encryption method,
8+
* as it improves upon the classic Vigenère Cipher by using the plaintext itself to
9+
* extend the key. This makes it harder to break using frequency analysis, as it
10+
* doesn’t rely solely on a repeated key.
11+
*
12+
* @author bennybebo
13+
*/
14+
public class AutokeyCipher {
15+
16+
// Encrypts the plaintext using the Autokey cipher
17+
public String encrypt(String plaintext, String keyword) {
18+
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
19+
keyword = keyword.toUpperCase();
20+
21+
StringBuilder extendedKey = new StringBuilder(keyword);
22+
extendedKey.append(plaintext); // Extend key with plaintext
23+
24+
StringBuilder ciphertext = new StringBuilder();
25+
26+
for (int i = 0; i < plaintext.length(); i++) {
27+
char plainChar = plaintext.charAt(i);
28+
char keyChar = extendedKey.charAt(i);
29+
30+
int encryptedChar = ((plainChar - 'A') + (keyChar - 'A')) % 26 + 'A';
31+
ciphertext.append((char) encryptedChar);
32+
}
33+
34+
return ciphertext.toString();
35+
}
36+
37+
// Decrypts the ciphertext using the Autokey cipher
38+
public String decrypt(String ciphertext, String keyword) {
39+
ciphertext = ciphertext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
40+
keyword = keyword.toUpperCase();
41+
42+
StringBuilder plaintext = new StringBuilder();
43+
StringBuilder extendedKey = new StringBuilder(keyword);
44+
45+
for (int i = 0; i < ciphertext.length(); i++) {
46+
char cipherChar = ciphertext.charAt(i);
47+
char keyChar = extendedKey.charAt(i);
48+
49+
int decryptedChar = ((cipherChar - 'A') - (keyChar - 'A') + 26) % 26 + 'A';
50+
plaintext.append((char) decryptedChar);
51+
52+
extendedKey.append((char) decryptedChar); // Extend key with each decrypted char
53+
}
54+
55+
return plaintext.toString();
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class AutokeyCipherTest {
8+
9+
AutokeyCipher autokeyCipher = new AutokeyCipher();
10+
11+
@Test
12+
void autokeyEncryptTest() {
13+
// given
14+
String plaintext = "MEET AT DAWN";
15+
String keyword = "QUEEN";
16+
17+
// when
18+
String cipherText = autokeyCipher.encrypt(plaintext, keyword);
19+
20+
// then
21+
assertEquals("QKUC YT DVZN", cipherText);
22+
}
23+
24+
@Test
25+
void autokeyDecryptTest() {
26+
// given
27+
String ciphertext = "QKUC YT DVZN";
28+
String keyword = "QUEEN";
29+
30+
// when
31+
String plainText = autokeyCipher.decrypt(ciphertext, keyword);
32+
33+
// then
34+
assertEquals("MEET AT DAWN", plainText);
35+
}
36+
}

0 commit comments

Comments
 (0)