-
Notifications
You must be signed in to change notification settings - Fork 0
/
S-DES.java
313 lines (240 loc) · 5.53 KB
/
S-DES.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
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
package cybersecurity;
public class S_DES {
//int key[]= {0,0,1,0,0,1,0,1,1,1};
int key[]= {1,0,1,0,0,0,0,0,1,0}; //extra example for checking perpose
int P10[]= {3,5,2,7,4,10,1,9,8,6};
int P8[]= {6,3,7,4,8,5,10,9};
int key1[]=new int[8];
int key2[]=new int[8];
int []IP= {2,6,3,1,4,8,5,7};
int []EP= {4,1,2,3,2,3,4,1};
int []P4= {2,4,3,1};
int []IP_inv= {4,1,3,5,7,2,8,6};
int[][]S0= {
{1,0,3,2},
{3,2,1,0},
{0,2,1,3},
{3,1,3,2}
};
int[][]S1= {
{0,1,2,3},
{2,0,1,3},
{3,0,1,0},
{2,1,0,3}
};
// this function basically generates the key(key1 and key2)
// using P10 and P8 with (1 and 2)left shifts
void key_generation()
{
int key_[]=new int[10];
for(int i=0;i<10;i++)
{
key_[i]=key[P10[i]-1];
}
int Ls[]=new int[5];
int Rs[]=new int[5];
for(int i=0;i<5;i++)
{
Ls[i]=key_[i];
Rs[i]=key_[i+5];
}
int []Ls_1=shift(Ls,1);
int []Rs_1=shift(Rs,1);
for(int i=0;i<5;i++)
{
key_[i]=Ls_1[i];
key_[i+5]=Rs_1[i];
}
for(int i=0;i<8;i++)
{
key1[i]=key_[P8[i]-1];
}
int []Ls_2=shift(Ls,2);
int []Rs_2=shift(Rs,2);
for(int i=0;i<5;i++)
{
key_[i]=Ls_2[i];
key_[i+5]=Rs_2[i];
}
for(int i=0;i<8;i++)
{
key2[i]=key_[P8[i]-1];
}
System.out.println("Your Key-1 :");
for(int i=0;i<8;i++)
System.out.print(key1[i]+" ");
System.out.println();
System.out.println("Your Key-2 :");
for(int i=0;i<8;i++)
System.out.print(key2[i]+" ");
}
// this function is use full for shifting(circular) the array n position towards left
int[] shift(int []ar,int n)
{
while(n>0)
{
int temp=ar[0];
for(int i=0;i<ar.length-1;i++)
{
ar[i]=ar[i+1];
}
ar[ar.length-1]=temp;
n--;
}
return ar;
}
// this is main encryption function takes plain text as input
// uses another functions and returns the array of cipher text
int[] encryption(int []plaintext)
{
int []arr=new int[8];
for(int i=0;i<8;i++)
{
arr[i]=plaintext[IP[i]-1];
}
int[] arr1=function_(arr,key1);
int []after_swap=swap(arr1,arr1.length/2);
int []arr2=function_(after_swap,key2);
int []ciphertext=new int[8];
for(int i=0;i<8;i++)
{
ciphertext[i]=arr2[IP_inv[i]-1];
}
return ciphertext;
}
//decimal to binary string 0-3
String binary_(int val)
{
if(val==0)
return "00";
else if(val==1)
return "01";
else if(val==2)
return "10";
else
return "11";
}
// this function is doing core things like expansion
// then xor with desired key then S0 and S1 substitution
// P4 permutation and again xor
// we have used this function 2 times(key-1 and key-2) during encryption and
// 2 times(key-2 and key-1) during decryption
int [] function_(int [] ar,int []key_)
{
int []l=new int[4];
int []r=new int[4];
for(int i=0;i<4;i++)
{
l[i]=ar[i];
r[i]=ar[i+4];
}
int[]ep=new int[8];
for(int i=0;i<8;i++)
{
ep[i]=r[EP[i]-1];
}
for(int i=0;i<8;i++)
{
ar[i]=key_[i]^ep[i];
}
int []l_1=new int[4];
int []r_1=new int[4];
for(int i=0;i<4;i++)
{
l_1[i]=ar[i];
r_1[i]=ar[i+4];
}
int row,col,val;
row=Integer.parseInt(""+l_1[0]+l_1[3], 2);
col=Integer.parseInt(""+l_1[1]+l_1[2], 2);
val=S0[row][col];
String str_l=binary_(val);
row=Integer.parseInt(""+r_1[0]+r_1[3], 2);
col=Integer.parseInt(""+r_1[1]+r_1[2], 2);
val=S1[row][col];
String str_r=binary_(val);
int []r_=new int[4];
for(int i=0;i<2;i++)
{
char c1=str_l.charAt(i);
char c2=str_r.charAt(i);
r_[i]=Character.getNumericValue(c1);
r_[i+2]=Character.getNumericValue(c2);
}
int[]r_p4=new int[4];
for(int i=0;i<4;i++)
{
r_p4[i]=r_[P4[i]-1];
}
for(int i=0;i<4;i++)
{
l[i]=l[i]^r_p4[i];
}
int []ouptput=new int[8];
for(int i=0;i<4;i++)
{
ouptput[i]=l[i];
ouptput[i+4]=r[i];
}
return ouptput;
}
// this function swaps the nibble of size n(4)
int []swap(int []aray,int n)
{
int[] l=new int[n];
int[] r=new int[n];
for(int i=0;i<n;i++)
{
l[i]=aray[i];
r[i]=aray[i+n];
}
int [] output=new int[2*n];
for(int i=0;i<n;i++)
{
output[i]=r[i];
output[i+n]=l[i];
}
return output;
}
// this is main decryption function
// here we have used all previously defined function
// it takes cipher text as input and returns the array of
// decrypted text
int [] decryption(int []ar)
{
int []arr=new int[8];
for(int i=0;i<8;i++)
{
arr[i]=ar[IP[i]-1];
}
int[] arr1=function_(arr,key2);
int []after_swap=swap(arr1,arr1.length/2);
int []arr2=function_(after_swap,key1);
int []decrypted=new int[8];
for(int i=0;i<8;i++)
{
decrypted[i]=arr2[IP_inv[i]-1];
}
return decrypted;
}
public static void main(String[] args) {
S_DES obj=new S_DES();
obj.key_generation(); //call to key generation function
//int []plaintext= {1,0,1,0,0,1,0,1};
int []plaintext= {1,0,0,1,0,1,1,1}; //extra example for checking perpose
System.out.println();
System.out.println("Your plain Text is :");
for(int i=0;i<8;i++) //printing the plaintext
System.out.print(plaintext[i]+" ");
int []ciphertext=obj.encryption(plaintext);
System.out.println();
System.out.println("Your cipher Text is :"); //printing the cipher text
for(int i=0;i<8;i++)
System.out.print(ciphertext[i]+" ");
int []decrypted=obj.decryption(ciphertext);
System.out.println();
System.out.println("Your decrypted Text is :"); //printing the decrypted text
for(int i=0;i<8;i++)
System.out.print(decrypted[i]+" ");
}
}