11package algorithms ;
22
3- import java .util .Arrays ;
43
54public class CaesarCipher {
6- private final String [] lowerAlpha = {"A" , "B" , "C" , "D" , "E" , "F" , "G" , "H" , "I" , "J" , "K" ,"L" , "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" , "U" , "V" , "W" , "X" , "Y" , "Z" };
75 private String originString ;
86 private int key ;
7+ private static final int ALPHA_NUMBER = 26 ;
8+ private static final char A = 'A' , Z = 'Z' , a = 'a' , z = 'z' ;
99
1010 public CaesarCipher (String originString , int key ) {
1111 super ();
@@ -27,20 +27,16 @@ public int getKey() {
2727 public void setKey (int key ) {
2828 this .key = key ;
2929 }
30- public String [] getLowerAlpha () {
31- return lowerAlpha ;
32- }
3330
3431 public static String encrypt (String origin , int key ) {
3532 String encryptContent = "" ;
3633
3734 for (int i = 0 ; i < origin .length (); i ++) {
3835 char temp = origin .charAt (i );
39- int charEncrypt = temp + key % 26 ;
40- if (temp >= 65 && temp <= 90 ) {
41- encryptContent += calcChar (temp , key , 65 , 90 );
42- }else if (temp >= 97 && temp <= 122 ) {
43- encryptContent += calcChar (temp , key , 97 , 122 );
36+ if (temp >= a && temp <= z ) {
37+ encryptContent += calcChar (temp , key , a );
38+ }else if (temp >= A && temp <= Z ) {
39+ encryptContent += calcChar (temp , key , A );
4440 } else {
4541 encryptContent += temp ;
4642 }
@@ -52,13 +48,11 @@ public static String decrypt(String origin, int key) {
5248 String decryptContent = "" ;
5349
5450 for (int i = 0 ; i < origin .length (); i ++) {
55- char temp = origin .charAt (i );
56- int charEncrypt = temp - key % 26 ;
57-
58- if (temp >= 65 && temp <= 90 ) {
59- decryptContent += calcCharDecrypt (temp , key , 65 , 90 );
60- }else if (temp >= 97 && temp <= 122 ) {
61- decryptContent += calcCharDecrypt (temp , key , 97 , 122 );
51+ char temp = origin .charAt (i );
52+ if (temp >= a && temp <= z ) {
53+ decryptContent += calcCharDecrypt (temp , key , a , z );
54+ }else if (temp >= A && temp <= Z ) {
55+ decryptContent += calcCharDecrypt (temp , key , A , Z );
6256 } else {
6357 decryptContent += temp ;
6458 }
@@ -71,11 +65,10 @@ public String encrypt() {
7165
7266 for (int i = 0 ; i < originString .length (); i ++) {
7367 char temp = originString .charAt (i );
74- int charEncrypt = temp + key % 26 ;
75- if (temp >= 65 && temp <= 90 ) {
76- encryptContent += calcChar (temp , key , 65 , 90 );
77- }else if (temp >= 97 && temp <= 122 ) {
78- encryptContent += calcChar (temp , key , 97 , 122 );
68+ if (temp >= a && temp <= z ) {
69+ encryptContent += calcChar (temp , key , a );
70+ }else if (temp >= A && temp <= Z ) {
71+ encryptContent += calcChar (temp , key , A );
7972 } else {
8073 encryptContent += temp ;
8174 }
@@ -87,45 +80,37 @@ public String decrypt() {
8780 String decryptContent = "" ;
8881
8982 for (int i = 0 ; i < originString .length (); i ++) {
90- char temp = originString .charAt (i );
91- int charEncrypt = temp - key % 26 ;
92-
93- if (temp >= 65 && temp <= 90 ) {
94- decryptContent += calcCharDecrypt (temp , key , 65 , 90 );
95- }else if (temp >= 97 && temp <= 122 ) {
96- decryptContent += calcCharDecrypt (temp , key , 97 , 122 );
83+ char temp = originString .charAt (i );
84+ if (temp >= a && temp <= z ) {
85+ decryptContent += calcCharDecrypt (temp , key , a , z );
86+ }else if (temp >= A && temp <= Z ) {
87+ decryptContent += calcCharDecrypt (temp , key , A , Z );
9788 } else {
9889 decryptContent += temp ;
9990 }
10091 }
10192 return decryptContent ;
10293 }
10394
104- public static char calcChar (int charOrigin , int indexKey , int start , int end ) {
105- int charEncrypt = charOrigin + indexKey % 26 ;
95+ public static char calcChar (int charOrigin , int indexKey , int start ) {
96+ int charEncrypt = ( charOrigin - start + indexKey ) % ALPHA_NUMBER ;
10697
107- if (charEncrypt <= end ) {
108- return (char ) (charEncrypt );
109- }
110- return (char ) (start + (charEncrypt - end - 1 ));
98+ return (char ) (start + charEncrypt );
11199 }
112100
113101 public static char calcCharDecrypt (int charOrigin , int indexKey , int start , int end ) {
114- int charDecrypt = charOrigin - indexKey % 26 ;
102+ int charDecrypt = (( charOrigin - start ) - ( indexKey % ALPHA_NUMBER )) ;
115103
116- if (charDecrypt >= start ) {
117- return (char ) (charDecrypt );
104+ if (charDecrypt < 0 ) {
105+ return (char ) (end + charDecrypt );
118106 }
119- return (char ) (end - start + charDecrypt + 1 );
107+ return (char ) (start + charDecrypt );
120108 }
121109
122110
123111 @ Override
124112 public String toString () {
125- return "CaesarCipher" ;
113+ return "CaesarCipher Algorithm " ;
126114 }
127115
128-
129-
130-
131116}
0 commit comments