Skip to content

Commit 0600370

Browse files
committed
Fixed Clang formatting issue
1 parent 7167721 commit 0600370

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.thealgorithms.ciphers;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
63
/**
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
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
107
* doesn’t rely solely on a repeated key.
118
* https://en.wikipedia.org/wiki/Autokey_cipher
12-
*
9+
*
1310
* @author bennybebo
1411
*/
1512
public class AutokeyCipher {
@@ -18,41 +15,41 @@ public class AutokeyCipher {
1815
public String encrypt(String plaintext, String keyword) {
1916
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
2017
keyword = keyword.toUpperCase();
21-
18+
2219
StringBuilder extendedKey = new StringBuilder(keyword);
2320
extendedKey.append(plaintext); // Extend key with plaintext
24-
21+
2522
StringBuilder ciphertext = new StringBuilder();
26-
23+
2724
for (int i = 0; i < plaintext.length(); i++) {
2825
char plainChar = plaintext.charAt(i);
2926
char keyChar = extendedKey.charAt(i);
30-
27+
3128
int encryptedChar = ((plainChar - 'A') + (keyChar - 'A')) % 26 + 'A';
3229
ciphertext.append((char) encryptedChar);
3330
}
34-
31+
3532
return ciphertext.toString();
3633
}
3734

3835
// Decrypts the ciphertext using the Autokey cipher
3936
public String decrypt(String ciphertext, String keyword) {
4037
ciphertext = ciphertext.toUpperCase().replaceAll("[^A-Z]", ""); // Sanitize input
4138
keyword = keyword.toUpperCase();
42-
39+
4340
StringBuilder plaintext = new StringBuilder();
4441
StringBuilder extendedKey = new StringBuilder(keyword);
45-
42+
4643
for (int i = 0; i < ciphertext.length(); i++) {
4744
char cipherChar = ciphertext.charAt(i);
4845
char keyChar = extendedKey.charAt(i);
49-
46+
5047
int decryptedChar = ((cipherChar - 'A') - (keyChar - 'A') + 26) % 26 + 'A';
5148
plaintext.append((char) decryptedChar);
52-
49+
5350
extendedKey.append((char) decryptedChar); // Extend key with each decrypted char
5451
}
55-
52+
5653
return plaintext.toString();
5754
}
5855
}

0 commit comments

Comments
 (0)