-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
308 lines (275 loc) · 9.36 KB
/
Copy pathMessage.java
File metadata and controls
308 lines (275 loc) · 9.36 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//Name: GUO, Yuzhou
//ID: 260715042
package assignment1;
public class Message {
public String message;
public int lengthOfMessage;
public Message (String m){
message = m;
lengthOfMessage = m.length();
this.makeValid();
}
public Message (String m, boolean b){
message = m;
lengthOfMessage = m.length();
}
/**
* makeValid modifies message to remove any character that is not a letter and turn Upper Case into Lower Case
*/
public void makeValid(){
String valid = this.message.replaceAll("[^a-zA-Z]", ""); //if the element is not in this range, delete it
this.message = valid.toLowerCase();
lengthOfMessage = this.message.length();
}
/**
* prints the string message
*/
public void print(){
System.out.println(message);
}
/**
* tests if two Messages are equal
*/
public boolean equals(Message m){
if (message.equals(m.message) && lengthOfMessage == m.lengthOfMessage){
return true;
}
return false;
}
/**
* caesarCipher implements the Caesar cipher : it shifts all letter by the number 'key' given as a parameter.
*/
public void caesarCipher(int key){
String s = "";
for (int i=0;i<message.length();i++)
{
s = s + charAfterShift(message.charAt(i), key); //the helper method can be found at very bottom, which works just as the name says
}
this.message = s;
lengthOfMessage = this.message.length(); //assign the message as well as the length back to the object
}
public void caesarDecipher(int key){
this.caesarCipher(- key);
}
/**
* caesarAnalysis breaks the Caesar cipher
* - compute how often each letter appear in the message
* - compute a shift (key) such that the letter that happens the most was originally an 'e'
* - decipher the message using the key just computed
*/
public void caesarAnalysis(){
int arrayLength = 0;
for (int i=97; i<123; i++)
{
for (int j=0; j<lengthOfMessage;j++)
{
if ((char)(i)==message.charAt(j))
{
arrayLength++;
}
}
} //note how many elements we have in the message (since we ensure that only alphabets there, we can do this)
int[] key = new int[arrayLength];
char[] value = new char[arrayLength]; //these two arrays together work like HashMap, notice that "same index" is important here, we can
int position = 0; //use this property to get the value or key.
char v;
for (int i=0; i<lengthOfMessage;i++)
{
if(containsValue(value, message.charAt(i))==false) //containsValue is a helper method, does the same job as the method in HashMap class.
{
v = message.charAt(i);
int c = 1; //if it is not in the value array, then we count the total number of that character.
for (int j=(i+1); j<message.length();j++) //if so, then there's no need to count again, we can skip it and go for next one.
{
if (message.charAt(i)==message.charAt(j))
{
c++;
}
else
{
continue;
}
}
value[position] = v;
key[position] = c;
position++;
}
else
{
continue;
}
}
int shiftKey = 0; //Declare the variable outside the loop.
for (int i=lengthOfMessage; i>0; i--) //the maximum a number can show up in the message is the length of message.
{
if (keyPosition(key, i)!=(-1))
{
shiftKey = (int)(value[keyPosition(key, i)]-'e');
break;
}
}
this.caesarDecipher(shiftKey); //once we find out the key, we can call the methods we just wrote to finish the job.
}
/**
* vigenereCipher implements the Vigenere Cipher : it shifts all letter from message by the corresponding shift in the 'key'
*/
public void vigenereCipher (int[] key){
String s = "";
for (int i=0;i<message.length();i++)
{
s = s + charAfterShift(message.charAt(i), key[i%key.length]);
}
this.message = s;
lengthOfMessage = this.message.length();
}
/**
* vigenereDecipher deciphers the message given the 'key' according to the Vigenere Cipher
*/
public void vigenereDecipher (int[] key){
String s = "";
for (int i=0;i<message.length();i++)
{
s = s + charAfterShift(message.charAt(i), -key[(i%key.length)]);
}
this.message = s;
lengthOfMessage = this.message.length();
}
/**
* transpositionCipher performs the transition cipher on the message by reorganizing the letters and eventually adding characters
*/
public void transpositionCipher (int key){
int column = key;
int row = (lengthOfMessage/column)+1;
boolean b = (((double)lengthOfMessage/(double)column)*10)%10==0; //make sure we don't add 1 for mistake.
if (b)
{
row = lengthOfMessage/column;
}
char[][] matrix = setMatrix1(message, column, row); //here we set the matrix structure and put elements in HORIZONTALLY.
String s = readMatrixCByC(matrix); //then read column by column.
this.message = s;
lengthOfMessage = this.message.length();
}
/**
* transpositionDecipher deciphers the message given the 'key' according to the transition cipher.
*/
public void transpositionDecipher (int key){
int column = key;
int row = (lengthOfMessage/column)+1;
boolean b = (((double)lengthOfMessage/(double)column)*10)%10==0; //make sure we don't add 1 for mistake.
if (b)
{
row = lengthOfMessage/column;
}
char[][] matrix = setMatrix2(message, column, row); //here we set the matrix structure and put elements in VERTICALLY.
String s = readMatrixRowByRow(matrix);
String ss = s.replace("*", "");
this.message = ss;
lengthOfMessage = this.message.length();
}
//charAfterShift: in the form of string, but more clear to state that it represents a shifted character.
public static String charAfterShift (char c, int key)
{
int shift = (int)(c)+(key%26);
if (shift>'z')
{
shift = 'a' + (shift-'z'-1);
}
else if (shift<'a')
{
shift = 'z'-('a'-shift-1);
}
String s = ""+(char)(shift);
return s;
}
//setMatrix1: set the matrix structure and put elements in horizontally.
public char[][] setMatrix1 (String m, int column, int row)
{
int counter = 0;
char[][] x = new char[row][column];
for (int i=0; i<row;i++)
{
for (int j=0; j<column; j++)
{
if (counter<m.length())
{
x[i][j]=m.charAt(counter);
counter++;
}
else if (counter>=m.length())
{
x[i][j] = '*';
}
}
}
return x;
}
//setMatrix2: set the matrix structure and put elements in vertically.
public char[][] setMatrix2 (String m, int column, int row)
{
int counter = 0;
char[][] x = new char[row][column];
for (int i=0; i<column;i++)
{
for (int j=0; j<row; j++)
{
if (counter<m.length())
{
x[j][i]=m.charAt(counter);
counter++;
}
}
}
return x;
}
//readMatrixRowByRow: as the name says.
public String readMatrixRowByRow (char[][] x)
{
String s = "";
for (int i=0;i<x.length;i++)
{
for (int j=0;j<x[0].length;j++)
{
s = s + x[i][j];
}
}
return s;
}
//readMatrixCByC: read the matrix column by column.
public String readMatrixCByC (char[][] x)
{
String s = "";
for (int i=0;i<x[0].length;i++)
{
for (int j=0;j<x.length;j++)
{
s = s + x[j][i];
}
}
return s;
}
//containsValue: test if a character is in the array char[].
public boolean containsValue (char[] s, char c)
{
for (int i=0; i<s.length;i++)
{
if (s[i]==c)
{
return true;
}
}
return false;
}
//keyPosition: report the position of an integer in the array, also has the property to tell whether it exists or not.
public int keyPosition (int[] s, int t)
{
for (int i=0; i<s.length;i++)
{
if (s[i]==t)
{
return i;
}
}
return -1; //so that we don't need another boolean method to tell us whether it exists or not.
}
}