-
Notifications
You must be signed in to change notification settings - Fork 1
/
Caesarcipher.java
69 lines (69 loc) · 2.36 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
public class Caesarcipher{
public static void main(String[] args) {
String S="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s="abcdefghijklmnopqrstuvwxyz";
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Input text");
String inputtext=sc.nextLine();
System.out.println("Enter the secret key");
int key=sc.nextInt();
String ciphertext=Ciphertext(S,s,inputtext,key);
System.out.println("Cipher text is "+ciphertext);
int secretkey=0;
String Finaltext="";
String a[]=Decryption(S,s,inputtext,ciphertext);
System.out.println();
System.out.println("Plaintext is obtained "+a[0]+ " at key "+a[1]);
}
public static String Ciphertext(String S,String s,String inputtext,int key){
String ciphertext="";
for(int i=0;i<inputtext.length();i++){
char a=inputtext.charAt(i);
if(!Character.isLetter(a)){
ciphertext+=a;
}
else if(Character.isUpperCase(a)){
ciphertext+=S.charAt((S.indexOf(a)+key)%26);
}
else{
ciphertext+=s.charAt((s.indexOf(a)+key)%26);
}
}
return ciphertext;
}
public static String[] Decryption(String S,String s,String inputtext,String ciphertext){
String ar[]=new String[2];
for(int deckey=1;deckey<=26;deckey++){
String plaintext="";
for(int i=0;i<ciphertext.length();i++){
char a=ciphertext.charAt(i);
int b=0;
if(!Character.isLetter(a)){
plaintext+=a;
}
else if(Character.isUpperCase(a)){
b=(S.indexOf(a)-deckey)%26;
if(b<0)
plaintext+=S.charAt(b+26);
else
plaintext+=S.charAt(b);
}
else{
b=(s.indexOf(a)-deckey)%26;
if(b<0)
plaintext+=s.charAt(b+26);
else
plaintext+=s.charAt(b);
}
if(plaintext.equals(inputtext)){
ar[0]=plaintext;
ar[1]=Integer.toString(deckey);
return ar;
}
}
System.out.println("plain text at serect key "+deckey+" is "+plaintext);
}
return ar;
}
}