-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDESCommon.py
executable file
·150 lines (122 loc) · 3.25 KB
/
DESCommon.py
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
from DESConstant import *
from DESUtil import get_column, get_row, to_binary, left_shift
def apply_IP(block):
"""input permutaion
"""
r = []
r.extend(block)
for i in range(0, 64):
r[i] = block[IP[i]]
return r
def apply_FP(block):
"""final permutaion
"""
r = []
r.extend(block)
for i in range(0, 64):
r[i] = block[(FP[i])]
return r
def e_box(block):
"""exapansion permutation to expand 32 bit block into 48 bit block
also called E-box
"""
dummy = []
for i in range(48):
dummy.append(block[E[i]])
r = []
for i in range(0, 48, 6):
j = i + 6
r.append(dummy[i:j])
return r
def s_box(block):
"""s-box function to reduce 48 bit block to 32 bit block
"""
for i in range(0, 8):
row = str(block[i][0]) + str(block[i][-1])
column = ''
for j in range(1, 5):
column += str(block[i][j])
a = 16 * get_row(row)
a += get_column(column)
block.pop(i)
block.insert(i, to_binary(ord(chr(s[i][a]))))
r = []
for i in block:
r.extend(i[4:8])
return r
def p_box(block):
"""p-box permutation
"""
r = []
r.extend(block)
for i in range(32):
r[i] = block[P[i]]
return r
def iterate(left_block, right_block, keys, CIPHERS_FOR_EACH_ROUND):
"""iterating for the 16 rounds
"""
for j in range(0, 16):
d9 = []
d9.extend(right_block)
right_block = e_box(right_block)
for i in range(0, 8):
di = i * 6
for k in range(0, 6):
right_block[i][k] ^= keys[j][di + k]
right_block = s_box(right_block)
right_block = p_box(right_block)
for i in range(0, 32):
right_block[i] ^= left_block[i]
left_block = []
left_block.extend(d9)
if CIPHERS_FOR_EACH_ROUND is not None:
t = left_block
t.extend(right_block)
CIPHERS_FOR_EACH_ROUND.append(t)
return left_block, right_block
def DES(text_bits, start, end, keys, CIPHERS_FOR_EACH_ROUND=None):
"""Heart of the program : DES algorithm
"""
block = []
for i in range(start, end):
block.append(text_bits[i])
block = apply_IP(block)
left_block = block[0:32]
right_block = block[32:64]
left_block, right_block = iterate(left_block, right_block, keys, CIPHERS_FOR_EACH_ROUND)
block = []
block.extend(right_block)
block.extend(left_block)
block = apply_FP(block)
cipher_block = ''
for i in block:
cipher_block += str(i)
return cipher_block
def generate_keys(key_text):
""" key generation
"""
key = []
for i in key_text:
key.extend(to_binary(ord(i)))
C = []
D = []
r = []
for i in range(28):
C.append(key[PC1_C[i]])
for i in range(28):
D.append(key[PC1_D[i]])
for i in range(0, 16):
if i in [0, 1, 8, 15]:
C = left_shift(C, 1)
D = left_shift(D, 1)
else:
C = left_shift(C, 2)
D = left_shift(D, 2)
CD = []
CD.extend(C)
CD.extend(D)
dummy = []
for i in range(48):
dummy.append(CD[PC2[i]])
r.append(dummy)
return r