@@ -91,28 +91,50 @@ private static boolean IsCapitalLatinLetter(char c) {
91
91
private static boolean IsSmallLatinLetter (char c ) {
92
92
return c >= 'a' && c <= 'z' ;
93
93
}
94
+ /**
95
+ * @return string array which contains all the possible decoded combination.
96
+ */
97
+ public static String [] bruteforce (String encryptedMessage ) {
98
+ String [] listOfAllTheAnswers = new String [27 ];
99
+ for (int i =0 ; i <=26 ; i ++) {
100
+ listOfAllTheAnswers [i ] = decode (encryptedMessage , i );
101
+ }
102
+
103
+ return listOfAllTheAnswers ;
104
+ }
94
105
95
106
public static void main (String [] args ) {
96
107
Scanner input = new Scanner (System .in );
108
+ int shift = 0 ;
97
109
System .out .println ("Please enter the message (Latin Alphabet)" );
98
110
String message = input .nextLine ();
99
111
System .out .println (message );
100
- System .out .println ("Please enter the shift number" );
101
- int shift = input .nextInt () % 26 ;
102
- System .out .println ("(E)ncode or (D)ecode ?" );
112
+ System .out .println ("(E)ncode or (D)ecode or (B)ruteforce?" );
103
113
char choice = input .next ().charAt (0 );
104
114
switch (choice ) {
105
115
case 'E' :
106
116
case 'e' :
117
+ System .out .println ("Please enter the shift number" );
118
+ shift = input .nextInt () % 26 ;
107
119
System .out .println (
108
120
"ENCODED MESSAGE IS \n " + encode (message , shift )); // send our function to handle
109
121
break ;
110
122
case 'D' :
111
123
case 'd' :
124
+ System .out .println ("Please enter the shift number" );
125
+ shift = input .nextInt () % 26 ;
112
126
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 (int i =0 ; i <=26 ; i ++) {
132
+ System .out .println ("FOR SHIFT " + String .valueOf (i ) + " decoded message is " + listOfAllTheAnswers [i ]);
133
+ }
113
134
default :
114
135
System .out .println ("default case" );
115
136
}
137
+
116
138
input .close ();
117
139
}
118
140
}
0 commit comments