Repository: TheAlgorithms/Java
Pull request #3185 (2022-07-08 21:36:12)

Add Polybius Cipher
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [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.
- [ ] I know that pull requests will not be merged if they fail the automated tests.
- [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [X] All filenames are in all uppercase characters with no spaces or dashes.
- [X] All functions and variable names follow Java naming conventions.
- [] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/ciphers/Polybius.java b/src/main/java/com/thealgorithms/ciphers/Polybius.java
new file mode 100644
index 00000000..a787870d
--- /dev/null
+++ b/src/main/java/com/thealgorithms/ciphers/Polybius.java
@@ -0,0 +1,59 @@
+package com.thealgorithms.ciphers;
+
+/**
+ * A Java implementation of Polybius Cipher
+ * Polybius is a substitution cipher method
+ * It was invented by a greek philosopher that name is Polybius
+ * Letters in alphabet takes place to two dimension table.
+ * Encrypted text is created according to row and column in two dimension table
+ * Decrypted text is generated by looking at the row and column respectively
+ * Additionally, some letters in english alphabet deliberately throws such as U because U is very similar with V
+ *
+ * @author Hikmet ÇAKIR
+ * @since 08-07-2022+03:00
+ */
+public class Polybius {
+
+    private static final char[][] key = {
+          //         0    1    2    3    4
+          /* 0 */  {'A', 'B', 'C', 'D', 'E'},
+          /* 1 */  {'F', 'G', 'H', 'I', 'J'},
+          /* 2 */  {'K', 'L', 'M', 'N', 'O'},
+          /* 3 */  {'P', 'Q', 'R', 'S', 'T'},
+          /* 4 */  {'V', 'W', 'X', 'Y', 'Z'}
+    };
+
+    private static String findLocationByCharacter(final char character) {
+        final StringBuilder location = new StringBuilder();
+        for (int i = 0; i < key.length; i++) {
+            for (int j = 0; j < key[i].length; j++) {
+                if (character == key[i][j]) {
+                    location.append(i).append(j);
+                    break;
+                }
+            }
+        }
+        return location.toString();
+    }
+
+    public static String encrypt(final String plaintext) {
+        final char[] chars = plaintext.toUpperCase().toCharArray();
+        final StringBuilder ciphertext = new StringBuilder();
+        for (char aChar : chars) {
+            String location = findLocationByCharacter(aChar);
+            ciphertext.append(location);
+        }
+        return ciphertext.toString();
+    }
+
+    public static String decrypt(final String ciphertext) {
+        final char[] chars = ciphertext.toCharArray();
+        final StringBuilder plaintext = new StringBuilder();
+        for(int i = 0; i < chars.length; i+=2) {
+            int pozitionX = Character.getNumericValue(chars[i]);
+            int pozitionY = Character.getNumericValue(chars[i + 1]);
+            plaintext.append(key[pozitionX][pozitionY]);
+        }
+        return plaintext.toString();
+    }
+}
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java b/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java
new file mode 100644
index 00000000..c54adab6
--- /dev/null
+++ b/src/test/java/com/thealgorithms/ciphers/PolybiusTest.java
@@ -0,0 +1,45 @@
+package com.thealgorithms.ciphers;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class PolybiusTest {
+
+    @Test
+    void testEncrypt() {
+        // Given
+        String plaintext = "HELLOWORLD";
+
+        // When
+        String actual = Polybius.encrypt(plaintext);
+
+        // Then
+        assertEquals("12042121244124322103", actual);
+    }
+
+    @Test
+    void testDecrypt() {
+        // Given
+        String ciphertext = "12042121244124322103";
+
+        // When
+        String actual = Polybius.decrypt(ciphertext);
+
+        // Then
+        assertEquals("HELLOWORLD", actual);
+    }
+
+    @Test
+    void testIsTextTheSameAfterEncryptionAndDecryption() {
+        // Given
+        String plaintext = "HELLOWORLD";
+
+        // When
+        String encryptedText = Polybius.encrypt(plaintext);
+        String actual = Polybius.decrypt(encryptedText);
+
+        // Then
+        assertEquals(plaintext, actual);
+    }
+}