-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS-DES.cpp
373 lines (338 loc) · 10.2 KB
/
S-DES.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <iostream>
using namespace std;
//Functions
void printArray(int arr[],int n)
{
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
void Permutation(int arr[], int index[], int n){
int temp[n];
for (int i=0; i<n; i++)
temp[i] = arr[index[i]-1];
for (int i=0; i<n; i++)
arr[i] = temp[i];
}
void ExPermutation(int arr[], int index[], int arr2[], int n){
for (int i=0; i<n; i++)
arr2[i] = arr[index[i]-1];
}
void Split(int arr[], int n, int *l, int *r){
for(int i=0;i<n/2;i++)
l[i] = arr[i];
for(int j=0,i=n/2;i<n;i++,j++)
r[j] = arr[i];
}
int bin2dec(int arr[],int size){
int decimal = 0 ;
for(int i = 0 ; i < size ; i++)
decimal = (decimal << 1) + arr[i] ;
return decimal;
}
void dec2bin(int opSn, int *ar){
int i=0;
while(opSn!=0)
{
ar[i] = opSn%2;
i++;
opSn = opSn/2;
}
}
void combine(int arr1[], int arr2[], int *arr3, int n){
for (int i=0;i<n/2;i++)
arr3[i]=arr1[i];
for (int i=n/2,j=0;i<n;i++,j++)
arr3[i]=arr2[j];
}
void S_box(int a[],int b[],int *opS0S1){
int S0[4][4] = {{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}}, S1[4][4] = {{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
//S0
int rowS0bin[2] = {a[0],a[3]}, colS0bin[2] = {a[1],a[2]};
int rowS0dec = bin2dec(rowS0bin,2), colS0dec = bin2dec(colS0bin,2);
int opS0dec = S0[rowS0dec][colS0dec];
int opS0bin[2]={};
dec2bin(opS0dec, opS0bin);
//S1
int rowS1bin[2] = {b[0],b[3]}, colS1bin[2] = {b[1],b[2]};
int rowS1dec = bin2dec(rowS1bin,2), colS1dec = bin2dec(colS1bin,2);
int opS1dec = S1[rowS1dec][colS1dec];
int opS1bin[2]={};
dec2bin(opS1dec, opS1bin);
for (int i=0;i<2;i++)
opS0S1[i]=opS0bin[i];
for (int i=2,j=0;i<4;i++,j++)
opS0S1[i]=opS1bin[j];
cout<<"After S-Box: ";
printArray(opS0S1,4);
cout<<endl;
}
void Swap(int *left_array, int *right_array, int n){
int temp[n];
for (int i=0; i<n; i++)
temp[i] = left_array[i];
for (int i=0; i<n; i++)
left_array[i]= right_array[i];
for (int i=0; i<n; i++)
right_array[i]= temp[i];
}
void XOR(int arr1[],int arr2[],int n){
int temp[n];
for(int i=0; i<n; i++)
{
temp[i] = arr1[i] ^ arr2[i];
}
for (int i=0; i<n; i++)
arr2[i] = temp[i];
}
void leftRotate(int arr[], int d, int n)
{
int temp[d];
for (int i=0; i<d; i++)
temp[i] = arr[i];
for (int i = 0; i < n-d; i++)
arr[i] = arr[i+d];
for (int i=n-d,j=0; i<n; i++,j++)
arr[i]=temp[j];
}
class KeyGeneration {
private:
int P10_rule[10] = {3,5,2,7,4,10,1,9,8,6};
int P8_rule[8] = {6,3,7,4,8,5,10,9};
int temp_left[5]={}, temp_right[5]={};
public:
KeyGeneration(){
cout<<endl;
cout<<"KEY GENERATION.."<<endl;
cout<<endl;
}
void key(int master_key[], int *k1, int *k2){
//P10
Permutation(master_key,P10_rule,10);
cout<<"After P10 Permutation: ";
printArray(master_key,10);
cout<<endl;
//Split
Split(master_key,10,temp_left,temp_right);
cout<<"After split, "<<endl;
cout<<"l = ";
printArray(temp_left,5);
cout<<"r = " ;
printArray(temp_right,5);
cout<<endl;
//LS-1
leftRotate(temp_left,1,5);
leftRotate(temp_right,1,5);
cout<<"After LeftShift-1, "<<endl;
cout<<"l = ";
printArray(temp_left,5);
cout<<"r = ";
printArray(temp_right,5);
cout<<endl;
//P-8
combine(temp_left,temp_right,master_key,10);
Permutation(master_key,P8_rule,10);
cout<<"After P8, Key-1: ";
for(int i=0;i<8;i++)
k1[i]=master_key[i];
printArray(k1,8);
cout<<endl;
//LS-2
leftRotate(temp_left,2,5);
leftRotate(temp_right,2,5);
cout<<"After LeftShift-2, "<<endl;
cout<<"l = ";
printArray(temp_left,5);
cout<<"r = ";
printArray(temp_right,5);
cout<<endl;
//P-8
combine(temp_left,temp_right,master_key,10);
Permutation(master_key,P8_rule,10);
cout<<"After P8, Key-2: ";
for(int i=0;i<8;i++)
k2[i]=master_key[i];
printArray(k2,8);
}
};
class Roundfunction{
private:
int Expanrule[8] = {4,1,2,3,2,3,4,1};
int P4_rule[4] = {2,4,3,1};
int r_arr2[8]={},a[4]={},b[4]={};
int opS0S1[4]={};
public:
void roundfun(int *k1,int *l_arr, int *r_arr, int *fk1){
ExPermutation(r_arr, Expanrule, r_arr2,8);
cout<<"After EP: ";
printArray(r_arr2,8);
cout<<endl;
//XOR with K1
XOR(k1,r_arr2,8);
cout<<"XOR with key"<<endl;
printArray(r_arr2,8);
cout<<endl;
//Split
Split(r_arr2,8,a,b);
cout<<"After Split"<<endl;
cout<<"l = ";
printArray(a,4);
cout<<"r = ";
printArray(b,4);
cout<<endl;
//Sbox
S_box(a,b,opS0S1);
//P4
Permutation(opS0S1,P4_rule,4);
cout<<"After P4"<<endl;
printArray(opS0S1,4);
cout<<endl;
//XOR with left array
XOR(opS0S1,l_arr,4);
cout<<"XOR with leftarray"<<endl;
printArray(l_arr,4);
cout<<endl;
//combine
combine(l_arr,r_arr,fk1,8);
cout<<"After combine"<<endl;
printArray(fk1,8);
cout<<endl;
}
};
class encrypt : public Roundfunction{
private:
int IP_rule[8] = {2,6,3,1,4,8,5,7};
int IP_inv_rule[8] = {4,1,3,5,7,2,8,6};
int fkk[8]={};
public:
encrypt(){
cout<<endl;
cout<<"ENCRYPTING.."<<endl;
cout<<endl;
}
int l_arr[4]={},r_arr[4]={};
//IP
void enc(int arr[], int *key1, int *key2, int *fk1){
Permutation(arr, IP_rule, 8);
cout<<"After IP: ";
printArray(arr,8);
cout<<endl;
//Split
Split(arr,8, l_arr,r_arr);
cout<<"After split, "<<endl;
cout<<"l = ";
printArray(l_arr,4);
cout<<"r = " ;
printArray(r_arr,4);
cout<<endl;
//fk1
cout<<"Round Function(fk)-1"<<endl;
Roundfunction::roundfun(key1,l_arr,r_arr,fk1);
//Swap
Swap(l_arr,r_arr,4);
cout<<"After Swap"<<endl;
cout<<"l = ";
printArray(l_arr,4);
cout<<"r = " ;
printArray(r_arr,4);
cout<<endl;
//fk2
cout<<"Round Function(fk)-2"<<endl;
Roundfunction::roundfun(key2,l_arr,r_arr,fk1);
//ipinv
Permutation(fk1,IP_inv_rule,8);
cout<<"After IP-Inverse, 8-bit Cipher Text is: "<<endl;
printArray(fk1,8);
}
};
class decrypt : public Roundfunction{
private:
int IP_rule[8] = {2,6,3,1,4,8,5,7};
int IP_inv_rule[8] = {4,1,3,5,7,2,8,6};
int fkk[8]={};
public:
int l_arr[4]={},r_arr[4]={};
//IP
void decryp(int arr[], int *key1, int *key2, int *fk1){
Permutation(arr, IP_rule, 8);
cout<<"IP"<<endl;
printArray(arr,8);
//Split
Split(arr,8, l_arr,r_arr);
cout<<"Split"<<endl;
printArray(l_arr,4);
printArray(r_arr,4);
//fk1
Roundfunction::roundfun(key2,l_arr,r_arr,fk1);
//Swap
Swap(l_arr,r_arr,4);
cout<<"swap"<<endl;
printArray(l_arr,4);
printArray(r_arr,4);
//fk2
Roundfunction::roundfun(key1,l_arr,r_arr,fk1);
//ipinv
Permutation(fk1,IP_inv_rule,8);
cout<<"After IP-Inverse, 8-bit Plain Text is: "<<endl;
printArray(fk1,8);
}
};
int main()
{
char input;
int arr[8]={};
int master_key[10]={};
int k1[8]={},k2[8]={};
int fk1[8] = {};
//Key
cout<<"Enter 10-bit Master Key (using space)"<<endl;
for(int i=0;i<10;i++){
cin>>master_key[i];
}
if((sizeof(master_key)/sizeof(master_key[0]))!=10)
throw "Error. Enter 10-bits";
KeyGeneration k;
k.key(master_key,k1,k2);
cout<<"_____________________________________________________________________________"<<endl;
cout<<endl;
cout<<"Enter e for Encryption | Enter d for Decryption | Enter b for Both"<<endl;
cin>>input;
if (input == 'b'||input == 'B'){
cout<<"Enter 8-bit Plain Text (using space)"<<endl;
for(int i=0;i<8;i++){
cin>>arr[i];
}
if((sizeof(arr)/sizeof(arr[0]))!=8)
throw "Error. Enter 8-bits";
encrypt e;
e.enc(arr,k1,k2,fk1);
for(int i=0;i<8;i++)
arr[i] = fk1[i];
cout<<"_____________________________________________________________________________"<<endl;
decrypt d;
d.decryp(arr,k1,k2,fk1);
}
else if (input == 'e'||input == 'E'){
cout<<"Enter 8-bit Plain Text (using space)"<<endl;
for(int i=0;i<8;i++){
cin>>arr[i];
}
if((sizeof(arr)/sizeof(arr[0]))!=8)
throw "Error. Enter 8-bits";
encrypt e;
e.enc(arr,k1,k2,fk1);
}
else if (input == 'd'||input == 'D'){
cout<<"Enter 8-bit Cipher Text (using space)"<<endl;
for(int i=0;i<8;i++){
cin>>arr[i];
}
if((sizeof(arr)/sizeof(arr[0]))!=8)
throw "Error. Enter 8-bits";
decrypt d;
d.decryp(arr,k1,k2,fk1);
}
else
throw "Error, Choose correct option";
return 0;
}