-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVigenereCipher.java
More file actions
35 lines (32 loc) · 950 Bytes
/
VigenereCipher.java
File metadata and controls
35 lines (32 loc) · 950 Bytes
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
import java.util.*;
public class VigenereCipher{
public String en(String text, String key)
{
String r = "";
text = text.toUpperCase();
key = key.toUpperCase();
for (int i = 0, k = 0; i < text.length(); i++){
char c = text.charAt(i);
if (c < 'A' || c > 'Z'){
continue;
}
r += (char) ((c + key.charAt(k) - 2 * 'A') % 26 + 'A');
k = ++k % key.length();
}
return r;
}
public String de(String text, String key){
String r = "";
text = text.toUpperCase();
key = key.toUpperCase();
for (int i = 0, k = 0; i < text.length(); i++){
char c = text.charAt(i);
if (c < 'A' || c > 'Z'){
continue;
}
r += (char) ((c - key.charAt(k) + 26) % 26 + 'A');
k = ++k % key.length();
}
return r;
}
}