This repository has been archived by the owner on May 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdes.rb
221 lines (180 loc) · 7.77 KB
/
des.rb
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
input = 0x0123456789abcdef
puts "Input:\t%064b\n\t%016x" % [input, input]
puts "\n## KEY GENERATION ##"
pc1 = Array.[]( 57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,
63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4 )
number = 0
# PC-1
for i in 0...pc1.length
position = pc1.length - pc1[i]
number = (number << 1) + ((input >> position) & 1)
end
puts "PC-1:\t%056b" % number
# Split in C0 and D0
d0 = number & 0xfffffff
c0 = (number >> 28) & 0xfffffff
puts "C0:\t%028b" % c0
puts "D0:\t%028b" % d0
# Left shift 1
c1 = ((c0 << 1) + ((c0 & 0xfffffff) >> 27)) & 0xfffffff
d1 = ((d0 << 1) + ((d0 & 0xfffffff) >> 27)) & 0xfffffff
puts "C1:\t%028b" % c1
puts "D1:\t%028b" % d1
# PC-2
pc2 = Array.[](14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32)
merge = (c1 << 28) + d1
key = 0
for i in 0...pc2.length
position = pc2.length - pc2[i]
key = (key << 1) + ((merge >> position) & 1)
end
puts "Key:\t%048b" % key
## DATA ENCRYPTION ##
puts "\n## DATA ENCRYPTION ##"
# IP
ip = Array.[](58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7)
number = 0
for i in 0...ip.length
position = ip.length - ip[i]
number = (number << 1) + ((input >> position) & 1)
end
puts "IP:\t%064b" % number
# Split in L0 and R0
r0 = number & 0xffffffff
l0 = (number >> 28) & 0xffffffff
puts "L0:\t%032b" % l0
puts "R0:\t%032b" % r0
# Expansion
exp = Array.[](32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1)
number = 0
for i in 0...exp.length
position = (Math.log(r0)/Math.log(2)).ceil() - exp[i]
number = (number << 1) + ((r0 >> position) & 1)
end
puts "E:\t%048b" % number
# Exclusive or E and K
a = number ^ key
puts "A:\t%048b" % a
# Breaking into 6bit boxes
blocks = Array.new(8)
number = a
for i in blocks.length.downto(1)
blocks[i-1] = number & 0x3f
number = (number >> 6)
end
print "Blocks:\t"
for i in 0...blocks.length
print "%06b " % blocks[i]
end
print "\n"
# S-Boxes
sboxes = [[[0x0e, 0x04, 0x0d, 0x01, 0x02, 0x0f, 0x0b, 0x08, 0x03, 0x0a, 0x06, 0x0c, 0x05, 0x09, 0x00, 0x07],
[0x00, 0x0f, 0x07, 0x04, 0x0e, 0x02, 0x0d, 0x01, 0x0a, 0x06, 0x0c, 0x0b, 0x09, 0x05, 0x03, 0x08],
[0x04, 0x01, 0x0e, 0x08, 0x0d, 0x06, 0x02, 0x0b, 0x0f, 0x0c, 0x09, 0x07, 0x03, 0x0a, 0x05, 0x00],
[0x0f, 0x0c, 0x08, 0x02, 0x04, 0x09, 0x01, 0x07, 0x05, 0x0b, 0x03, 0x0e, 0x0a, 0x00, 0x06, 0x0d]],
[[0x0f, 0x01, 0x08, 0x0e, 0x06, 0x0b, 0x03, 0x04, 0x09, 0x07, 0x02, 0x0d, 0x0c, 0x00, 0x05, 0x0a],
[0x03, 0x0d, 0x04, 0x07, 0x0f, 0x02, 0x08, 0x0e, 0x0c, 0x00, 0x01, 0x0a, 0x06, 0x09, 0x0b, 0x05],
[0x00, 0x0e, 0x07, 0x0b, 0x0a, 0x04, 0x0d, 0x01, 0x05, 0x08, 0x0c, 0x06, 0x09, 0x03, 0x02, 0x0f],
[0x0d, 0x08, 0x0a, 0x01, 0x03, 0x0f, 0x04, 0x02, 0x0b, 0x06, 0x07, 0x0c, 0x00, 0x05, 0x0e, 0x09]],
[[0x0a, 0x00, 0x09, 0x0e, 0x06, 0x03, 0x0f, 0x05, 0x01, 0x0d, 0x0c, 0x07, 0x0b, 0x04, 0x02, 0x08],
[0x0d, 0x07, 0x00, 0x09, 0x03, 0x04, 0x06, 0x0a, 0x02, 0x08, 0x05, 0x0e, 0x0c, 0x0b, 0x0f, 0x01],
[0x0d, 0x06, 0x04, 0x09, 0x08, 0x0f, 0x03, 0x00, 0x0b, 0x01, 0x02, 0x0c, 0x05, 0x0a, 0x0e, 0x07],
[0x01, 0x0a, 0x0d, 0x00, 0x06, 0x09, 0x08, 0x07, 0x04, 0x0f, 0x0e, 0x03, 0x0b, 0x05, 0x02, 0x0c]],
[[0x07, 0x0d, 0x0e, 0x03, 0x00, 0x06, 0x09, 0x0a, 0x01, 0x02, 0x08, 0x05, 0x0b, 0x0c, 0x04, 0x0f],
[0x0d, 0x08, 0x0b, 0x05, 0x06, 0x0f, 0x00, 0x03, 0x04, 0x07, 0x02, 0x0c, 0x01, 0x0a, 0x0e, 0x09],
[0x0a, 0x06, 0x09, 0x00, 0x0c, 0x0b, 0x07, 0x0d, 0x0f, 0x01, 0x03, 0x0e, 0x05, 0x02, 0x08, 0x04],
[0x03, 0x0f, 0x00, 0x06, 0x0a, 0x01, 0x0d, 0x08, 0x09, 0x04, 0x05, 0x0b, 0x0c, 0x07, 0x02, 0x0e]],
[[0x02, 0x0c, 0x04, 0x01, 0x07, 0x0a, 0x0b, 0x06, 0x08, 0x05, 0x03, 0x0f, 0x0d, 0x00, 0x0e, 0x09],
[0x0e, 0x0b, 0x02, 0x0c, 0x04, 0x07, 0x0d, 0x01, 0x05, 0x00, 0x0f, 0x0a, 0x03, 0x09, 0x08, 0x06],
[0x04, 0x02, 0x01, 0x0b, 0x0a, 0x0d, 0x07, 0x08, 0x0f, 0x09, 0x0c, 0x05, 0x06, 0x03, 0x00, 0x0e],
[0x0b, 0x08, 0x0c, 0x07, 0x01, 0x0e, 0x02, 0x0d, 0x06, 0x0f, 0x00, 0x09, 0x0a, 0x04, 0x05, 0x03]],
[[0x0c, 0x01, 0x0a, 0x0f, 0x09, 0x02, 0x06, 0x08, 0x00, 0x0d, 0x03, 0x04, 0x0e, 0x07, 0x05, 0x0b],
[0x0a, 0x0f, 0x04, 0x02, 0x07, 0x0c, 0x09, 0x05, 0x06, 0x01, 0x0d, 0x0e, 0x00, 0x0b, 0x03, 0x08],
[0x09, 0x0e, 0x0f, 0x05, 0x02, 0x08, 0x0c, 0x03, 0x07, 0x00, 0x04, 0x0a, 0x01, 0x0d, 0x0b, 0x06],
[0x04, 0x03, 0x02, 0x0c, 0x09, 0x05, 0x0f, 0x0a, 0x0b, 0x0e, 0x01, 0x07, 0x06, 0x00, 0x08, 0x0d]],
[[0x04, 0x0b, 0x02, 0x0e, 0x0f, 0x00, 0x08, 0x0d, 0x03, 0x0c, 0x09, 0x07, 0x05, 0x0a, 0x06, 0x01],
[0x0d, 0x00, 0x0b, 0x07, 0x04, 0x09, 0x01, 0x0a, 0x0e, 0x03, 0x05, 0x0c, 0x02, 0x0f, 0x08, 0x06],
[0x01, 0x04, 0x0b, 0x0d, 0x0c, 0x03, 0x07, 0x0e, 0x0a, 0x0f, 0x06, 0x08, 0x00, 0x05, 0x09, 0x02],
[0x06, 0x0b, 0x0d, 0x08, 0x01, 0x04, 0x0a, 0x07, 0x09, 0x05, 0x00, 0x0f, 0x0e, 0x02, 0x03, 0x0c]],
[[0x0d, 0x02, 0x08, 0x04, 0x06, 0x0f, 0x0b, 0x01, 0x0a, 0x09, 0x03, 0x0e, 0x05, 0x00, 0x0c, 0x07],
[0x01, 0x0f, 0x0d, 0x08, 0x0a, 0x03, 0x07, 0x04, 0x0c, 0x05, 0x06, 0x0b, 0x00, 0x0e, 0x09, 0x02],
[0x07, 0x0b, 0x04, 0x01, 0x09, 0x0c, 0x0e, 0x02, 0x00, 0x06, 0x0a, 0x0d, 0x0f, 0x03, 0x05, 0x08],
[0x02, 0x01, 0x0e, 0x07, 0x04, 0x0a, 0x08, 0x0d, 0x0f, 0x0c, 0x09, 0x00, 0x03, 0x05, 0x06, 0x0b]]]
print "S-Box:"
for i in 0...blocks.length
row = (((blocks[i] >> 5) & 1) << 1) + (blocks[i] & 1)
col = 0
for j in 4.downto(1)
col <<= 1
col += (blocks[i] >> j) & 1
end
blocks[i] = sboxes[i][row][col]
puts "\t%02b\t%04b\t=>\t%04b" % [row, col, blocks[i]]
end
# Permutation
p = [0x10, 0x07, 0x14, 0x15,
0x1d, 0x0c, 0x1c, 0x11,
0x01, 0x0f, 0x17, 0x1a,
0x05, 0x12, 0x1f, 0x0a,
0x02, 0x08, 0x18, 0x0e,
0x20, 0x1b, 0x03, 0x09,
0x13, 0x0d, 0x1e, 0x06,
0x16, 0x0b, 0x04, 0x19]
concat = 0
for i in 0...blocks.length
concat <<= 4
concat += blocks[i]
end
number = 0
for i in 0...p.length
position = (Math.log(concat)/Math.log(2)).ceil() - p[i]
number = (number << 1) + ((concat >> position) & 1)
end
puts "P:\t%032b" % number
# Exclusive or l0 and p
r = l0 ^ number
puts "R:\t%032b" % r
# Final permutation
fp = [0x28, 0x08, 0x30, 0x10, 0x38, 0x18, 0x40, 0x20,
0x27, 0x07, 0x2f, 0x0f, 0x37, 0x17, 0x3f, 0x1f,
0x26, 0x06, 0x2e, 0x0e, 0x36, 0x16, 0x3e, 0x1e,
0x25, 0x05, 0x2d, 0x0d, 0x35, 0x15, 0x3d, 0x1d,
0x24, 0x04, 0x2c, 0x0c, 0x34, 0x14, 0x3c, 0x1c,
0x23, 0x03, 0x2b, 0x0b, 0x33, 0x13, 0x3b, 0x1b,
0x22, 0x02, 0x2a, 0x0a, 0x32, 0x12, 0x3a, 0x1a,
0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19]
rl = (r << 32) + l0
puts "RL:\t%064b" % rl
number = 0
for i in 0...fp.length
position = fp.length - fp[i]
number = (number << 1) + ((rl >> position) & 1)
end
puts "FP:\t%064b" % number