-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vernam.java
83 lines (68 loc) · 2.34 KB
/
Vernam.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Scanner;
/**
* Created by Ruby on 3/10/2016.
*/
public class Vernam {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter key: ");
String key=in.nextLine();
System.out.print("Enter msg: ");
String msg=in.nextLine();
System.out.println("Encrypting.....");
System.out.print("The encrypted text: " + getEncrypted(msg, key));
}
static String getEncrypted(String plaintext, String key){
String alphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int decimal1, decimal2, XORResult;
String binary1, binary2, encryptedText="";
plaintext=plaintext.replaceAll(" ","").toUpperCase();
key=key.toUpperCase();
if (key.length()<plaintext.length()){
key=manageKeyLength(key, plaintext.length());
}
for (int i=0; i<plaintext.length(); i++){
decimal1=alphabets.indexOf(plaintext.charAt(i));
binary1=Integer.toBinaryString(decimal1);
if (binary1.length()<5){ binary1=increaseLength(binary1);}
decimal2=alphabets.indexOf(key.charAt(i));
binary2=Integer.toBinaryString(decimal2);
if (binary2.length()<5){ binary2=increaseLength(binary2);}
XORResult=Integer.parseInt(performXOR(binary1,binary2), 2);
if (XORResult>25){
XORResult=XORResult%26;
}
encryptedText+=alphabets.charAt(XORResult);
}
return encryptedText;
}
static String increaseLength(String binary){
int diff=5-binary.length();
for (int i=0; i<diff; i++){
binary=0+ binary;
}
return binary;
}
static String performXOR(String binary1, String binary2){
String newBinary="";
for (int i=0; i<5; i++){
if (binary1.charAt(i)==binary2.charAt(i)){
newBinary=newBinary+0;
}else{
newBinary=newBinary+1;
}
}
return newBinary;
}
static String manageKeyLength(String key, int reqLen){
int val=reqLen-key.length();
int j=0;
for (int i=val; i>0; i--){
key+=key.charAt(j++);
if (j==key.length()-1){
j=0;
}
}
return key;
}
}