-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOneTimePad.java
56 lines (55 loc) · 1.36 KB
/
OneTimePad.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
import java.util.*;
class OneTimePad{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the PlainText");
String inputtext=sc.nextLine();
System.out.println("Enter the Key");
String key=sc.nextLine();
Ciphertext(inputtext,key);
}
public static void Ciphertext(String inputtext,String key){
String cipher="";
String temp=key;
int n=temp.length();
int i=0;
while(key.length()<inputtext.length()){
if(i==n){
i=0;
}
key+=temp.charAt(i);
i++;
}
//System.out.println(key);
for(int j=0;j<inputtext.length();j++){
String a=Integer.toBinaryString(inputtext.charAt(j));
String b=Integer.toBinaryString(key.charAt(j));
String c=xorop(a,b);
cipher+=c+" ";
}
System.out.println("Cipher text is "+cipher);
decrypt(cipher,key);
}
public static String xorop(String a,String b){
String c="";
for(int i=0;i<a.length();i++){
if(a.charAt(i)==b.charAt(i))
c+="0";
else
c+="1";
}
return c;
}
public static void decrypt(String cipher,String key){
String[] a=cipher.split(" ");
String plain="";
for(int i=0;i<a.length;i++)
{
String b=Integer.toBinaryString(key.charAt(i));
String c=xorop(a[i],b);
int decimal=Integer.parseInt(c,2);
plain+=(char)decimal;
}
System.out.println("Plain text is "+plain);
}
}