Skip to content

Commit 8426bed

Browse files
authored
Merge pull request TheAlgorithms#1307 from SageTendo/Development
CaesarBruteForce
2 parents 4690efb + 2567859 commit 8426bed

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.ciphers;
2+
3+
public class CaesarBruteForce {
4+
5+
/**
6+
* Recursively Brute forces a parsed encrypted text, trying out all shifting keys from 1-26, printing out all decryption attempts
7+
* @param message (String) The encrypted text.
8+
* @param Key (int) The key used to decrypt the encrypted text and is increment upon a recursive call.
9+
* @return (String) Concatenated string of all decryption attempts (For unit testing purposes).
10+
*/
11+
public String decrypt(String message, int Key) {
12+
final String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
13+
if (Key > 26){ System.out.println(); return null; }
14+
15+
StringBuilder plainText = new StringBuilder();
16+
for (char character : message.toUpperCase().toCharArray()) {
17+
int index = LETTERS.indexOf(character);
18+
19+
if (index != -1) {
20+
index -= Key;
21+
//Wrap around index value range[1-26]
22+
if (index < 0) { index += LETTERS.length(); }
23+
plainText.append(LETTERS.toCharArray()[index]);
24+
} else { plainText.append(character); }
25+
}
26+
System.out.println(String.format("Current Decryption Key %d : %s", Key, plainText));
27+
return plainText.append(decrypt(message, Key+1)).toString();
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.ciphers;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
class CaesarBruteForceTest {
7+
@Test
8+
void testCaesarBruteForce() {
9+
CaesarBruteForce cipher = new CaesarBruteForce();
10+
11+
Assertions.assertSame(true, cipher.decrypt("TKFK", 1).contains("JAVA"));
12+
Assertions.assertSame(true, cipher.decrypt("QHCH", 1).contains("JAVA"));
13+
Assertions.assertSame(true, cipher.decrypt("LUHREIU", 1).contains("VERBOSE"));
14+
Assertions.assertSame(true, cipher.decrypt("Mkockb", 1).contains("CAESAR"));
15+
Assertions.assertSame(true, cipher.decrypt("olssv", 1).contains("HELLO"));
16+
Assertions.assertSame(true, cipher.decrypt("Zvksxdohd", 1).contains("PLAINTEXT"));
17+
Assertions.assertSame(true, cipher.decrypt("XGVKRIM", 1).contains("ENCRYPT"));
18+
Assertions.assertSame(true, cipher.decrypt("OZRRVNQC123", 1).contains("PASSWORD123"));
19+
Assertions.assertSame(true, cipher.decrypt("GEQDZMYQ", 1).contains("USERNAME"));
20+
Assertions.assertSame(true, cipher.decrypt("IKHMXVMXW", 1).contains("PROTECTED"));
21+
Assertions.assertSame(true, cipher.decrypt("ZPSRC-DMPACB", 1).contains("BRUTE-FORCED"));
22+
Assertions.assertSame(true, cipher.decrypt("VCTKJ!", 1).contains("PWNED!"));
23+
Assertions.assertSame(true, cipher.decrypt("JKLMNOP", 1).contains("ABCDEFG"));
24+
Assertions.assertSame(true, cipher.decrypt("QFMDHCUFODVWQ", 1).contains("CRYPTOGRAPHIC"));
25+
Assertions.assertSame(true, cipher.decrypt("Dmbncdc", 1).contains("ENCODED"));
26+
}
27+
}

0 commit comments

Comments
 (0)