This repository was archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVernamCipher.java
More file actions
115 lines (97 loc) · 3.56 KB
/
VernamCipher.java
File metadata and controls
115 lines (97 loc) · 3.56 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.io.*;
import java.util.ArrayList;
class Vernam {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
File input = new File("YOUR FILE FOR INPUT");
File output = new File("YOUR FILE FOR OUTPUT");
BufferedReader br1 = new BufferedReader(new FileReader(input));
BufferedWriter or = new BufferedWriter(new FileWriter(output));
System.out.println("Select 1 for Encryption, select 2 for Decryption: ");
int choice = Integer.parseInt(br.readLine());
if (choice == 1) {
String str = br1.readLine();
String key = keyString(str.length());
System.out.println("Random Key generated is: " + key);
ArrayList<String> pt = strToBinary(str);
ArrayList<String> keyList = strToBinary(key);
String ct = "", temp = "";
StringBuffer sb = new StringBuffer(ct);
for (int i = 0; i < pt.size(); i++) {
temp = xoring(pt.get(i), keyList.get(i), pt.get(i).length());
int decimal = Integer.parseInt(temp, 2);
System.out.print(decimal + " ");
sb.append((char) decimal);
}
System.out.println();
or.write(sb.toString());
} else {
String str = br1.readLine();
String key = br.readLine();
ArrayList<String> pt = strToBinary(str);
ArrayList<String> keyList = strToBinary(key);
String ct = "", temp = "";
StringBuffer sb = new StringBuffer(ct);
for (int i = 0; i < pt.size(); i++) {
temp = xoring(pt.get(i), keyList.get(i), pt.get(i).length());
int decimal = Integer.parseInt(temp, 2);
System.out.print(decimal + " ");
sb.append((char) decimal);
}
System.out.println();
or.write(sb.toString());
}
or.close();
}
static String keyString(int n) {
String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvxyz";
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; i++) {
int index = (int)(AlphaNumericString.length() * Math.random());
sb.append(AlphaNumericString.charAt(index));
}
return sb.toString();
}
static ArrayList<String> strToBinary(String s) {
int n = s.length();
ArrayList<String> al = new ArrayList<>();
for (int i = 0; i < n; i++) {
int val = Integer.valueOf(s.charAt(i));
String bin = "";
while (val > 0) {
if (val % 2 == 1) {
bin += '1';
} else
bin += '0';
val /= 2;
}
bin = reverse(bin);
al.add((bin));
System.out.print(bin + " ");
}
System.out.println();
return al;
}
static String reverse(String input)
{
char[] a = input.toCharArray();
int l, r = 0;
r = a.length - 1;
for (l = 0; l < r; l++, r--) {
char temp = a[l];
a[l] = a[r];
a[r] = temp;
}
return String.valueOf(a);
}
static String xoring(String a, String b, int n) {
String ans = "";
for (int i = 0; i < n; i++) {
if (a.charAt(i) == b.charAt(i))
ans += "0";
else
ans += "1";
}
return ans;
}
}