-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCipherKey.java
More file actions
135 lines (123 loc) · 4.58 KB
/
CipherKey.java
File metadata and controls
135 lines (123 loc) · 4.58 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
ENCODE
1. If the letters are on the same row, use the letters below them to replace them.
2. If the letters are on the same column, use the letters to their right to replace them.
3. If the letters are different, replace them with the letters on the same row, but in the column of the other letter
4. If the letters are the same, insert an X between them.
DECODE (The key is symmetrical [used to encode/decode], but the algorithm is not!)
Be aware that encoding and decoding have opposite shifts for same column/row letters!
1. If the letters are on the same row, use the letters ABOVE them to replace them.
2. If the letters are on the same column, use the letters to their LEFT to replace them.
3. Same.
4. The double letter problem means that there will be extra X's in your code. A human can deal with this.
*/
public class CipherKey{
public static int[] findRowCol(String[][] key, String letter){
int[] rowCol = {0, 0};
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
if (key[i][j].equals(letter)){
rowCol[0] = i;
rowCol[1] = j;
}
}
}
return rowCol;
}
public static String[][] makeDoubleArray(String keyString){
String[][] key = new String[5][5];
int index = 0;
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
key[i][j] = keyString.substring(index, index + 1);
index++;
}
}
return key;
}
public static String doubleLetter(String text){
for (int i = 0; i < text.length() - 1; i += 2){
if (text.substring(i, i+1).equals(text.substring(i+1, i+2)))
text = text.substring(0, i+1) + "X" + text.substring(i+1);
}
return text;
}
public static String noJ(String text){
String x = "";
for (int i = 0; i < text.length(); i++){
if (text.substring(i, i+1).equals("J")) x += "I";
else x += text.substring(i, i+1);
}
return x;
}
public static String vertical(String letterPair, String[][] key, int direction){
// direction is 1 if encode, -1 if decode
int[] rc1 = findRowCol(key, letterPair.substring(0, 1));
int[] rc2 = findRowCol(key, letterPair.substring(1));
int r = rc1[0] + direction;
if (r > 4) r = 0;
if (r < 0) r = 4;
String newPair = key[r][rc1[1]] + key[r][rc2[1]];
return newPair;
}
public static String horizontal(String letterPair, String[][] key, int direction){
// direction is 1 if encode, -1 if decode
int[] rc1 = findRowCol(key, letterPair.substring(0, 1));
int[] rc2 = findRowCol(key, letterPair.substring(1));
int c = rc1[1] + direction;
if (c > 4) c = 0;
if (c < 0) c = 4;
String newPair = key[rc1[0]][c] + key[rc2[0]][c];
return newPair;
}
public static String regular(String letterPair, String[][] key){
int[] rc1 = findRowCol(key, letterPair.substring(0, 1));
int[] rc2 = findRowCol(key, letterPair.substring(1));
String newPair = key[rc1[0]][rc2[1]] + key[rc2[0]][rc1[1]];
return newPair;
}
public static String encode(String plaintext, String [][] key){
String text = noJ(plaintext);
text = doubleLetter(text);
String ciphertext = "";
if (text.length() % 2 != 0) text += "Z";
for (int i = 0; i < text.length() - 1; i += 2){
String l1, l2 = "";
l1 = text.substring(i, i+1);
l2 = text.substring(i+1, i+2);
int[] rc1 = findRowCol(key, l1);
int[] rc2 = findRowCol(key, l2);
String pair = l1 + l2;
if (rc1[0] == rc2[0]) pair = vertical(pair, key, 1);
else if (rc1[1] == rc2[1]) pair = horizontal(pair, key, 1);
else pair = regular(pair, key);
ciphertext += pair;
}
return ciphertext;
}
public static String decode(String text, String [][] key){
String plaintext = "";
for (int i = 0; i < text.length() - 1; i += 2){
String l1, l2 = "";
l1 = text.substring(i, i+1);
if (i+1 < text.length() - 1) l2 = text.substring(i+1, i+2);
else l2 = text.substring(i+1);
int[] rc1 = findRowCol(key, l1);
int[] rc2 = findRowCol(key, l2);
String pair = l1 + l2;
if (rc1[0] == rc2[0]) pair = vertical(pair, key, -1);
else if (rc1[1] == rc2[1]) pair = horizontal(pair, key, -1);
else pair = regular(pair, key);
plaintext += pair;
}
return plaintext;
}
public static void main(String[] args) {
String encode = args[0];
String text = args[1].toUpperCase();
String keyString = args[2].toUpperCase();
String[][] key = makeDoubleArray(keyString);
if (encode.equals("encode")) System.out.println(encode(text, key));
else System.out.println(decode(text, key));
}
}