-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.cpp
354 lines (248 loc) · 7.17 KB
/
encrypt.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <iostream>
#include <fstream>
#include <string>
#include <cstdint>
#include <cmath>
#include "encrypt.h"
/********* Functions *********/
/*** Input Related ***/
u_int16_t ReadKey(const char* hexInput)
{
u_int16_t key = 0x0;
// This is just the string version of the char array. Ease of use with built in string functions
std::string strHex = hexInput;
key = stoi(strHex, 0, 16); // Starting at index 0. 16 is the base (hex)
return key;
}
/**
* @brief Reads in all of the contents of a file and places it into a std::string
*
* @param file Filename of the file to read in
* @return std::string The input of the file
*/
std::string ReadFile(const char* file)
{
std::string input = "";
std::ifstream inStream;
// Open the file and fail if it doesn't exist
inStream.open(file);
if(inStream.fail())
{
perror(file);
exit(1);
}
// Read in each line and keep the newlines
std::string currLine = "";
while(std::getline(inStream, currLine))
{
input += currLine;
input += '\n';
}
return input;
}
void KeyGeneration(u_int16_t key, char& k1, char& k2)
{
/* Generate Key 1 */
// Permutate the key
key = PermP10(key);
std::cerr << "p10: " << key << std::endl;
// The binary representation of the 10 bit key
int binaryKey[10];
ToBinaryArr(binaryKey, key, 10);
// The left half of the binary key
int left[5];
// The right half of the binary key
int right[5];
SplitArr(binaryKey, left, right, 10);
// Shift right and left by 1
LeftShift(left, 5);
LeftShift(right, 5);
// Combine the arrays to send to p10
int shiftedkey[10];
CombineArrs(shiftedkey, left, right, 10);
u_int16_t key1 = PermP8(ToInt(shiftedkey, 10));
std::cerr << "k1: " << key1 << std::endl;
/* Generate Key 2 */
// Shift right and left by 2
LeftShift(left, 5);
LeftShift(left, 5);
LeftShift(right, 5);
LeftShift(right, 5);
// Combine the arrays to send to p10
CombineArrs(shiftedkey, left, right, 10);
u_int16_t key2 = PermP8(ToInt(shiftedkey, 10));
std::cerr << "k2: " << key2 << std::endl;
k1 = key1;
k2 = key2;
}
void EncryptByte(unsigned char input, char key1, char key2, int count)
{
/* Permutate input */
std::cerr<< "encrypting byte #" << count << " with value " << int(input) << std::endl;
input = PermIP(input);
std::cerr << "ip: " << int(input) << std::endl;
/* Send Key 1 to Encryption */
int key1Binary[8];
ToBinaryArr(key1Binary, key1, 8);
input = Feistal(input, key1);
/* SW */
int binary[8];
ToBinaryArr(binary, input, 8);
Swap(binary, 8);
input = ToInt(binary, 8);
std::cerr << "fk1: " << int(input) << std::endl;
/* Send Key 2 to Encryption */
input = Feistal(input, key2);
std::cerr << "fk2: " << int(input) << std::endl;
/* Give to final permutation */
input = PermIPn(input);
std::cout << input;
fflush(stdout);
}
u_int16_t Feistal(unsigned char input, u_int16_t key)
{
/* Convert to binary and split */
int binary[8];
int left[4];
int right[4];
ToBinaryArr(binary, input, 8);
SplitArr(binary, left, right, 8);
/* Expand R from 4->8bits */
unsigned char rightVal;
rightVal = ToInt(right, 4);
rightVal = Expansion(rightVal);
/* XOR the Expanded Right side and the Key */
unsigned char xored = rightVal^key;
/* Substitution */
int binSubstituion[8];
ToBinaryArr(binSubstituion, xored, 8);
int LSub[4];
int RSub[4];
SplitArr(binSubstituion, LSub, RSub, 8);
int Sub[4];
int LRow = LSub[0]*2 + LSub[3];
int LCol = LSub[1]*2 + LSub[2];
int LVal = S0[LRow][LCol];
Sub[0] = LVal > 1; // Grab the left bit
Sub[1] = LVal & 1; // Grab the right bit
int RRow = RSub[0]*2 + RSub[3];
int RCol = RSub[1]*2 + RSub[2];
int RVal = S1[RRow][RCol];
Sub[2] = RVal > 1; // Grab the left bit
Sub[3] = RVal & 1; // Grab the right bit
/* Permute 4 */
int subbed = ToInt(Sub, 4);
subbed = PermP4(subbed);
/* XOR Left with Subbed for output */
unsigned char leftnibble = ToInt(left, 4);
u_int16_t output = leftnibble^subbed;
int temp[4];
ToBinaryArr(temp, output, 4);
/* Combine the left and the right for the final */
output = output * 0x10 + ToInt(right, 4);
return output;
}
/*** Binary Actions ***/
void ToBinaryArr(int binary[], u_int16_t val, int size)
{
// The binary array that is created from char
// Hard coded size of 16 because there are no values that should be above 16 bits
size = size-1;
for(int i = 0; i <= size; i++)
binary[i] = (val >> size-i) & 1;
return;
}
u_int16_t ToInt(int binary[], int size = 8)
{
u_int16_t val = 0;
for(int i = 0; i < size; i++)
val += binary[i] * pow(2, (size - (i+1)));
return val;
}
void LeftShift(int val[], int size = 8)
{
// Save the first element to wrap around
int temp = val[0];
for(int i = 0; i < size-1; i++)
{
val[i] = val[i+1];
}
val[size-1] = temp;
return;
}
void SplitArr(int binary[], int left[], int right[], int size)
{
int split = size / 2;
for(int i = 0; i < split; i++) {
left[i] = binary[i];
right[i] = binary[i + split];
}
return;
}
void CombineArrs( int full[], int left[], int right[], int full_size)
{
int split = full_size / 2;
for (int i = 0; i < split; i++) {
full[i] = left[i];
full[i + split] = right[i];
}
return;
}
void Swap(int binary[], int size)
{
int left[size/2];
int right[size/2];
SplitArr(binary, left, right, size);
CombineArrs(binary, right, left, size);
return;
}
/*** Permutations ***/
/* Actually all of the work */
u_int16_t Permutation(u_int16_t val, int input_size, int permutation_size, int permutation[])
{
// Binary array of length 8 that will be used to accomplish permutation
int binary[input_size];
ToBinaryArr(binary, val, input_size);
// The permutated character in binary array form
int permutated[16];
// std::cout << "Permuting " << val << std::endl;
// for(int i = 0; i < input_size; i++)
// std::cout << binary[i];
// std::cout << std::endl;
for(int i = 0; i < permutation_size; i++)
{
permutated[i] = binary[permutation[i]-1];
// std::cout << "Swapping " << binary[i] << " with " << binary[permutation[i]-1] << std::endl;
}
// The permutated character
u_int16_t permCh = ToInt(permutated, permutation_size);
// std::cout << "Permuted char " << permCh << std::endl;
return permCh;
}
/* Encryption */
unsigned char PermIP(unsigned char ch)
{
return Permutation(ch, 8, 8, IP);
}
unsigned char PermIPn(unsigned char ch)
{
return Permutation(ch, 8, 8, IPn);
}
/* Key Gen */
u_int16_t PermP10(u_int16_t val)
{
return Permutation(val, 10, 10, P10);
}
u_int16_t PermP8(u_int16_t val)
{
return Permutation(val, 10, 8, P8);
}
/* Feistal Function */
u_int16_t Expansion(u_int16_t val)
{
return Permutation(val, 4, 8, EP);
}
u_int16_t PermP4(u_int16_t val)
{
return Permutation(val, 4, 4, P4);
}