未验证 提交 8426bedf 编写于 作者: Stepfen Shawn's avatar Stepfen Shawn 提交者: GitHub

Merge pull request #1307 from SageTendo/Development

CaesarBruteForce
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();
}
}
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"));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册