@matfah/

RouteCipher FR 2011

Java

No description

fork
loading
Files
  • Main.java
  • RouteCipher.java
Main.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
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
import java.lang.reflect.*;
import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    for(int i = 0; i < 100; i++)
      testFillBlock();
    for(int i = 0; i < 100; i++)
      testEncryptMessage();
    System.out.println("All tests passed!");
  }
  
  public static void testFillBlock() {
    
    int r = (int)(Math.random()*8) + 1;
    int c = (int)(Math.random()*8) + 1;
    
    CustomRouteCipher cipher = new CustomRouteCipher(r, c);
    String randomMessage = "";
    
    for(int i = 0; i < 18; i++) {
      if(Math.random() < 0.1)
        randomMessage += " ";
      else
        randomMessage += (char)('a' + (int)(Math.random()*26));
    }
    
    try {
     cipher.fillBlock(randomMessage); 
    }
    catch(Exception e) {
      System.out.println("Error calling fillBlock() method");
      System.exit(0);
    }
    
    if(cipher.getNumCols() != c) {
      System.out.println("Calling fillBlock() should NOT change the numCols instance variable");
      System.exit(0);
    }
    
    if(cipher.getNumRows() != r) {
      System.out.println("Calling fillBlock() should NOT change the numRows instance variable");
      System.exit(0);
    }
    
    String[][] result = cipher.getLetterBlock();
    
    for(String[] row: result) {
      for(String r2: row) {
        if(r2 == null) {
          System.out.println("Calling fillBlock() should NOT leave null values in the array!");
          System.exit(0);
        }
        else if(r2.length() != 1) {
          System.out.println("Calling fillBlock() should NOT put String's with length different than 1 in the array");
          System.exit(0);
        }
      }
    }
    
    String resultStr = java.util.Arrays.deepToString(result).replace("[","").replace("]", "").replace(", ", "");
    String shouldBe = randomMessage;
    
    if(shouldBe.length() >= r*c)
      shouldBe = randomMessage.substring(0, r*c);
    else {
      //shouldBe needs to end with some A's...
      while(shouldBe.length() < r*c)
        shouldBe += "A";
    }
    
    if(!resultStr.equals(shouldBe)) {
      System.out.println(resultStr);
      System.out.println(randomMessage);
      System.out.println("Calling fillBlock() does not fill the 2D array with the correct values");
      System.exit(0);
    }
  }
  
  
  public static void testEncryptMessage() {
    
    int r = (int)(Math.random()*8)+1;
    int c = (int)(Math.random()*8)+1;
    
    CustomRouteCipher cipher = new CustomRouteCipher(r, c);
    String randomMessage = "";
    
    for(int i = 0; i < 18; i++) {
      if(Math.random() < 0.1)
        randomMessage += " ";
      else
        randomMessage += (char)('a' + (int)(Math.random()*26));
    }
    
    String resultMessage = "";
    
    try {
     resultMessage = cipher.encryptMessage(randomMessage); 
    }
    catch(Exception e) {
      System.out.println("Error calling encryptMessage() method");
      System.exit(0);
    }
    
    if(cipher.getNumCols() != c) {
      System.out.println("Calling encryptMessage() should NOT change the numCols instance variable");
      System.exit(0);
    }
    
    if(cipher.getNumRows() != r) {
      System.out.println("Calling encryptMessage() should NOT change the numRows instance variable");
      System.exit(0);
    }
    
    int size = c*r;
    int callsRequired = (int)Math.ceil(randomMessage.length() / (double)size);
    
    if(cipher.fillBlocks.size() < callsRequired) {
      System.out.println("The method fillBlock() is not called enough times within the encryptMessage() method");
      System.exit(0);
    }
    
    if(cipher.encryptBlocks.size() < callsRequired) {
      System.out.println("The method encryptBlock() is not called enough times within the encryptMessage() method");
      System.exit(0);
    }
    
    String undone = "";
    
    while(resultMessage.length() > 0) {
      for(int j = 0; j < r; j++) {
        for(int i = 0; i < c; i++) {
        
          undone += resultMessage.substring(j + i*r, j + i*r + 1);
        }
      }
      
      resultMessage = resultMessage.substring(r*c);
    }
    
    undone = undone.replace("A", "");
    
    if(!undone.equals(randomMessage)) {
      System.out.println("String returned by encryptMessage() is not correct");
      System.exit(0);
    }
  }
}

class CustomRouteCipher extends RouteCipher {
  
  public CustomRouteCipher(int r, int c) {
    Field f = null;
    try {
      f = RouteCipher.class.getDeclaredField("letterBlock");
    }
    catch(Exception e) {
      System.out.println("Unable to access letterBlock instance variable");
      System.exit(0);
    }
    f.setAccessible(true);
    
    try {
      f.set(this, new String[r][c]);
    }
    catch(Exception e) {
      System.out.println("Unable to set letterBlock instance variable");
      System.exit(0);
    }
    
    f = null;
    try {
      f = RouteCipher.class.getDeclaredField("numRows");
    }
    catch(Exception e) {
      System.out.println("Unable to access numRows instance variable");
      System.exit(0);
    }
    f.setAccessible(true);
    
    try {
      f.set(this, r);
    }
    catch(Exception e) {
      System.out.println("Unable to set numRows instance variable");
      System.exit(0);
    }
    
    f = null;
    try {
      f = RouteCipher.class.getDeclaredField("numCols");
    }
    catch(Exception e) {
      System.out.println("Unable to access numCols instance variable");
      System.exit(0);
    }
    f.setAccessible(true);
    
    try {
      f.set(this, c);
    }
    catch(Exception e) {
      System.out.println("Unable to set numCols instance variable");
      System.exit(0);
    }
  }
  
  public String[][] getLetterBlock() {
    Field f = null;
    try {
      f = RouteCipher.class.getDeclaredField("letterBlock");
    }
    catch(Exception e) {
      System.out.println("Unable to access letterBlock instance variable");
      System.exit(0);
    }
    f.setAccessible(true);
    
    String[][] result = null;
    
    try {
      result = (String[][])f.get(this);
    }
    catch(Exception e) {
      System.out.println("Unable to access letterBlock instance variable");
      System.exit(0);
    }
    
    return result;
  }
  
  
  public int getNumRows() {
    Field f = null;
    try {
      f = RouteCipher.class.getDeclaredField("numRows");
    }
    catch(Exception e) {
      System.out.println("Unable to access numRows instance variable");
      System.exit(0);
    }
    f.setAccessible(true);
    
    int result = 0;
    
    try {
      result = (Integer)f.get(this);
    }
    catch(Exception e) {
      System.out.println("Unable to access numRows instance variable");
      System.exit(0);
    }
    
    return result;
  }
  
  public int getNumCols() {
    Field f = null;
    try {
      f = RouteCipher.class.getDeclaredField("numCols");
    }
    catch(Exception e) {
      System.out.println("Unable to access numCols instance variable");
      System.exit(0);
    }
    f.setAccessible(true);
    
    int result = 0;
    
    try {
      result = (Integer)f.get(this);
    }
    catch(Exception e) {
      System.out.println("Unable to access numCols instance variable");
      System.exit(0);
    }
    
    return result;
  }
  
  ArrayList<String> fillBlocks = new ArrayList<String>();
  ArrayList<String> encryptBlocks = new ArrayList<String>();
  
  public void fillBlock(String str) {
    fillBlocks.add(str);
    super.fillBlock(str);
  }
  
  
  public String encryptBlock() {
    String str = "";
    String[][] letterBlock = getLetterBlock();
    
    for(int c = 0; c < letterBlock[0].length; c++) {
      for(int r = 0; r < letterBlock.length; r++) {
        str += letterBlock[r][c];
      }
    }
    
    encryptBlocks.add(str);
    
    return str;
  }
}