From 2567859e3255e42654506a6496bf9cff821d40ae Mon Sep 17 00:00:00 2001 From: SageTendo Date: Sat, 9 May 2020 00:31:51 +0200 Subject: [PATCH] CaesarBruteForce --- .../java/com/ciphers/CaesarBruteForce.java | 29 +++++++++++++++++++ .../com/ciphers/CaesarBruteForceTest.java | 27 +++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/com/ciphers/CaesarBruteForce.java create mode 100644 src/test/java/com/ciphers/CaesarBruteForceTest.java diff --git a/src/main/java/com/ciphers/CaesarBruteForce.java b/src/main/java/com/ciphers/CaesarBruteForce.java new file mode 100644 index 00000000..ab60fec0 --- /dev/null +++ b/src/main/java/com/ciphers/CaesarBruteForce.java @@ -0,0 +1,29 @@ +package com.ciphers; + +public class CaesarBruteForce { + + /** + * Recursively Brute forces a parsed encrypted text, trying out all shifting keys from 1-26, printing out all decryption attempts + * @param message (String) The encrypted text. + * @param Key (int) The key used to decrypt the encrypted text and is increment upon a recursive call. + * @return (String) Concatenated string of all decryption attempts (For unit testing purposes). + */ + public String decrypt(String message, int Key) { + final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + if (Key > 26){ System.out.println(); return null; } + + StringBuilder plainText = new StringBuilder(); + for (char character : message.toUpperCase().toCharArray()) { + int index = LETTERS.indexOf(character); + + if (index != -1) { + index -= Key; + //Wrap around index value range[1-26] + if (index < 0) { index += LETTERS.length(); } + plainText.append(LETTERS.toCharArray()[index]); + } else { plainText.append(character); } + } + System.out.println(String.format("Current Decryption Key %d : %s", Key, plainText)); + return plainText.append(decrypt(message, Key+1)).toString(); + } +} diff --git a/src/test/java/com/ciphers/CaesarBruteForceTest.java b/src/test/java/com/ciphers/CaesarBruteForceTest.java new file mode 100644 index 00000000..ddaff9a9 --- /dev/null +++ b/src/test/java/com/ciphers/CaesarBruteForceTest.java @@ -0,0 +1,27 @@ +package com.ciphers; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class CaesarBruteForceTest { + @Test + void testCaesarBruteForce() { + CaesarBruteForce cipher = new CaesarBruteForce(); + + Assertions.assertSame(true, cipher.decrypt("TKFK", 1).contains("JAVA")); + Assertions.assertSame(true, cipher.decrypt("QHCH", 1).contains("JAVA")); + Assertions.assertSame(true, cipher.decrypt("LUHREIU", 1).contains("VERBOSE")); + Assertions.assertSame(true, cipher.decrypt("Mkockb", 1).contains("CAESAR")); + Assertions.assertSame(true, cipher.decrypt("olssv", 1).contains("HELLO")); + Assertions.assertSame(true, cipher.decrypt("Zvksxdohd", 1).contains("PLAINTEXT")); + Assertions.assertSame(true, cipher.decrypt("XGVKRIM", 1).contains("ENCRYPT")); + Assertions.assertSame(true, cipher.decrypt("OZRRVNQC123", 1).contains("PASSWORD123")); + Assertions.assertSame(true, cipher.decrypt("GEQDZMYQ", 1).contains("USERNAME")); + Assertions.assertSame(true, cipher.decrypt("IKHMXVMXW", 1).contains("PROTECTED")); + Assertions.assertSame(true, cipher.decrypt("ZPSRC-DMPACB", 1).contains("BRUTE-FORCED")); + Assertions.assertSame(true, cipher.decrypt("VCTKJ!", 1).contains("PWNED!")); + Assertions.assertSame(true, cipher.decrypt("JKLMNOP", 1).contains("ABCDEFG")); + Assertions.assertSame(true, cipher.decrypt("QFMDHCUFODVWQ", 1).contains("CRYPTOGRAPHIC")); + Assertions.assertSame(true, cipher.decrypt("Dmbncdc", 1).contains("ENCODED")); + } +}