-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBCEncryption.java
285 lines (239 loc) · 10.7 KB
/
CBCEncryption.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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;
import java.util.Random;
import java.util.Scanner;
class CleartextCipher implements BlockCipher64 {
public long encrypt(long block) {
return block;
}
public long decrypt(long block) {
return block;
}
}
public class CBCEncryption {
public static final Charset utf8 = StandardCharsets.UTF_8;
public static final Random rng = new SecureRandom();
public static byte[] stringToBytes(String s) {
return s.getBytes(utf8);
}
public static String bytesToString(byte[] b) {
return new String(b, utf8);
}
public static long randomIV() {
// code here
return 0;
}
public static String longToHexString(long x) {
return Long.toHexString(x);
}
public static long hexStringToLong(String hex) {
return new BigInteger(hex, 16).longValue();
}
public static boolean containsMatch(String s, String r) {
return s.matches(String.format("(?i).*%s.*", r));
}
public static void main(String[] args) throws InvalidKeyException, FileNotFoundException {
if(containsMatch(args[0], "partial"))
partialBlockSample();
if(containsMatch(args[0], "even"))
evenBlockSample();
if(containsMatch(args[0], "file"))
fileSample(args[1], args[2]);
if(containsMatch(args[0], "encrypt"))
encryptFileBlowfish(stringToBytes(args[1]), args[2], args[3]);
if(containsMatch(args[0], "decrypt"))
decryptFileBlowfish(stringToBytes(args[1]), args[2], args[3]);
}
public static void fileSample(String keystring, String plaintext_filename) throws InvalidKeyException, FileNotFoundException {
byte[] key = stringToBytes(keystring);
String ciphertext_filename = plaintext_filename + ".secret.txt";
String decrypted_filename = ciphertext_filename + ".opened.txt";
encryptFileBlowfish(key, plaintext_filename, ciphertext_filename);
decryptFileBlowfish(key, ciphertext_filename, decrypted_filename);
}
public static void partialBlockSample() throws InvalidKeyException, FileNotFoundException {
String s = "abcdefghIJKLMNOP12";
byte[] plaintext = stringToBytes(s); // opposite is bytesToString
System.out.println("plaintext = " + Arrays.toString(plaintext));
long firstBlock = longAt(plaintext, 0);
long secondBlock = longAt(plaintext, 8);
System.out.println("firstBlock = " +
Arrays.toString(BlockCipher64.longToBytes(firstBlock))); // opposite is BlockCipher64.bytesToLong
System.out.println("secondBlock = " +
Arrays.toString(BlockCipher64.longToBytes(secondBlock)));
System.out.println("firstBlock ^ secondBlock = " +
Arrays.toString(BlockCipher64.longToBytes(firstBlock ^ secondBlock)));
long iv = 0; // this makes it easier for us to see what's going on
BlockCipher64 cipher = new CleartextCipher(); // same here
byte[] ciphertext = encrypt(plaintext, cipher, iv);
System.out.println("ciphertext = " + Arrays.toString(ciphertext));
byte[] decrypted = decrypt(ciphertext, cipher, iv);
System.out.println("decrypted = " + Arrays.toString(decrypted));
System.out.println("bytesToString(decrypted) = " + bytesToString(decrypted));
}
public static void evenBlockSample() throws InvalidKeyException, FileNotFoundException {
String s = "abcdefghIJKLMNOP";
byte[] plaintext = stringToBytes(s); // opposite is bytesToString
System.out.println("plaintext = " + Arrays.toString(plaintext));
long firstBlock = longAt(plaintext, 0);
long secondBlock = longAt(plaintext, 8);
System.out.println("firstBlock = " +
Arrays.toString(BlockCipher64.longToBytes(firstBlock))); // opposite is BlockCipher64.bytesToLong
System.out.println("secondBlock = " +
Arrays.toString(BlockCipher64.longToBytes(secondBlock)));
System.out.println("firstBlock ^ secondBlock = " +
Arrays.toString(BlockCipher64.longToBytes(firstBlock ^ secondBlock)));
long iv = 0; // this makes it easier for us to see what's going on
BlockCipher64 cipher = new CleartextCipher(); // same here
byte[] ciphertext = encrypt(plaintext, cipher, iv);
System.out.println("ciphertext = " + Arrays.toString(ciphertext));
byte[] decrypted = decrypt(ciphertext, cipher, iv);
System.out.println("decrypted = " + Arrays.toString(decrypted));
System.out.println("bytesToString(decrypted) = " + bytesToString(decrypted));
}
public static long longAt(byte[] b, int pos) {
return BlockCipher64.bytesToLong(Arrays.copyOfRange(b, pos, pos + 8));
}
public static void storeLongAt(byte[] b, long x, int pos) {
byte[] c = BlockCipher64.longToBytes(x);
for(int i = 0; i < 8 && (pos + i) < b.length; ++i) {
b[pos + i] = c[i];
}
}
public static byte[] encrypt(String plaintext, BlockCipher64 cipher, long IV) {
return encrypt(stringToBytes(plaintext), cipher, IV);
}
/*
Try making a message of 18 characters, printing out the bytes (stringToBytes and Arrays.toString),
and printing out the bytes of the encrypt result using CleartextCipher and an IV of zero.
How did the blocks change? Which blocks changed? Does the result make sense
given the picture of how ciphertext stealing and chaining is supposed to work?
Can you calculate what the first block xor'ed with the second block is?
*/
public static byte[] encrypt(byte[] plaintext, BlockCipher64 cipher, long IV) {
if(plaintext.length <= 8)
throw new IllegalArgumentException("plaintext must be longer than 8 bytes!");
byte[] ciphertext = new byte[plaintext.length];
int blocks = plaintext.length / 8;
if(plaintext.length % 8 != 0) ++blocks;
long prev = IV;
for(int block = 0; block < blocks; ++block) {
prev = cipher.encrypt(prev ^ longAt(plaintext, block * 8));
storeLongAt(ciphertext, prev, block * 8);
}
// copy penultimate to last, then prev to penultimate (ciphertext stealing)
int lastBlock = (blocks - 1) * 8;
int secondLastBlock = (blocks - 2) * 8;
storeLongAt(ciphertext, longAt(ciphertext, secondLastBlock), lastBlock);
storeLongAt(ciphertext, prev, secondLastBlock);
return ciphertext;
}
public static long maskForLastNBytes(int n) {
long result = 0;
for(int i = 0; i < n; ++i) {
result <<= 8;
result |= 0xffL; // notice the L
}
return result;
}
public static long maskForFirstNBytes(int n) {
// code here
return 0;
}
public static long firstNBytesOfXRestFromY(int n, long x, long y) {
// code here
return 0;
}
public static byte[] decrypt(byte[] ciphertext, BlockCipher64 cipher, long IV) {
// code here
// check for an illegal argument
// create a byte[] for the plaintext
// calculate how many blocks there are
// handle the last two blocks (which are special because of ciphertext stealing)
// loop over all other blocks, decrypting and xor'ing, and saving the results
return stringToBytes("hello");
}
public static String gatherMessage(Scanner sc) {
StringBuilder sb = new StringBuilder();
String line = sc.nextLine();
while(!line.startsWith(">>>")) {
sb.append(line);
sb.append('\n');
line = sc.nextLine();
}
sb.deleteCharAt(sb.length() - 1);
return sb.toString();
}
public static String gatherBase64(Scanner sc) {
StringBuilder sb = new StringBuilder();
String line = sc.nextLine();
// code here (in the while condition)
while(rng.nextInt() > 0 /* this is wrong. what condition goes here instead? */) {
sb.append(line);
line = sc.nextLine();
}
return sb.toString();
}
public static String breakInto64CharLines(String message) {
StringBuilder sb = new StringBuilder();
int count = 0;
for(char c : message.toCharArray()) {
if(count == 64) {
// code here
// what should happen to count here?
sb.append('\n');
}
sb.append(c);
++count;
}
return sb.toString();
}
/*
Try encrypting the sample file with some key (remember the helper stringToBytes).
Call this method in main with the file names. Check out the result file.
*/
public static void encryptFileBlowfish(byte[] key, String input_filename, String output_filename)
throws InvalidKeyException, FileNotFoundException {
try (Scanner sc = new Scanner(new File(input_filename));
PrintWriter write = new PrintWriter(output_filename)) {
while(sc.hasNextLine()) {
String line = sc.nextLine();
if(!line.startsWith("<<<"))
write.println(line);
else {
long iv = randomIV();
String message = gatherMessage(sc);
byte[] ciphertext = encrypt(message, new Blowfish(key), iv);
String encoded = Base64.getEncoder().encodeToString(ciphertext);
write.printf("<<< Base64 encoding of 16 round Blowfish in CBC " +
"mode with ciphertext stealing. IV:%s\n", longToHexString(iv));
write.println(breakInto64CharLines(encoded));
write.println(">>>");
}
}
}
}
/*
This method should reverse the effect of encryptFileBlowfish. The result should reproduce
the original file before encryption.
*/
public static void decryptFileBlowfish(byte[] key, String input_filename, String output_filename)
throws InvalidKeyException, FileNotFoundException {
// code here
// start by copy pasting encryptFile
// when you find a line starting with "<<<"
// get the IV
// gather the encoded text
// decode and decrypt it
// turn the bytes back into a string
// write out the <<< line, the message, and the >>> line
}
}