CaesarBruteForce
This commit is contained in:
parent
4690efbea4
commit
2567859e32
29
src/main/java/com/ciphers/CaesarBruteForce.java
Normal file
29
src/main/java/com/ciphers/CaesarBruteForce.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
}
|
27
src/test/java/com/ciphers/CaesarBruteForceTest.java
Normal file
27
src/test/java/com/ciphers/CaesarBruteForceTest.java
Normal file
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user