1
- /**
2
- Author : FAHRI YARDIMCI
1
+ package ciphers ;
3
2
4
- A Java implementation of Caesar Cipher.
5
- /It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. /
6
- **/
7
3
import java .util .Scanner ;
4
+
5
+ /**
6
+ *
7
+ * A Java implementation of Caesar Cipher. /It is a type of substitution cipher
8
+ * in which each letter in the plaintext is replaced by a letter some fixed
9
+ * number of positions down the alphabet. /
10
+ *
11
+ * @author FAHRI YARDIMCI
12
+ * @author khalil2535
13
+ */
8
14
public class Caesar {
9
- public static String encode (String message ,int shift )
10
- {
11
- String encoded = "" ;
12
- for (int i = 0 ; i <message .length () ;i ++)
13
- {
14
- int current = message .charAt (i ); //using char to shift characters because ascii is in-order latin alphabet
15
- if (current ==32 )
16
- {
17
- encoded += " " ;
18
- continue ;
19
-
20
- }
21
- else if (current >=65 && current <= 90 )
22
- {
23
- int numAlphabet = message .charAt (i );
24
- if (shift + numAlphabet > 90 )
25
- {
26
- int j = 90 - numAlphabet ;
27
- char nextKey = (char )(65 + (shift - j - 1 ));
28
- encoded += nextKey ;
29
-
30
- }
31
- else
32
- {
33
- char nextKey = (char )(current + shift );
34
- encoded += nextKey ;
35
- }
36
- }
37
- else if (current >=97 && current <= 122 )
38
- {
39
- int numAlphabet = message .charAt (i );
40
- if (shift + numAlphabet > 122 )
41
- {
42
- int j = 122 - numAlphabet ;
43
- char nextKey = (char )(97 + (shift - j - 1 ));
44
- encoded += nextKey ;
45
- }
46
- else
47
- {
48
- char nextKey = (char )(current + shift );
49
- encoded += nextKey ;
50
- }
51
- }
52
- }
53
- return encoded ;
54
- }
55
- public static String decode (String message ,int shift )
56
- {
57
- String decoded = "" ;
58
- for (int i = 0 ; i <message .length () ;i ++)
59
- {
60
- int current = message .charAt (i );
61
- if (current ==32 )
62
- {
63
- decoded += " " ;
64
- continue ;
65
-
66
- }
67
- else if (current >=65 && current <= 90 )
68
- {
69
- int numAlphabet = message .charAt (i );
70
- if (numAlphabet - shift < 65 )
71
- {
72
- int j = numAlphabet - 65 ;
73
- char nextKey = (char )(90 - (shift - j - 1 ));
74
- decoded += nextKey ;
75
-
76
- }
77
- else
78
- {
79
- char nextKey = (char )(current - shift );
80
- decoded += nextKey ;
81
- }
82
- }
83
- else if (current >=97 && current <= 122 )
84
- {
85
- int numAlphabet = message .charAt (i );
86
- if (numAlphabet - shift < 97 )
87
- {
88
- int j = numAlphabet - 97 ;
89
- char nextKey = (char )(122 - (shift - j - 1 ));
90
- decoded += nextKey ;
91
- }
92
- else
93
- {
94
- char nextKey = (char )(current - shift );
95
- decoded += nextKey ;
96
- }
97
- }
98
- }
99
- return decoded ;
100
- }
101
- public static void main (String [] args )
102
- {
103
- Scanner input = new Scanner (System .in );
104
- System .out .println ("Please enter the message (Latin Alphabet)" );
105
- String message = input .nextLine ();
106
- System .out .println (message );
107
- System .out .println ("Please enter the shift number" );
108
- int shift = input .nextInt () % 26 ;
109
- System .out .println ("(E)ncode or (D)ecode ?" );
110
- char choice = input .next ().charAt (0 );
111
- if (choice == 'E' || choice =='e' )
112
- System .out .println ("ENCODED MESSAGE IS \n " + encode (message ,shift )); //send our function to handle
113
- if (choice =='D' || choice =='d' )
114
- System .out .println ("DECODED MESSAGE IS \n " + decode (message ,shift ));
115
- }
116
15
117
- }
16
+ /**
17
+ * Encrypt text by shifting every Latin char by add number shift for ASCII
18
+ * Example : A + 1 -> B
19
+ *
20
+ * @param message
21
+ * @param shift
22
+ * @return Encrypted message
23
+ */
24
+ public static String encode (String message , int shift ) {
25
+ String encoded = "" ;
26
+
27
+ while (shift >= 26 ) { // 26 = number of latin letters
28
+ shift -= 26 ;
29
+ }
30
+
31
+ final int length = message .length ();
32
+ for (int i = 0 ; i < length ; i ++) {
33
+
34
+ // int current = message.charAt(i); //using char to shift characters because ascii is in-order latin alphabet
35
+ char current = message .charAt (i ); // Java law : char + int = char
36
+
37
+ if (current >= 'A' && current <= 'Z' ) {
38
+
39
+ current += shift ;
40
+ encoded += (char ) (current > 'Z' ? current - 26 : current ); // 26 = number of latin letters
41
+
42
+ } else if (current >= 'a' && current <= 'z' ) {
43
+
44
+ current += shift ;
45
+ encoded += (char ) (current > 'z' ? current - 26 : current ); // 26 = number of latin letters
46
+
47
+ } else {
48
+ encoded += current ;
49
+ }
50
+ }
51
+ return encoded ;
52
+ }
53
+
54
+ /**
55
+ * Decrypt message by shifting back every Latin char to previous the ASCII
56
+ * Example : B - 1 -> A
57
+ *
58
+ * @param encryptedMessage
59
+ * @param shift
60
+ * @return message
61
+ */
62
+ public static String decode (String encryptedMessage , int shift ) {
63
+ String decoded = "" ;
64
+
65
+ while (shift >= 26 ) { // 26 = number of latin letters
66
+ shift -= 26 ;
67
+ }
68
+
69
+ final int length = encryptedMessage .length ();
70
+ for (int i = 0 ; i < length ; i ++) {
71
+ char current = encryptedMessage .charAt (i );
72
+ if (current >= 'A' && current <= 'Z' ) {
73
+
74
+ current -= shift ;
75
+ decoded += (char ) (current < 'A' ? current + 26 : current );// 26 = number of latin letters
76
+
77
+ } else if (current >= 'a' && current <= 'z' ) {
78
+
79
+ current -= shift ;
80
+ decoded += (char ) (current < 'a' ? current + 26 : current );// 26 = number of latin letters
81
+
82
+ } else {
83
+ decoded += current ;
84
+ }
85
+ }
86
+ return decoded ;
87
+ }
88
+
89
+ /**
90
+ *
91
+ * @deprecated TODO remove main and make JUnit Testing
92
+ */
93
+ public static void main (String [] args ) {
94
+ Scanner input = new Scanner (System .in );
95
+ System .out .println ("Please enter the message (Latin Alphabet)" );
96
+ String message = input .nextLine ();
97
+ System .out .println (message );
98
+ System .out .println ("Please enter the shift number" );
99
+ int shift = input .nextInt () % 26 ;
100
+ System .out .println ("(E)ncode or (D)ecode ?" );
101
+ char choice = input .next ().charAt (0 );
102
+ if (choice == 'E' || choice == 'e' ) {
103
+ System .out .println ("ENCODED MESSAGE IS \n " + encode (message , shift )); //send our function to handle
104
+ }
105
+ if (choice == 'D' || choice == 'd' ) {
106
+ System .out .println ("DECODED MESSAGE IS \n " + decode (message , shift ));
107
+ }
108
+ }
109
+
110
+ }
0 commit comments