1
1
package com .thealgorithms .ciphers ;
2
2
3
- import java .util .Scanner ;
4
-
5
3
/**
6
4
* A Java implementation of Caesar Cipher. /It is a type of substitution cipher
7
5
* in which each letter in the plaintext is replaced by a letter some fixed
@@ -18,7 +16,7 @@ public class Caesar {
18
16
*
19
17
* @return Encrypted message
20
18
*/
21
- public static String encode (String message , int shift ) {
19
+ public String encode (String message , int shift ) {
22
20
StringBuilder encoded = new StringBuilder ();
23
21
24
22
shift %= 26 ;
@@ -29,10 +27,10 @@ public static String encode(String message, int shift) {
29
27
// is in-order latin alphabet
30
28
char current = message .charAt (i ); // Java law : char + int = char
31
29
32
- if (IsCapitalLatinLetter (current )) {
30
+ if (isCapitalLatinLetter (current )) {
33
31
current += shift ;
34
32
encoded .append ((char ) (current > 'Z' ? current - 26 : current )); // 26 = number of latin letters
35
- } else if (IsSmallLatinLetter (current )) {
33
+ } else if (isSmallLatinLetter (current )) {
36
34
current += shift ;
37
35
encoded .append ((char ) (current > 'z' ? current - 26 : current )); // 26 = number of latin letters
38
36
} else {
@@ -48,18 +46,18 @@ public static String encode(String message, int shift) {
48
46
*
49
47
* @return message
50
48
*/
51
- public static String decode (String encryptedMessage , int shift ) {
49
+ public String decode (String encryptedMessage , int shift ) {
52
50
StringBuilder decoded = new StringBuilder ();
53
51
54
52
shift %= 26 ;
55
53
56
54
final int length = encryptedMessage .length ();
57
55
for (int i = 0 ; i < length ; i ++) {
58
56
char current = encryptedMessage .charAt (i );
59
- if (IsCapitalLatinLetter (current )) {
57
+ if (isCapitalLatinLetter (current )) {
60
58
current -= shift ;
61
59
decoded .append ((char ) (current < 'A' ? current + 26 : current )); // 26 = number of latin letters
62
- } else if (IsSmallLatinLetter (current )) {
60
+ } else if (isSmallLatinLetter (current )) {
63
61
current -= shift ;
64
62
decoded .append ((char ) (current < 'a' ? current + 26 : current )); // 26 = number of latin letters
65
63
} else {
@@ -72,69 +70,26 @@ public static String decode(String encryptedMessage, int shift) {
72
70
/**
73
71
* @return true if character is capital Latin letter or false for others
74
72
*/
75
- private static boolean IsCapitalLatinLetter (char c ) {
73
+ private static boolean isCapitalLatinLetter (char c ) {
76
74
return c >= 'A' && c <= 'Z' ;
77
75
}
78
76
79
77
/**
80
78
* @return true if character is small Latin letter or false for others
81
79
*/
82
- private static boolean IsSmallLatinLetter (char c ) {
80
+ private static boolean isSmallLatinLetter (char c ) {
83
81
return c >= 'a' && c <= 'z' ;
84
82
}
85
83
86
84
/**
87
85
* @return string array which contains all the possible decoded combination.
88
86
*/
89
- public static String [] bruteforce (String encryptedMessage ) {
87
+ public String [] bruteforce (String encryptedMessage ) {
90
88
String [] listOfAllTheAnswers = new String [27 ];
91
89
for (int i = 0 ; i <= 26 ; i ++) {
92
90
listOfAllTheAnswers [i ] = decode (encryptedMessage , i );
93
91
}
94
92
95
93
return listOfAllTheAnswers ;
96
94
}
97
-
98
- public static void main (String [] args ) {
99
- Scanner input = new Scanner (System .in );
100
- int shift = 0 ;
101
- System .out .println ("Please enter the message (Latin Alphabet)" );
102
- String message = input .nextLine ();
103
- System .out .println (message );
104
- System .out .println ("(E)ncode or (D)ecode or (B)ruteforce?" );
105
- char choice = input .next ().charAt (0 );
106
- switch (choice ) {
107
- case 'E' :
108
- case 'e' :
109
- System .out .println ("Please enter the shift number" );
110
- shift = input .nextInt () % 26 ;
111
- System .out .println (
112
- "ENCODED MESSAGE IS \n " + encode (message , shift )
113
- ); // send our function to handle
114
- break ;
115
- case 'D' :
116
- case 'd' :
117
- System .out .println ("Please enter the shift number" );
118
- shift = input .nextInt () % 26 ;
119
- System .out .println (
120
- "DECODED MESSAGE IS \n " + decode (message , shift )
121
- );
122
- break ;
123
- case 'B' :
124
- case 'b' :
125
- String [] listOfAllTheAnswers = bruteforce (message );
126
- for (int i = 0 ; i <= 26 ; i ++) {
127
- System .out .println (
128
- "FOR SHIFT " +
129
- String .valueOf (i ) +
130
- " decoded message is " +
131
- listOfAllTheAnswers [i ]
132
- );
133
- }
134
- default :
135
- System .out .println ("default case" );
136
- }
137
-
138
- input .close ();
139
- }
140
95
}
0 commit comments