-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINS_P8.java
More file actions
57 lines (49 loc) · 1.37 KB
/
Copy pathINS_P8.java
File metadata and controls
57 lines (49 loc) · 1.37 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
package ins;
import java.util.Scanner;
/**
*
* @author Raj Dhanani
*/
class Vernam {
String plainText, cipherText, key;
Vernam(String plainText, String key) {
this.key = key;
this.plainText = plainText;
encrypt();
}
void encrypt() {
byte[] pT = plainText.getBytes();
byte[] ky = key.getBytes();
byte[] cT = new byte[pT.length];
int i = 0;
for (byte x : pT) {
int index = i % ky.length;
cT[i] = (byte) (x ^ ky[index]);
i++;
}
cipherText = new String(cT);
}
String decrypt(String cipherText) {
this.cipherText = cipherText;
byte[] cT = cipherText.getBytes();
byte[] ky = key.getBytes();
byte[] pT = new byte[cT.length];
int i = 0;
for (byte x : cT) {
int index = i % ky.length;
pT[i] = (byte) (x ^ ky[index]);
i++;
}
plainText = new String(pT);
return (plainText);
}
}
public class INS_P8 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("ENTER PLAIN TEXT AND KEY (LINE SEPERATED):");
Vernam v = new Vernam(in.nextLine(), in.nextLine());
System.out.println("ENC:" + v.cipherText);
System.out.println("DEC:" + v.decrypt(v.cipherText));
}
}