Skip to content

Added BaconianCipher.java and BaconianCipherTest.java #5932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions src/main/java/com/thealgorithms/ciphers/BaconianCipher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.thealgorithms.ciphers;

import java.util.HashMap;
import java.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
*/
public class BaconianCipher {

private static final Map<Character, String> BACONIAN_MAP = new HashMap<>();
private static final Map<String, Character> REVERSE_BACONIAN_MAP = new HashMap<>();

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"};
char letter = 'A';
for (String code : 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.
*/
public String encrypt(String plaintext) {
StringBuilder ciphertext = new StringBuilder();
plaintext = plaintext.toUpperCase().replaceAll("[^A-Z]", ""); // Remove non-letter characters

for (char letter : plaintext.toCharArray()) {
ciphertext.append(BACONIAN_MAP.get(letter));
}

return ciphertext.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.
*/
public String decrypt(String ciphertext) {
StringBuilder plaintext = new StringBuilder();

for (int i = 0; i < ciphertext.length(); i += 5) {
String code = ciphertext.substring(i, i + 5);
if (REVERSE_BACONIAN_MAP.containsKey(code)) {
plaintext.append(REVERSE_BACONIAN_MAP.get(code));
} else {
throw new IllegalArgumentException("Invalid Baconian code: " + code);
}
}

return plaintext.toString();
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/thealgorithms/ciphers/BaconianCipherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.thealgorithms.ciphers;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class BaconianCipherTest {

BaconianCipher baconianCipher = new BaconianCipher();

@Test
void baconianCipherEncryptTest() {
// given
String plaintext = "MEET AT DAWN";

// when
String cipherText = baconianCipher.encrypt(plaintext);

// then
assertEquals("ABBAAAABAAAABAABAABBAAAAABAABBAAABBAAAAABABBAABBAB", cipherText);
}

@Test
void baconianCipherDecryptTest() {
// given
String ciphertext = "ABBAAAABAAAABAABAABBAAAAABAABBAAABBAAAAABABBAABBAB";

// when
String plainText = baconianCipher.decrypt(ciphertext);

// then
assertEquals("MEETATDAWN", plainText);
}
}