-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.cpp
More file actions
315 lines (265 loc) · 8.68 KB
/
cipher.cpp
File metadata and controls
315 lines (265 loc) · 8.68 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
309
310
311
312
313
314
315
/***********************************************************
cipher.cpp
This file contains functions that implement each part of
the Blowfish cipher.
************************************************************/
#include "cipher.h"
#include "subkeys.h"
/*
Performs encryption on an input file using the key provided
and writes the encrypted data to an output file.
*/
void encryptFile(ifstream *inFile, ofstream *outFile, char *key)
{
// run key schedule to set up subkeys
keySchedule((unsigned char*)key);
// produce an MD5 hash of the key to use as an initialization vector
unsigned long digest[MD5_DIGEST_LENGTH];
MD5((unsigned char*)key, strlen(key), (unsigned char*)digest);
unsigned long prev_lblock = digest[0];
unsigned long prev_rblock = digest[1];
// get file size (assumes file opened at end), and start reading from beginning
unsigned long fSize = inFile->tellg();
inFile->seekg(0, inFile->beg);
while (fSize > 0) // encrypt blocks until the file has been read entirely
{
unsigned long lblock;
unsigned long rblock;
if (fSize < BLOCK_SIZE) // file length is not a multiple of the block size; need to add padding
{
lblock = rblock = 0UL;
unsigned long padBytes = BLOCK_SIZE - fSize;
if (fSize < sizeof(long)) // less than half a block of plaintext left
{
inFile->read(reinterpret_cast< char* >(&lblock),fSize);
}
else
{
inFile->read(reinterpret_cast< char* >(&lblock),sizeof(long));
inFile->read(reinterpret_cast< char* >(&rblock),fSize - sizeof(long));
}
// XOR block as per CBC mode
lblock ^= prev_lblock;
rblock ^= prev_rblock;
// encrypt final data block with zero padding
encryptBlock(&lblock, &rblock);
prev_lblock = lblock;
prev_rblock = rblock;
outFile->write( (char*)&lblock, sizeof(long));
outFile->write( (char*)&rblock, sizeof(long));
// add extra block with padding length
lblock = rblock = 0UL;
rblock |= padBytes; // right half is encoded with padding length
// XOR block as per CBC mode
lblock ^= prev_lblock;
rblock ^= prev_rblock;
encryptBlock(&lblock, &rblock);
outFile->write( (char*)&lblock, sizeof(long));
outFile->write( (char*)&rblock, sizeof(long));
break;
}
else if (fSize == BLOCK_SIZE)
{
// encrypt final data block
inFile->read(reinterpret_cast< char* >(&lblock),sizeof(long));
inFile->read(reinterpret_cast< char* >(&rblock),sizeof(long));
// XOR block as per CBC mode
lblock ^= prev_lblock;
rblock ^= prev_rblock;
encryptBlock(&lblock, &rblock);
prev_lblock = lblock;
prev_rblock = rblock;
outFile->write( (char*)&lblock, sizeof(long));
outFile->write( (char*)&rblock, sizeof(long));
// add extra block to indicate there was no padding added
lblock = rblock = 0UL;
// XOR block as per CBC mode
lblock ^= prev_lblock;
rblock ^= prev_rblock;
encryptBlock(&lblock, &rblock);
outFile->write( (char*)&lblock, sizeof(long));
outFile->write( (char*)&rblock, sizeof(long));
break;
}
// read in a block of data and encrypt
inFile->read(reinterpret_cast< char* >(&lblock),sizeof(long));
inFile->read(reinterpret_cast< char* >(&rblock),sizeof(long));
// XOR block as per CBC mode
lblock ^= prev_lblock;
rblock ^= prev_rblock;
encryptBlock(&lblock, &rblock);
prev_lblock = lblock;
prev_rblock = rblock;
// write encrypted block to output file
outFile->write( (char*)&lblock, sizeof(long));
outFile->write( (char*)&rblock, sizeof(long));
fSize -= BLOCK_SIZE;
}
}
/*
Performs decryption on the given input file and writes the
plaintext to the given output file.
*/
void decryptFile(ifstream *inFile, ofstream *outFile, char *key)
{
// run key schedule to set up subkeys
keySchedule((unsigned char*)key);
// produce an MD5 hash of the key to use as an initialization vector
unsigned long digest[MD5_DIGEST_LENGTH];
MD5((unsigned char*)key, strlen(key), (unsigned char*)digest);
unsigned long lcipher = digest[0];
unsigned long rcipher = digest[1];
// get file size (assumes file opened at end), and start reading from beginning
unsigned long fSize = inFile->tellg();
inFile->seekg(0, inFile->beg);
while (fSize > 0) // decrypt blocks until the file has been read entirely
{
unsigned long lblock;
unsigned long rblock;
unsigned long ltemp;
unsigned long rtemp;
if (fSize == 2 * BLOCK_SIZE) // reached the last 2 blocks
{
// read last data block
inFile->read(reinterpret_cast< char* >(&lblock),sizeof(long));
inFile->read(reinterpret_cast< char* >(&rblock),sizeof(long));
// store copy of encrypted block
ltemp = lblock;
rtemp = rblock;
decryptBlock(&lblock, &rblock);
// XOR block as per CBC mode
lblock ^= lcipher;
rblock ^= rcipher;
// read padding block
unsigned long lpad, rpad;
inFile->read(reinterpret_cast< char* >(&lpad),sizeof(long));
inFile->read(reinterpret_cast< char* >(&rpad),sizeof(long));
decryptBlock(&lpad, &rpad);
// XOR block as per CBC mode
lpad ^= ltemp;
rpad ^= rtemp;
// make sure we only write the data (not the padding) to the output file
if (rpad > sizeof(long))
{
outFile->write( (char*)&lblock, (BLOCK_SIZE - rpad));
}
else
{
outFile->write( (char*)&lblock, sizeof(long));
if (rpad != sizeof (long))
outFile->write( (char*)&rblock, (sizeof(long) - rpad));
}
break;
}
// read a block of encrypted data and decrypt
inFile->read(reinterpret_cast< char* >(&lblock),sizeof(long));
inFile->read(reinterpret_cast< char* >(&rblock),sizeof(long));
// store copy of encrypted block
ltemp = lblock;
rtemp = rblock;
decryptBlock(&lblock, &rblock);
// XOR block as per CBC mode
lblock ^= lcipher;
rblock ^= rcipher;
// update ciphertext block for CBC decryption
lcipher = ltemp;
rcipher = rtemp;
// write encrypted block to output file
outFile->write( (char*)&lblock, sizeof(long));
outFile->write( (char*)&rblock, sizeof(long));
fSize -= BLOCK_SIZE;
}
}
/*
Runs the Blowfish key schedule algorithm using the subkey arrays P and S
(declared and initialized in subkeys.h)
*/
void keySchedule(unsigned char *key)
{
int i, j, k;
unsigned long d, dl, dr;
int keyLen = strlen((char*)key);
// XOR P array with key
j = 0;
for (i = 0; i < N+2; ++i)
{
d = 0UL;
for (k = 0; k < 4; ++k)
{
d = (d << 8) | key[j];
j++;
if (j >= keyLen) j = 0;
}
P[i] = P[i]^d;
}
// encryption step to further randomize P array and randomize S boxes
dl=dr=0UL;
for (i = 0; i < N + 2; i += 2)
{
encryptBlock(&dl, &dr);
P[i] = dl;
P[i+1] = dr;
}
for (i = 0; i < 4; ++i)
{
for (j = 0; j < 256; j+=2)
{
encryptBlock(&dl, &dr);
S[i][j] = dl;
S[i][j+1] = dr;
}
}
}
/*
The round function.
*/
unsigned long f(unsigned long x)
{
unsigned short a, b, c, d;
unsigned long y;
d = x & 0x00FF; x >>= 8;
c = x & 0x00FF; x >>= 8;
b = x & 0x00FF; x >>= 8;
a = x & 0x00FF;
y = S[0][a] + S[1][b];
y = y ^ S[2][c];
y = y + S[3][d];
return y;
}
/*
Perform encryption on a 64 bit block (separated into two halves) via Blowfish algorithm.
*/
void encryptBlock(unsigned long *L, unsigned long *R)
{
for (int i=0 ; i<N ; ++i)
{
*L ^= P[i];
*R ^= f(*L);
unsigned long temp = *L;
*L = *R;
*R = temp;
}
unsigned long temp = *L;
*L = *R;
*R = temp;
*R ^= P[N];
*L ^= P[N+1];
}
/*
Perform decryption on a 64 bit block (separated into two halves) via Blowfish algorithm.
*/
void decryptBlock(unsigned long *L, unsigned long *R)
{
for (int i=N+1 ; i > 1 ; --i) {
*L ^= P[i];
*R ^= f(*L);
unsigned long temp = *L;
*L = *R;
*R = temp;
}
unsigned long temp = *L;
*L = *R;
*R = temp;
*R ^= P[1];
*L ^= P[0];
}