-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesarCipher.java
47 lines (43 loc) · 1.57 KB
/
CaesarCipher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//class for doing encryption and decryption using the Caesar Cipher
public class CaesarCipher {
//encryption array
protected char[] encoder = new char[26];
//decryption array
protected char[] decoder = new char[26];
//constructor that initializes the encryption and decryption arrays
public CaesarCipher(int rotation){
for(int k=0;k<26;k++){
encoder[k] = (char)('A'+(k+rotation)%26);
decoder[k] = (char)('A'+(k-rotation+26)%26);
}
}
//return string representing encrypted message
public String encrypt(String message){
//use encoder array
return transform(message,encoder);
}
//return decrypted message given encrypted secret
public String decrypt(String secret){
return transform(secret,decoder);
}
//return transformation of original string using given code
private String transform(String original,char[] code){
char[] msg=original.toCharArray();
for(int k=0;k<msg.length;k++)
if (Character.isUpperCase(msg[k])){ //we have a letter to change
int j=msg[k]-'A'; //will be value from 0 to 25
msg[k]=code[j]; //replace the characyer
}
return new String(msg);
}
public static void main(String[] args){
CaesarCipher cipher = new CaesarCipher(3);
System.out.println("encryption code = "+new String(cipher.encoder));
System.out.println("decryption code = "+new String(cipher.decoder));
String message="THE EAGLE IS IN PLAY;MEET AT JOE'S.";
String coded=cipher.encrypt(message);
System.out.println("secret:"+coded);
String answer=cipher.decrypt(coded);
System.out.println("Message:"+answer); //should be plaintext again
}
}