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

Commit549a27a

Browse files
Add bruteforce to the caesar cipher (TheAlgorithms#2887)
1 parent32cdf02 commit549a27a

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

‎src/main/java/com/thealgorithms/ciphers/Caesar.java‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,28 +91,50 @@ private static boolean IsCapitalLatinLetter(char c) {
9191
privatestaticbooleanIsSmallLatinLetter(charc) {
9292
returnc >='a' &&c <='z';
9393
}
94+
/**
95+
* @return string array which contains all the possible decoded combination.
96+
*/
97+
publicstaticString[]bruteforce(StringencryptedMessage) {
98+
String[]listOfAllTheAnswers =newString[27];
99+
for (inti=0;i<=26;i++) {
100+
listOfAllTheAnswers[i] =decode(encryptedMessage,i);
101+
}
102+
103+
returnlistOfAllTheAnswers;
104+
}
94105

95106
publicstaticvoidmain(String[]args) {
96107
Scannerinput =newScanner(System.in);
108+
intshift =0;
97109
System.out.println("Please enter the message (Latin Alphabet)");
98110
Stringmessage =input.nextLine();
99111
System.out.println(message);
100-
System.out.println("Please enter the shift number");
101-
intshift =input.nextInt() %26;
102-
System.out.println("(E)ncode or (D)ecode ?");
112+
System.out.println("(E)ncode or (D)ecode or (B)ruteforce?");
103113
charchoice =input.next().charAt(0);
104114
switch (choice) {
105115
case'E':
106116
case'e':
117+
System.out.println("Please enter the shift number");
118+
shift =input.nextInt() %26;
107119
System.out.println(
108120
"ENCODED MESSAGE IS\n" +encode(message,shift));// send our function to handle
109121
break;
110122
case'D':
111123
case'd':
124+
System.out.println("Please enter the shift number");
125+
shift =input.nextInt() %26;
112126
System.out.println("DECODED MESSAGE IS\n" +decode(message,shift));
127+
break;
128+
case'B':
129+
case'b':
130+
String[]listOfAllTheAnswers =bruteforce(message);
131+
for (inti =0;i<=26;i++) {
132+
System.out.println("FOR SHIFT " +String.valueOf(i) +" decoded message is " +listOfAllTheAnswers[i]);
133+
}
113134
default:
114135
System.out.println("default case");
115136
}
137+
116138
input.close();
117139
}
118140
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp