-
Notifications
You must be signed in to change notification settings - Fork 8
/
TestCaesarCipher.java
56 lines (51 loc) · 1.77 KB
/
TestCaesarCipher.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
48
49
50
51
52
53
54
55
56
class CaesarCipher{
private String alphabet;
private String shiftedAlphabet;
private int mainKey;
public CaesarCipher(int key){
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
shiftedAlphabet = alphabet.substring(key) + alphabet.substring(0,key);
mainKey = key;
}
public String encrypt(String input){
StringBuilder sb = new StringBuilder(input);
String alphaLower = alphabet.toLowerCase();
String shiftLower = shiftedAlphabet.toLowerCase();
for (int i=0; i < sb.length(); i++) {
char c = sb.charAt(i);
int idx = alphabet.indexOf(c);
int idxLower = alphaLower.indexOf(c);
if(Character.isLowerCase(c)){
if(idxLower != -1){
c = shiftLower.charAt(idxLower);
sb.setCharAt(i, c);
}
}
else{
if (idx != -1){
c = shiftedAlphabet.charAt(idx);
sb.setCharAt(i, c);
}
}
}
return sb.toString();
}
public String decrypt(String input){
CaesarCipher c = new CaesarCipher(26 - mainKey);
String decrypt = c.encrypt(input);
return decrypt;
}
public void testEncrypt(){
String message = "Can you imagine life WITHOUT the internet AND computers in your pocket?";
String encryptedMessage = encrypt(message);
System.out.println(encryptedMessage);
System.out.println("Decrypted message: " + decrypt(encryptedMessage));
}
}
public class TestCaesarCipher {
public static void main(String[] args) {
CaesarCipher c=new CaesarCipher(2);
String encrypt=c.encrypt("dscjscsdjcdscsdcsdcdc");
System.out.println(encrypt);
}
}