-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVigenereCipher.java
More file actions
65 lines (64 loc) · 2.6 KB
/
Copy pathVigenereCipher.java
File metadata and controls
65 lines (64 loc) · 2.6 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
public class VigenereCipher {
// A = 65, Z = 90, a = 97, z = 122
static String encrypt(String plainText, String key) {
String cipherText = "";
int letter, extraShift, noOfShift;
plainText = plainText.toLowerCase();
key = key.toLowerCase();
for (int i = 0, j = 0; i < plainText.length(); i++, j++) {
if (j == key.length())
j = 0;
letter = plainText.charAt(i);
noOfShift = key.charAt(j) - 97;
if (Character.isLetter(letter)) {
if(letter + noOfShift > 'z') {
// calculating extra shift of the letter over and above 'z'
extraShift = (letter + noOfShift) - ('z' + 1);
// adding extra shift to 'a'
letter = 'a' + extraShift;
} else
letter += noOfShift;
} else {
if (letter + noOfShift > '~') {
// calculating extra shift of the letter over and above '~'
extraShift = (letter + noOfShift) - ('~' + 1);
// adding extra shift to ' ' (whitespace)
letter = ' ' + extraShift;
} else
letter += noOfShift;
}
cipherText += (char) letter;
}
return cipherText;
}
static public String decrypt(String cipherText, String key) {
String plainText = "";
int letter, extraShift, noOfShift;
key = key.toLowerCase();
for (int i = 0, j = 0; i < cipherText.length(); i++, j++) {
if (j == key.length())
j = 0;
letter = cipherText.charAt(i);
noOfShift = key.charAt(j) - 97;
if (Character.isLetter(letter)) {
if ((letter - noOfShift) < 'a') {
// calculating extra shift of the letter gone below 'a'
extraShift = ('a' - 1) - (letter - noOfShift);
// reducing the extra shift from 'z'
letter = 'z' - extraShift;
} else
letter -= noOfShift;
} else {
if (letter - noOfShift < ' ') {
// calculating extra shift of the letter gone below ' ' (whitespace)
extraShift = (' ' - 1) - (letter - noOfShift);
// reducing extra shift from '~'
letter = '~' + extraShift;
} else
letter -= noOfShift;
}
plainText += (char) letter;
}
return plainText;
}
}