|
@@ -0,0 +1,231 @@
|
|
1
|
+import org.junit.Test;
|
|
2
|
+
|
|
3
|
+import javax.crypto.BadPaddingException;
|
|
4
|
+import javax.crypto.IllegalBlockSizeException;
|
|
5
|
+import javax.crypto.NoSuchPaddingException;
|
|
6
|
+import java.io.IOException;
|
|
7
|
+import java.security.InvalidKeyException;
|
|
8
|
+import java.security.NoSuchAlgorithmException;
|
|
9
|
+import java.security.NoSuchProviderException;
|
|
10
|
+
|
|
11
|
+import static org.junit.Assert.*;
|
|
12
|
+
|
|
13
|
+public class ROT13Test {
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+ @Test
|
|
17
|
+ public void rotateStringTest0() {
|
|
18
|
+ // Given
|
|
19
|
+ String s1 = "ABCDEF";
|
|
20
|
+ String s2 = "ABCDEF";
|
|
21
|
+
|
|
22
|
+ // When
|
|
23
|
+ ROT13 cipher = new ROT13();
|
|
24
|
+ String actual = cipher.rotate(s1, 'A');
|
|
25
|
+ System.out.println(s1);
|
|
26
|
+ System.out.println(actual);
|
|
27
|
+
|
|
28
|
+ // Then
|
|
29
|
+ assertTrue(actual.equals(s2));
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ @Test
|
|
33
|
+ public void rotateStringTest1() {
|
|
34
|
+ // Given
|
|
35
|
+ String s1 = "ABCDEF";
|
|
36
|
+ String s2 = "DEFABC";
|
|
37
|
+
|
|
38
|
+ // When
|
|
39
|
+ ROT13 cipher = new ROT13();
|
|
40
|
+ String actual = cipher.rotate(s1, 'D');
|
|
41
|
+ System.out.println(s1);
|
|
42
|
+ System.out.println(actual);
|
|
43
|
+
|
|
44
|
+ // Then
|
|
45
|
+ assertTrue(actual.equals(s2));
|
|
46
|
+ }
|
|
47
|
+
|
|
48
|
+ @Test
|
|
49
|
+ public void rotateStringTest2() {
|
|
50
|
+ // Given
|
|
51
|
+ String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
52
|
+ String s2 = "NOPQRSTUVWXYZABCDEFGHIJKLM";
|
|
53
|
+
|
|
54
|
+ // When
|
|
55
|
+ ROT13 cipher = new ROT13();
|
|
56
|
+ String actual = cipher.rotate(s1, 'N');
|
|
57
|
+ System.out.println(s1);
|
|
58
|
+ System.out.println(actual);
|
|
59
|
+ // Then
|
|
60
|
+ assertTrue(actual.equals(s2));
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ @Test
|
|
64
|
+ public void cryptTest1() {
|
|
65
|
+ // Given
|
|
66
|
+ ROT13 cipher = new ROT13('a', 'n');
|
|
67
|
+
|
|
68
|
+ String Q1 = "Why did the chicken cross the road?";
|
|
69
|
+ String A1 = "Jul qvq gur puvpxra pebff gur ebnq?";
|
|
70
|
+
|
|
71
|
+ String Q2 = "Gb trg gb gur bgure fvqr!";
|
|
72
|
+ String A2 = "To get to the other side!";
|
|
73
|
+
|
|
74
|
+ // When
|
|
75
|
+ String actual = cipher.encrypt(Q1);
|
|
76
|
+ System.out.println(Q1);
|
|
77
|
+ System.out.println(A1);
|
|
78
|
+ System.out.println(actual);
|
|
79
|
+ // Then
|
|
80
|
+ assertTrue(actual.equals(A1));
|
|
81
|
+
|
|
82
|
+ // When
|
|
83
|
+ String actual2 = cipher.decrypt(Q2);
|
|
84
|
+ System.out.println(Q2);
|
|
85
|
+ System.out.println(A2);
|
|
86
|
+ System.out.println(actual2);
|
|
87
|
+ // Then
|
|
88
|
+ assertTrue(actual2.equals(A2));
|
|
89
|
+ }
|
|
90
|
+ @Test
|
|
91
|
+ public void cryptTest2() {
|
|
92
|
+ // Given
|
|
93
|
+ ROT13 cipher = new ROT13('a', 'n');
|
|
94
|
+
|
|
95
|
+ String Q1 = "Why did the chicken cross the road?";
|
|
96
|
+ System.out.println(Q1);
|
|
97
|
+
|
|
98
|
+ // When
|
|
99
|
+ String actual = cipher.crypt(cipher.crypt(Q1));
|
|
100
|
+ System.out.println(actual);
|
|
101
|
+ // Then
|
|
102
|
+ assertTrue(actual.equals(Q1));
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ @Test
|
|
106
|
+ public void cryptTxtFileTest() throws IOException {
|
|
107
|
+ // Given
|
|
108
|
+ ROT13 cipher = new ROT13('a', 'n');
|
|
109
|
+
|
|
110
|
+ String expected = "Shall I compare thee to a summer’s day?\n" +
|
|
111
|
+ "Thou art more lovely and more temperate:\n" +
|
|
112
|
+ "Rough winds do shake the darling buds of May,\n" +
|
|
113
|
+ "And summer’s lease hath all too short a date;\n" +
|
|
114
|
+ "Sometime too hot the eye of heaven shines,\n" +
|
|
115
|
+ "And often is his gold complexion dimm'd;\n" +
|
|
116
|
+ "And every fair from fair sometime declines,\n" +
|
|
117
|
+ "By chance or nature’s changing course untrimm'd;\n" +
|
|
118
|
+ "But thy eternal summer shall not fade,\n" +
|
|
119
|
+ "Nor lose possession of that fair thou ow’st;\n" +
|
|
120
|
+ "Nor shall death brag thou wander’st in his shade,\n" +
|
|
121
|
+ "When in eternal lines to time thou grow’st:\n" +
|
|
122
|
+ " So long as men can breathe or eyes can see,\n" +
|
|
123
|
+ " So long lives this, and this gives life to thee.\n";
|
|
124
|
+ System.out.println(expected);
|
|
125
|
+
|
|
126
|
+ // When
|
|
127
|
+ String actualE = cipher.cryptFile("sonnet18.txt");
|
|
128
|
+ String actual = cipher.decrypt(actualE);
|
|
129
|
+ System.out.println(actual);
|
|
130
|
+ // Then
|
|
131
|
+ assertTrue(actual.equals(expected));
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ @Test
|
|
135
|
+ public void readFileWriteFileTest() throws IOException {
|
|
136
|
+ // Given
|
|
137
|
+ ROT13 cipher = new ROT13('a', 'n');
|
|
138
|
+
|
|
139
|
+ String expected = "Shall I compare thee to a summer’s day?\n" +
|
|
140
|
+ "Thou art more lovely and more temperate:\n" +
|
|
141
|
+ "Rough winds do shake the darling buds of May,\n" +
|
|
142
|
+ "And summer’s lease hath all too short a date;\n" +
|
|
143
|
+ "Sometime too hot the eye of heaven shines,\n" +
|
|
144
|
+ "And often is his gold complexion dimm'd;\n" +
|
|
145
|
+ "And every fair from fair sometime declines,\n" +
|
|
146
|
+ "By chance or nature’s changing course untrimm'd;\n" +
|
|
147
|
+ "But thy eternal summer shall not fade,\n" +
|
|
148
|
+ "Nor lose possession of that fair thou ow’st;\n" +
|
|
149
|
+ "Nor shall death brag thou wander’st in his shade,\n" +
|
|
150
|
+ "When in eternal lines to time thou grow’st:\n" +
|
|
151
|
+ " So long as men can breathe or eyes can see,\n" +
|
|
152
|
+ " So long lives this, and this gives life to thee.\n";
|
|
153
|
+ System.out.println(expected);
|
|
154
|
+
|
|
155
|
+ cipher.writeFile("sonnet18e.txt", expected);
|
|
156
|
+ String actual = cipher.readFileToString("sonnet18e.txt");
|
|
157
|
+
|
|
158
|
+ assertTrue(actual.equals(expected));
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ @Test
|
|
162
|
+ public void cryptTxtFileWriteTest() throws IOException {
|
|
163
|
+ // Given
|
|
164
|
+ ROT13 cipher = new ROT13('a', 'n');
|
|
165
|
+
|
|
166
|
+ String expected = "Shall I compare thee to a summer’s day?\n" +
|
|
167
|
+ "Thou art more lovely and more temperate:\n" +
|
|
168
|
+ "Rough winds do shake the darling buds of May,\n" +
|
|
169
|
+ "And summer’s lease hath all too short a date;\n" +
|
|
170
|
+ "Sometime too hot the eye of heaven shines,\n" +
|
|
171
|
+ "And often is his gold complexion dimm'd;\n" +
|
|
172
|
+ "And every fair from fair sometime declines,\n" +
|
|
173
|
+ "By chance or nature’s changing course untrimm'd;\n" +
|
|
174
|
+ "But thy eternal summer shall not fade,\n" +
|
|
175
|
+ "Nor lose possession of that fair thou ow’st;\n" +
|
|
176
|
+ "Nor shall death brag thou wander’st in his shade,\n" +
|
|
177
|
+ "When in eternal lines to time thou grow’st:\n" +
|
|
178
|
+ " So long as men can breathe or eyes can see,\n" +
|
|
179
|
+ " So long lives this, and this gives life to thee.\n";
|
|
180
|
+ System.out.println(expected);
|
|
181
|
+
|
|
182
|
+ // When
|
|
183
|
+ String actualE = cipher.cryptFile("sonnet18.txt");
|
|
184
|
+ cipher.writeFile("sonnet18e.txt", actualE);
|
|
185
|
+ String actual = cipher.readFileToString("sonnet18e.txt");
|
|
186
|
+ assertTrue(actualE.equals(actual));
|
|
187
|
+
|
|
188
|
+ String original = cipher.decrypt(actual);
|
|
189
|
+ assertTrue(original.equals(expected));
|
|
190
|
+ System.out.println(actual);
|
|
191
|
+ }
|
|
192
|
+
|
|
193
|
+ @Test
|
|
194
|
+ public void testAES() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
|
|
195
|
+ ROT13 cipher = new ROT13('a', 'n');
|
|
196
|
+ try {
|
|
197
|
+ cipher.AES();
|
|
198
|
+ } catch (NoSuchPaddingException e) {
|
|
199
|
+ e.printStackTrace();
|
|
200
|
+ } catch (NoSuchAlgorithmException e) {
|
|
201
|
+ e.printStackTrace();
|
|
202
|
+ } catch (InvalidKeyException e) {
|
|
203
|
+ e.printStackTrace();
|
|
204
|
+ } catch (BadPaddingException e) {
|
|
205
|
+ e.printStackTrace();
|
|
206
|
+ } catch (IllegalBlockSizeException e) {
|
|
207
|
+ e.printStackTrace();
|
|
208
|
+ }
|
|
209
|
+ }
|
|
210
|
+
|
|
211
|
+ @Test
|
|
212
|
+ public void testPublicKeyPrivateKey() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException, InvalidKeyException {
|
|
213
|
+ ROT13 cipher = new ROT13('a', 'n');
|
|
214
|
+ try {
|
|
215
|
+ cipher.publicPrivateKeyPair();
|
|
216
|
+ } catch (NoSuchProviderException e) {
|
|
217
|
+ e.printStackTrace();
|
|
218
|
+ } catch (NoSuchAlgorithmException e) {
|
|
219
|
+ e.printStackTrace();
|
|
220
|
+ } catch (NoSuchPaddingException e) {
|
|
221
|
+ e.printStackTrace();
|
|
222
|
+ } catch (InvalidKeyException e) {
|
|
223
|
+ e.printStackTrace();
|
|
224
|
+ } catch (BadPaddingException e) {
|
|
225
|
+ e.printStackTrace();
|
|
226
|
+ } catch (IllegalBlockSizeException e) {
|
|
227
|
+ e.printStackTrace();
|
|
228
|
+ }
|
|
229
|
+ }
|
|
230
|
+
|
|
231
|
+}
|