Skip to content

CaesarBruteForce #1307

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 1 commit into from
May 10, 2020
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
29 changes: 29 additions & 0 deletions src/main/java/com/ciphers/CaesarBruteForce.java
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions src/test/java/com/ciphers/CaesarBruteForceTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}