-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneTimePad.java
More file actions
247 lines (239 loc) · 9.13 KB
/
Copy pathOneTimePad.java
File metadata and controls
247 lines (239 loc) · 9.13 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication4;
import java.io.*;
import java.security.SecureRandom;
public class OneTimePad {
StringBuilder binary = new StringBuilder();
StringBuilder newkey = new StringBuilder();
StringBuilder encryptioncipher = new StringBuilder();
StringBuilder cipher = new StringBuilder();
StringBuilder keyretrieved = new StringBuilder();
StringBuilder decryptioncipher = new StringBuilder();
StringBuilder frequencychecker = new StringBuilder();
StringBuilder k = new StringBuilder();
StringBuilder textfromfile = new StringBuilder();
public void Enc()
//this function is used to read key from key.txt file and plaintext from plaintext.txt file and then implement xor and generate cipher text which is stored in the ciphertext.txt file
{
try {
System.out.println("message can't be more than 4 letters");
FileInputStream fis1 = new FileInputStream("/Users/aarushisinghal/desktop/key.txt");
int y;
do {
y = fis1.read();
if (y ==-1) {
break;
}
k.append((char)y);
} while (true);
System.out.println("key "+k);
} catch (Exception e) {
System.out.println("file doesnot exist" + e);
}
char[] a = {};
char[] v = {};
try {
FileInputStream fis = new FileInputStream("/Users/aarushisinghal/desktop/plaintext.txt");
do {
int i = fis.read();
if (i == -1) {
break;
}
String text = String.valueOf((char)i);
textfromfile.append(text);
byte[] msg = text.getBytes();
for (byte b : msg) {
int val = b;
for (int j = 0; j < 8; j++) {
binary.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
} while (true);
if (binary.length() ==k.length()){
System.out.println("message " +textfromfile);
System.out.println("plaintext " +binary);
for (int g = 0; g < binary.length(); g++) {
a = binary.toString().toCharArray();
}
for (int q = 0; q < k.length(); q++) {
v = k.toString().toCharArray();
}
for (int x = 0; x <k.length(); x++) {
if (a[x] == v[x]) {
encryptioncipher.append("0");
} else {
encryptioncipher.append("1");
}
}
try {
FileOutputStream writer = new FileOutputStream("/Users/aarushisinghal/desktop/ciphertext.txt", true);
PrintStream out = new PrintStream(writer, true);
out.print(encryptioncipher);
} catch (Exception e) {
System.out.println("File doesnot exist" +e);
}
System.out.println("ciphertext " + encryptioncipher);
System.out.println("encryption time " +System.nanoTime());
}
else {
System.out.println("length of the message is incorrect");
}
} catch (Exception e) {
System.out.println("file doesnot exist");
}
}
public void keygeneration()
//this function is used to generate random secure key with length betwee 1 to 128 bits and storing it in newkey.txt file
{
SecureRandom random = new SecureRandom();
final byte[] key;
int sp = random.nextInt(16);
if (sp >= 1) {
key = new byte[sp];
random.nextBytes(key);
for (byte b : key) {
int val = b;
for (int j = 0; j < 8; j++) {
newkey.append((val & 128) == 0 ? 0 : 1);
val <<= 1;
}
}
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("/Users/aarushisinghal/desktop/newkey.txt"));
writer.write(newkey.toString());
writer.close();
} catch (Exception e) {
System.out.println("file doesnot exist" + e);
}
System.out.println("random key generation " +newkey);
}
}
public void keyfrequencychecker()
//for checking frequency of each random 3 bit key which is randomly generated and checking whether it is uniformly sistributed or not
{
SecureRandom random = new SecureRandom();
int sp = random.nextInt(999) + 100;
String o = Integer.toString(sp);
if (o.length() == 3) {
while (sp != 0) {
if (sp % 2 == 0) {
frequencychecker.append("0");
sp = sp / 10;
} else {
frequencychecker.append("1");
sp = sp / 10;
}
}
}
System.out.println("3 bit key " + frequencychecker.append(" "));
int count = 0;
String text = frequencychecker.toString();
try {
String[] keys = text.split(" ");
String[] uniqueKeys;
uniqueKeys = getUniqueKeys(keys);
for (String key : uniqueKeys) {
for (String s : keys) {
if (key.equals(s)) {
count++;
}
}
System.out.println("Count of [" + key + "] is : " + count);
count = 0;
}
} catch (Exception e) {
}
}
public String[] getUniqueKeys(String[] keys) {
String[] uniqueKeys = new String[keys.length];
uniqueKeys[0] = keys[0];
int uniqueKeyIndex = 1;
boolean keyAlreadyExists = false;
for (int i = 1; i < keys.length; i++) {
for (int j = 0; j <= uniqueKeyIndex; j++) {
if (keys[i].equals(uniqueKeys[j])) {
keyAlreadyExists = true;
}
}
if (!keyAlreadyExists) {
uniqueKeys[uniqueKeyIndex] = keys[i];
uniqueKeyIndex++;
}
keyAlreadyExists = false;
}
return uniqueKeys;
}
public void decryption()
//this function reads the input from ciphertext.txt and secret key from key.txt and implement exclusive or on it to retrieve secure message and store it in result.txt
{
try {
FileInputStream fis1 = new FileInputStream("/Users/aarushisinghal/desktop/ciphertext.txt");
FileInputStream fis2 = new FileInputStream("/Users/aarushisinghal/desktop/key.txt");
do {
int i = fis1.read();
if (i == -1) {
break;
}
cipher.append((char) i);
int y = fis2.read();
if (y == -1) {
break;
}
keyretrieved.append((char) y);
} while (true);
System.out.println("ciphertext " + cipher);
System.out.println("keyretrieved " + keyretrieved);
char[] a = {};
char[] v = {};
for (int g = 0; g < cipher.length(); g++) {
a = cipher.toString().toCharArray();
}
for (int q = 0; q < keyretrieved.length(); q++) {
v = keyretrieved.toString().toCharArray();
}
if (cipher.length() == keyretrieved.length()) {
for (int x = 0; x < keyretrieved.length(); x++) {
if (a[x] == v[x]) {
decryptioncipher.append("0");
} else {
decryptioncipher.append("1");
}
}
} else {
System.out.println("ERROR");
}
} catch (Exception e) {
System.out.println("file doesnot exist" + e);
}
String text = decryptioncipher.toString();
try {
String message = "";
for (int i = 0; i < (text.length()) / 8; i++) {
int a = Integer.parseInt(text.substring(8 * i, (i + 1) * 8), 2);
message += (char) (a);
}
FileOutputStream fos3 = new FileOutputStream("/Users/aarushisinghal/desktop/result.txt", true);
PrintStream out = new PrintStream(fos3, true);
System.out.println("decrypted text " + decryptioncipher);
System.out.println("decrypted message " + message);
out.println("message " + message);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] arg) {
OneTimePad op = new OneTimePad();
op.Enc();
//op.keygeneration();
op.decryption();
// for (int t = 0; t < 5000; t++) {
// op.keyfrequencychecker();
// }
// This commeneted part is to check uniform distribution of key for 3 bt key length
}
}