-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDES.java
More file actions
314 lines (254 loc) · 7.92 KB
/
DES.java
File metadata and controls
314 lines (254 loc) · 7.92 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package desf;
/**
*
* @author vinita
*/
public class Desf {
/**
* @param args the command line arguments
*/
static int IP[]={2,6,3,1,4,8,5,7};
static int IPINV[]={4,1,3,5,7,2,8,6};
static int plaintext[]={1,0,0,1,0,0,1,1};
static int EP[]={4,1,2,3,2,3,4,1};
static int s0[][]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}};
static int s1[][]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
static int ciphertext[]=new int[8];
static int pbox[]={2,4,3,1};
static int key1[]={1,1,0,0,1,1,0,1};
static int key2[]={1,1,0,0,0,1,1,0};
static int keyx[]={0,1,0,1,1,1,0,1,0,0};
public static void main(String[] args) {
Desf d=new Desf();
System.out.print("plaintext:");
d.print(plaintext);
ciphertext=d.encrypt(plaintext);
System.out.print("ciphertext:");
d.print(ciphertext);
System.out.println("DECRYPTION:");
System.out.print("ciphertext:");
d.print(ciphertext);
plaintext=d.decrypt(ciphertext);
System.out.print("plaintext:");
d.print(plaintext);
d.genkey(keyx);
}
public int[] encrypt(int plain[]){
int postip[]=new int[8];
int left[]=new int[4];
int right[]=new int[4];
int postfk[]=new int[4];
int temp[]=new int[4];
int postfk2[]=new int[8];
int postipinv[]=new int[8];
//step1:initial permutation
for(int i=0;i<8;i++){
postip[i]=plain[IP[i]-1];
}
System.out.print("POST INITIAL PERMUTATION:");
print(postip);
//step2:
for(int i=0;i<4;i++){
left[i]=postip[i];
right[i]=postip[i+4];
}
System.out.print("LEFT:");
print(left);
System.out.print("RIGHT:");
print(right);
postfk=fk(right,key1);
left=xor(postfk,left);
for(int i=0;i<4;i++){
temp[i]=left[i];
left[i]=right[i];
right[i]=temp[i];
}
System.out.println("AFTER SWAPPING");
System.out.print("LEFT:");
print(left);
System.out.print("RIGHT:");
print(right);
postfk=fk(right,key2);
left=xor(postfk,left);
for(int i=0;i<4;i++){
postfk2[i]=left[i];
postfk2[i+4]=right[i];
}
System.out.print("POST FK2:");
print(postfk2);
for(int i=0;i<8;i++){
postipinv[i]=postfk2[IPINV[i]-1];
}
return postipinv;
}
public int[] decrypt(int plain[]){
int postip[]=new int[8];
int left[]=new int[4];
int right[]=new int[4];
int postfk[]=new int[4];
int temp[]=new int[4];
int postfk2[]=new int[8];
int postipinv[]=new int[8];
//step1:initial permutation
for(int i=0;i<8;i++){
postip[i]=plain[IP[i]-1];
}
System.out.print("POST INITIAL PERMUTATION:");
print(postip);
//step2:
for(int i=0;i<4;i++){
left[i]=postip[i];
right[i]=postip[i+4];
}
System.out.print("LEFT:");
print(left);
System.out.print("RIGHT:");
print(right);
postfk=fk(right,key2);
left=xor(postfk,left);
for(int i=0;i<4;i++){
temp[i]=left[i];
left[i]=right[i];
right[i]=temp[i];
}
System.out.println("AFTER SWAPPING");
System.out.print("LEFT:");
print(left);
System.out.print("RIGHT:");
print(right);
postfk=fk(right,key1);
left=xor(postfk,left);
for(int i=0;i<4;i++){
postfk2[i]=left[i];
postfk2[i+4]=right[i];
}
System.out.print("POST FK2:");
print(postfk2);
for(int i=0;i<8;i++){
postipinv[i]=postfk2[IPINV[i]-1];
}
return postipinv;
}
public int[] fk(int r[],int key[]){
int postep[]=new int[8];
int postaddkey[]=new int[8];
int postsbox[]=new int[4];
int postpbox[]=new int[4];
for(int i=0;i<4;i++){
postep[i]=r[EP[i]-1];
postep[i+4]=r[EP[i+4]-1];
}
System.out.print("POST EXPANSION PERMUTATION:");
print(postep);
postaddkey=xor(postep,key);
System.out.print("POST KEY ADDITION:");
print(postaddkey);
int r1,c1,r2,c2;
r1=postaddkey[0]*2+postaddkey[3];
c1=postaddkey[1]*2+postaddkey[2];
r2=postaddkey[4]*2+postaddkey[7];
c2=postaddkey[5]*2+postaddkey[6];
System.out.println("r1:"+r1+" c1:"+c1+" r2:"+r2+" c2:"+c2);
String x=Integer.toBinaryString(s0[r1][c1]);
String y=Integer.toBinaryString(s1[r2][c2]);
if(x.length()==1){
x="0"+x;
}
if(y.length()==1){
y="0"+y;
}
String z=x+y;
for(int i=0;i<4;i++){
postsbox[i]=Character.getNumericValue(z.charAt(i));
}
System.out.print("POST SBOX:");
print(postsbox);
for(int i=0;i<4;i++){
postpbox[i]=postsbox[pbox[i]-1];
}
System.out.print("POST PBOX:");
print(postpbox);
return postpbox;
}
public int[] xor(int x[],int[]y){
int temp[]=new int[x.length];
for(int i=0;i<x.length;i++){
temp[i]=x[i]^y[i];
}
return temp;
}
public void genkey(int a[]){
int left[]=new int[5];
int right[]=new int[5];
int p10[]={3,5,2,7,4,10,1,9,8,6};
int p8[]={6,3,7,4,8,5,10,9};
int postp10[]=new int[10];
int postshift[]=new int[10];
int key1[]=new int[8];
int key2[]=new int[8];
for(int i=0;i<10;i++){
postp10[i]=a[p10[i]-1];
}
System.out.print("POST P10:");
print(postp10);
for(int i=0;i<5;i++){
left[i]=postp10[i];
right[i]=postp10[i+5];
}
System.out.print("LEFT:");
print(left);
System.out.print("RIGHT:");
print(right);
for(int i=0;i<4;i++){
postshift[i]=left[i+1];
postshift[i+5]=right[i+1];
}
postshift[4]=left[0];
postshift[9]=right[0];
System.out.print("POSTSHIFT:");
print(postshift);
for(int i=0;i<8;i++){
key1[i]=postshift[p8[i]-1];
}
System.out.print("POSTP8:");
print(key1);
for(int i=0;i<5;i++){
left[i]=postshift[i];
right[i]=postshift[i+5];
}
System.out.print("LEFT:");
print(left);
System.out.print("RIGHT:");
print(right);
for(int i=0;i<3;i++){
for(int j=0;j<4;j++){
postshift[j]=left[j+1];
postshift[j+5]=right[j+1];
}
postshift[4]=left[0];
postshift[9]=right[0];
for(int k=0;k<5;k++){
left[k]=postshift[k];
right[k]=postshift[k+5];
}
}
for(int i=0;i<8;i++){
key2[i]=postshift[p8[i]-1];
}
System.out.print("key1:");
print(key1);
System.out.print("key2:");
print(key2);
}
public static void print(int []a){
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}