Skip to content

Commit d17c92d

Browse files
authored
Merge branch 'master' into master
2 parents 4b19e8f + 07cb6c4 commit d17c92d

File tree

2 files changed

+91
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)