-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvigenere.java
53 lines (48 loc) · 1.46 KB
/
vigenere.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
import java.util.*;
class Main{
private static class VigenereCipher{
private String preprocess(String text){
return (text.toLowerCase()).replaceAll("\\s+","");
}
private String keyGenerate(String key, String plainText){
key = preprocess(key);
while(key.length() < plainText.length())
key += key;
return key.substring(0,plainText.length());
}
private String encrypt(String plainText, String key){
plainText = preprocess(plainText);
key = keyGenerate(key, plainText);
int n = plainText.length();
int ks = key.length();
String res = "";
for(int i = 0; i < n; i++){
res += (char)(((plainText.charAt(i) + key.charAt(i) - 194) % 26) + 97)+"";
}
return res;
}
private String decrypt(String cipherText, String key){
cipherText = preprocess(cipherText);
key = keyGenerate(key, cipherText);
int n = cipherText.length();
int ks = key.length();
String res = "";
for(int i = 0; i < n; i++){
res += (char)(((cipherText.charAt(i) - key.charAt(i) + 26) % 26) + 97) + "";
}
return res;
}
}
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.printf("Enter plainText: ");
String text = in.nextLine();
System.out.printf("Enter key: ");
String key = in.nextLine();
VigenereCipher cipher = new VigenereCipher();
text = cipher.encrypt(text, key);
System.out.printf("cipherText: "+text+"\n");
text = cipher.decrypt(text, key);
System.out.printf("plainText: "+text);
}
}