Repository: TheAlgorithms/Java
Pull request #5932 (2024-10-21 18:00:12)

Added BaconianCipher.java and BaconianCipherTest.java
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5932?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `91.66667%` with `2 lines` in your changes missing coverage. Please review.
> Please [upload](https://docs.codecov.com/docs/codecov-uploader) report for BASE (`master@676d451`). [Learn more](https://docs.codecov.io/docs/error-reference?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#section-missing-base-commit) about missing BASE report.
> Report is 121 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5932?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...java/com/thealgorithms/ciphers/BaconianCipher.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5932?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fciphers%2FBaconianCipher.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9jaXBoZXJzL0JhY29uaWFuQ2lwaGVyLmphdmE=) | 91.66% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5932?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff            @@
##             master    #5932   +/-   ##
=========================================
  Coverage          ?   66.83%           
  Complexity        ?     4508           
=========================================
  Files             ?      612           
  Lines             ?    16982           
  Branches          ?     3279           
=========================================
  Hits              ?    11350           
  Misses            ?     5183           
  Partials          ?      449           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5932?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/src/main/java/com/thealgorithms/ciphers/BaconianCipher.java b/src/main/java/com/thealgorithms/ciphers/BaconianCipher.java
new file mode 100644
index 00000000..16dfd6e6
--- /dev/null
+++ b/src/main/java/com/thealgorithms/ciphers/BaconianCipher.java
@@ -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();
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/ciphers/BaconianCipherTest.java b/src/test/java/com/thealgorithms/ciphers/BaconianCipherTest.java
new file mode 100644
index 00000000..bb1ae5a7
--- /dev/null
+++ b/src/test/java/com/thealgorithms/ciphers/BaconianCipherTest.java
@@ -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);
+    }
+}