Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8426bed

Browse files
authored
Merge pull requestTheAlgorithms#1307 from SageTendo/Development
CaesarBruteForce
2 parents4690efb +2567859 commit8426bed

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+
packagecom.ciphers;
2+
3+
publicclassCaesarBruteForce {
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+
publicStringdecrypt(Stringmessage,intKey) {
12+
finalStringLETTERS ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
13+
if (Key >26){System.out.println();returnnull; }
14+
15+
StringBuilderplainText =newStringBuilder();
16+
for (charcharacter :message.toUpperCase().toCharArray()) {
17+
intindex =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+
returnplainText.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+
packagecom.ciphers;
2+
3+
importorg.junit.jupiter.api.Assertions;
4+
importorg.junit.jupiter.api.Test;
5+
6+
classCaesarBruteForceTest {
7+
@Test
8+
voidtestCaesarBruteForce() {
9+
CaesarBruteForcecipher =newCaesarBruteForce();
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)

[8]ページ先頭

©2009-2025 Movatter.jp