-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCaesar.java
More file actions
38 lines (32 loc) · 960 Bytes
/
Caesar.java
File metadata and controls
38 lines (32 loc) · 960 Bytes
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
import java.util.*;
class Caesar{
private int key;
public String encrypt(String message, int shift){
String encrypted = "";
char ch;
for(int i=0;i<message.length();i++){
ch=message.charAt(i);
if(Character.isLetter(ch)){
if(Character.isUpperCase(ch)){
encrypted+=(char)((int)(ch+shift-65)%26+65);
}else if(Character.isLowerCase(ch)){
encrypted+=(char)((int)(ch+shift-97)%26+97);
}
}else{
encrypted+=ch;
}
}
key=shift;
return encrypted;
}
public String decrypt(String message, int key){
return encrypt(message,26-(key%26));
}
String[] bruteForce(String cipher){
String results[]=new String[26];
for(int i=0;i<26;i++){
results[i]=decrypt(cipher, 26-i);
}
return results;
}
}