forked from scr34m0/Java-Cryptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVernamCipher.java
More file actions
41 lines (39 loc) · 1.27 KB
/
VernamCipher.java
File metadata and controls
41 lines (39 loc) · 1.27 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author arthur.paixao
* @category Vernam Cipher Encode/Decode
*/
public class VernamCipher {
public static void main(String args[]) throws IOException {
String line, key, result1, result2;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter g:");
int g = Integer.parseInt(in.readLine());
System.out.println("Enter a:");
int a = Integer.parseInt(in.readLine());
System.out.println("Enter b:");
int b = Integer.parseInt(in.readLine());
System.out.println("Enter p:");
int p = Integer.parseInt(in.readLine());
int keyint = (int) (Math.pow(g, a * b)) % p;
key = Integer.toString(keyint);
System.out.println("Key:");
System.out.println(key);
System.out.println("Enter the string:");
line = in.readLine();
System.out.println("Encrypted:");
result1 = "";
for (int i = 0; i < line.length(); i++) {
result1 += (char) (line.charAt(i) ^ key.charAt(i % key.length()));
}
System.out.println(result1);
System.out.println("Decrypted:");
result2 = "";
for (int i = 0; i < result1.length(); i++) {
result2 += (char) (result1.charAt(i) ^ key.charAt(i % key.length()));
}
System.out.println(result2);
}
}