|
| 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 | +} |
0 commit comments