Skip to content

Commit 87030af

Browse files
authored
Add BaconianCipher (#5932)
1 parent c56d282 commit 87030af

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.thealgorithms.ciphers;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* The Baconian Cipher is a substitution cipher where each letter is represented
8+
* by a group of five binary digits (A's and B's). It can also be used to hide
9+
* messages within other texts, making it a simple form of steganography.
10+
* https://en.wikipedia.org/wiki/Bacon%27s_cipher
11+
*
12+
* @author Bennybebo
13+
*/
14+
public class BaconianCipher {
15+
16+
private static final Map<Character, String> BACONIAN_MAP = new HashMap<>();
17+
private static final Map<String, Character> REVERSE_BACONIAN_MAP = new HashMap<>();
18+
19+
static {
20+
// Initialize the Baconian cipher mappings
21+
String[] baconianAlphabet = {"AAAAA", "AAAAB", "AAABA", "AAABB", "AABAA", "AABAB", "AABBA", "AABBB", "ABAAA", "ABAAB", "ABABA", "ABABB", "ABBAA", "ABBAB", "ABBBA", "ABBBB", "BAAAA", "BAAAB", "BAABA", "BAABB", "BABAA", "BABAB", "BABBA", "BABBB", "BBAAA", "BBAAB"};
22+
char letter = 'A';
23+
for (String code : baconianAlphabet) {
24+
BACONIAN_MAP.put(letter, code);
25+
REVERSE_BACONIAN_MAP.put(code, letter);
26+
letter++;
27+
}
28+
29+
// Handle I/J as the same letter
30+
BACONIAN_MAP.put('I', BACONIAN_MAP.get('J'));
31+
REVERSE_BACONIAN_MAP.put(BACONIAN_MAP.get('I'), 'I');
32+
}
33+
34+
/**
35+
* Encrypts the given plaintext using the Baconian cipher.
36+
*
37+
* @param plaintext The plaintext message to encrypt.
38+
* @return The ciphertext as a binary (A/B) sequence.
39+
*/
40+
public String encrypt(String plaintext) {
41+
StringBuilder ciphertext = new StringBuilder();
42+
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Remove non-letter characters
43+
44+
for (char letter : plaintext.toCharArray()) {
45+
ciphertext.append(BACONIAN_MAP.get(letter));
46+
}
47+
48+
return ciphertext.toString();
49+
}
50+
51+
/**
52+
* Decrypts the given ciphertext encoded in binary (A/B) format using the Baconian cipher.
53+
*
54+
* @param ciphertext The ciphertext to decrypt.
55+
* @return The decrypted plaintext message.
56+
*/
57+
public String decrypt(String ciphertext) {
58+
StringBuilder plaintext = new StringBuilder();
59+
60+
for (int i = 0; i < ciphertext.length(); i += 5) {
61+
String code = ciphertext.substring(i, i + 5);
62+
if (REVERSE_BACONIAN_MAP.containsKey(code)) {
63+
plaintext.append(REVERSE_BACONIAN_MAP.get(code));
64+
} else {
65+
throw new IllegalArgumentException("Invalid Baconian code: " + code);
66+
}
67+
}
68+
69+
return plaintext.toString();
70+
}
71+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 BaconianCipherTest {
8+
9+
BaconianCipher baconianCipher = new BaconianCipher();
10+
11+
@Test
12+
void baconianCipherEncryptTest() {
13+
// given
14+
String plaintext = "MEET AT DAWN";
15+
16+
// when
17+
String cipherText = baconianCipher.encrypt(plaintext);
18+
19+
// then
20+
assertEquals("ABBAAAABAAAABAABAABBAAAAABAABBAAABBAAAAABABBAABBAB", cipherText);
21+
}
22+
23+
@Test
24+
void baconianCipherDecryptTest() {
25+
// given
26+
String ciphertext = "ABBAAAABAAAABAABAABBAAAAABAABBAAABBAAAAABABBAABBAB";
27+
28+
// when
29+
String plainText = baconianCipher.decrypt(ciphertext);
30+
31+
// then
32+
assertEquals("MEETATDAWN", plainText);
33+
}
34+
}

0 commit comments

Comments
 (0)