11package com .thealgorithms .ciphers ;
22
3- import java .util .Scanner ;
4-
53/**
64 * A Java implementation of Caesar Cipher. /It is a type of substitution cipher
75 * in which each letter in the plaintext is replaced by a letter some fixed
@@ -18,7 +16,7 @@ public class Caesar {
1816 *
1917 * @return Encrypted message
2018 */
21- public static String encode (String message , int shift ) {
19+ public String encode (String message , int shift ) {
2220 StringBuilder encoded = new StringBuilder ();
2321
2422 shift %= 26 ;
@@ -29,10 +27,10 @@ public static String encode(String message, int shift) {
2927 // is in-order latin alphabet
3028 char current = message .charAt (i ); // Java law : char + int = char
3129
32- if (IsCapitalLatinLetter (current )) {
30+ if (isCapitalLatinLetter (current )) {
3331 current += shift ;
3432 encoded .append ((char ) (current > 'Z' ? current - 26 : current )); // 26 = number of latin letters
35- } else if (IsSmallLatinLetter (current )) {
33+ } else if (isSmallLatinLetter (current )) {
3634 current += shift ;
3735 encoded .append ((char ) (current > 'z' ? current - 26 : current )); // 26 = number of latin letters
3836 } else {
@@ -48,18 +46,18 @@ public static String encode(String message, int shift) {
4846 *
4947 * @return message
5048 */
51- public static String decode (String encryptedMessage , int shift ) {
49+ public String decode (String encryptedMessage , int shift ) {
5250 StringBuilder decoded = new StringBuilder ();
5351
5452 shift %= 26 ;
5553
5654 final int length = encryptedMessage .length ();
5755 for (int i = 0 ; i < length ; i ++) {
5856 char current = encryptedMessage .charAt (i );
59- if (IsCapitalLatinLetter (current )) {
57+ if (isCapitalLatinLetter (current )) {
6058 current -= shift ;
6159 decoded .append ((char ) (current < 'A' ? current + 26 : current )); // 26 = number of latin letters
62- } else if (IsSmallLatinLetter (current )) {
60+ } else if (isSmallLatinLetter (current )) {
6361 current -= shift ;
6462 decoded .append ((char ) (current < 'a' ? current + 26 : current )); // 26 = number of latin letters
6563 } else {
@@ -72,69 +70,26 @@ public static String decode(String encryptedMessage, int shift) {
7270 /**
7371 * @return true if character is capital Latin letter or false for others
7472 */
75- private static boolean IsCapitalLatinLetter (char c ) {
73+ private static boolean isCapitalLatinLetter (char c ) {
7674 return c >= 'A' && c <= 'Z' ;
7775 }
7876
7977 /**
8078 * @return true if character is small Latin letter or false for others
8179 */
82- private static boolean IsSmallLatinLetter (char c ) {
80+ private static boolean isSmallLatinLetter (char c ) {
8381 return c >= 'a' && c <= 'z' ;
8482 }
8583
8684 /**
8785 * @return string array which contains all the possible decoded combination.
8886 */
89- public static String [] bruteforce (String encryptedMessage ) {
87+ public String [] bruteforce (String encryptedMessage ) {
9088 String [] listOfAllTheAnswers = new String [27 ];
9189 for (int i = 0 ; i <= 26 ; i ++) {
9290 listOfAllTheAnswers [i ] = decode (encryptedMessage , i );
9391 }
9492
9593 return listOfAllTheAnswers ;
9694 }
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- }
14095}
0 commit comments