-
Notifications
You must be signed in to change notification settings - Fork 0
/
DES1.java
122 lines (112 loc) · 3.56 KB
/
DES1.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
import java.util.Vector;
public class DES1 extends DES_Steps
{
//Sames as in DES0
private String name;
private String p;
private String nP;
private String k;
private String nK;
private String cPK;
private Vector<String> rCPK;
private String cNPK;
private Vector<String> rCNPK;
private String cPNK;
private Vector<String> rCPNK;
//Same as in DES0
public DES1(String aName, String p, String nP, String k, String nK)
{
name = aName;
this.p = p;
this.nP = nP;
this.k = k;
this.nK = nK;
rCPK = new Vector<>();
rCNPK = new Vector<>();
rCPNK = new Vector<>();
cPK = encrypt(this.p, this.k, rCPK);
cNPK = encrypt(this.nP, this.k, rCNPK);
cPNK = encrypt(this.p, this.nK, rCPNK);
}
public String getName(){
return name;
}
//Same as in DES0
public Vector<Integer> compareP(){
Vector<Integer> output = new Vector<>();
Integer count = 0;
for(int i = 0; i < rCPK.size(); i++){
String a = rCPK.get(i);
String b = rCNPK.get(i);
count = 0;
for(int j = 0; j < a.length(); j++){
if(a.charAt(j) != b.charAt(j)){
count++;
}
}
output.add(count);
}
count = 0;
for(int i = 0; i < cPK.length(); i++){
if(cPK.charAt(i) != cNPK.charAt(i)){
count++;
}
}
output.add(count);
return output;
}
//Same as in DES0
public Vector<Integer> compareK(){
Vector<Integer> output = new Vector<>();
Integer count = 0;
for(int i = 0; i < rCPK.size(); i++){
String a = rCPK.get(i);
String b = rCPNK.get(i);
count = 0;
for(int j = 0; j < a.length(); j++){
if(a.charAt(j) != b.charAt(j)){
count ++;
}
}
output.add(count);
}
count = 0;
for(int i = 0; i < cPK.length(); i++){
if(cPK.charAt(i) != cPNK.charAt(i)){
count++;
}
}
output.add(count);
return output;
}
//Same as in DES0, however Fiestal function is modifies as outlined in assignment spec
private String encrypt(String plainText, String key, Vector<String> rState)
{
String modKey = keyBitDrop(key);
Vector<String>bigKeys = new Vector<>(keyRotate(modKey));
Vector<String>smallKeys = new Vector<>(keyCompress(bigKeys));
String output = doIP(plainText);
output = roundsAndSwap(output, smallKeys, rState);
output = doFP(output);
return output;
}
//Same as DES0, with modification outlined in assignment spec
private String roundsAndSwap(String input, Vector<String> keyList, Vector<String> rState){
String output = "";
String left = input.substring(0, (input.length()/2));
String right = input.substring((input.length()/2));
for(int i = 0; i < 16; i++){
String tempR = expandTxt(right);
String tempL = right;
//XOR with Round key commented out as assignment spec wanted to explore effect of removeing thsi step in DES
//tempR = xor(tempR, keyList.get(i));
tempR = sBox(tempR);
tempR = fPerm(tempR);
right = xor(left, tempR);
left = tempL;
rState.add(left + right);
}
output = right + left;
return output;
}
}