- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathBaconianCipher.java
71 lines (60 loc) · 2.56 KB
/
BaconianCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
packagecom.thealgorithms.ciphers;
importjava.util.HashMap;
importjava.util.Map;
/**
* The Baconian Cipher is a substitution cipher where each letter is represented
* by a group of five binary digits (A's and B's). It can also be used to hide
* messages within other texts, making it a simple form of steganography.
* https://en.wikipedia.org/wiki/Bacon%27s_cipher
*
* @author Bennybebo
*/
publicclassBaconianCipher {
privatestaticfinalMap<Character, String> BACONIAN_MAP = newHashMap<>();
privatestaticfinalMap<String, Character> REVERSE_BACONIAN_MAP = newHashMap<>();
static {
// Initialize the Baconian cipher mappings
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"};
charletter = 'A';
for (Stringcode : baconianAlphabet) {
BACONIAN_MAP.put(letter, code);
REVERSE_BACONIAN_MAP.put(code, letter);
letter++;
}
// Handle I/J as the same letter
BACONIAN_MAP.put('I', BACONIAN_MAP.get('J'));
REVERSE_BACONIAN_MAP.put(BACONIAN_MAP.get('I'), 'I');
}
/**
* Encrypts the given plaintext using the Baconian cipher.
*
* @param plaintext The plaintext message to encrypt.
* @return The ciphertext as a binary (A/B) sequence.
*/
publicStringencrypt(Stringplaintext) {
StringBuilderciphertext = newStringBuilder();
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Remove non-letter characters
for (charletter : plaintext.toCharArray()) {
ciphertext.append(BACONIAN_MAP.get(letter));
}
returnciphertext.toString();
}
/**
* Decrypts the given ciphertext encoded in binary (A/B) format using the Baconian cipher.
*
* @param ciphertext The ciphertext to decrypt.
* @return The decrypted plaintext message.
*/
publicStringdecrypt(Stringciphertext) {
StringBuilderplaintext = newStringBuilder();
for (inti = 0; i < ciphertext.length(); i += 5) {
Stringcode = ciphertext.substring(i, i + 5);
if (REVERSE_BACONIAN_MAP.containsKey(code)) {
plaintext.append(REVERSE_BACONIAN_MAP.get(code));
} else {
thrownewIllegalArgumentException("Invalid Baconian code: " + code);
}
}
returnplaintext.toString();
}
}