-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdes.py
More file actions
201 lines (152 loc) · 6.12 KB
/
des.py
File metadata and controls
201 lines (152 loc) · 6.12 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
from __future__ import print_function, unicode_literals
from PyInquirer import prompt
from des_constants import initial_permutation, final_permutation
from des_constants import expansion_D_box, straight_D_box, compression_D_box
from des_constants import sbox, pc1_table, shift_table
from utilities import hex2bin, bin2hex, dec2bin, bin2dec
from utilities import xor, permute, shift_left
def sub_key_generation(key):
print("--------------------------------\nGeneration of 16 subkeys")
key = hex2bin(key)
print(f"\nGiven key = {bin2hex(key)} = {key}")
# PC-1 Table
key = permute(key, pc1_table, 56)
print(f"Given key after PC1 = {bin2hex(key)} = {key}\n")
left = key[0:28]
right = key[28:]
print(f"Left half = {bin2hex(left)} = {left}")
print(f"Right half = {bin2hex(right)} = {right}\n")
round_key_binary = []
number_of_rounds = 16
for i in range(0, number_of_rounds):
left = shift_left(left, shift_table[i])
right = shift_left(right, shift_table[i])
print(f"Key Generation Round {i+1} Left half = {bin2hex(left)} = {left}")
print(f"Key Generation Round {i+1} Right half = {bin2hex(right)} = {right}")
combined = left + right
print(
f"Key Generation Round {i+1} Combined key = {bin2hex(combined)} = {combined}"
)
round_key = permute(combined, compression_D_box, 48)
print(
f"Key Generation Round {i+1} Compressed key = {bin2hex(round_key)} = {round_key}\n"
)
round_key_binary.append(round_key)
return round_key_binary
def encrypt(plaintext, round_key_bin, text="Plain"):
binary_plaintext = hex2bin(plaintext)
print("--------------------------------\n16 rounds of DES")
print(f"{text} text = {bin2hex(binary_plaintext)} = {binary_plaintext}")
initial_permutated = permute(binary_plaintext, initial_permutation, 64)
print(
f"{text} text after IP = {bin2hex(initial_permutated)} = {initial_permutated}\n"
)
left = initial_permutated[0:32]
right = initial_permutated[32:]
print(f"Left half = {bin2hex(left)} = {left}")
print(f"Right half = {bin2hex(right)} = {right}\n")
number_of_rounds = 16
for i in range(0, number_of_rounds):
print(f"Cipher Round {i+1} input to F = {bin2hex(right)} = {right}")
right_expanded = permute(right, expansion_D_box, 48)
print(
f"Cipher Round {i+1} Right expanded = {bin2hex(right_expanded)} = {right_expanded}"
)
s_box_input = xor(right_expanded, round_key_bin[i])
print(
f"Cipher Round {i+1} S-box input = {bin2hex(s_box_input)} = {s_box_input}"
)
s_box_output = ""
for j in range(0, 8):
first_bit = s_box_input[j * 6]
second_bit = s_box_input[j * 6 + 1]
third_bit = s_box_input[j * 6 + 2]
fourth_bit = s_box_input[j * 6 + 3]
fifth_bit = s_box_input[j * 6 + 4]
sixth_bit = s_box_input[j * 6 + 5]
row = bin2dec(int(first_bit + sixth_bit))
col = bin2dec(int(second_bit + third_bit + fourth_bit + fifth_bit))
val = sbox[j][row][col]
s_box_output = s_box_output + dec2bin(val)
print(
f"Cipher Round {i+1} S-box output = {bin2hex(s_box_output)} = {s_box_output}"
)
s_box_output = permute(s_box_output, straight_D_box, 32)
left = xor(left, s_box_output)
if i != 15:
left, right = right, left
print(f"Cipher Round {i+1} Output of F = {bin2hex(right)} = {right}")
print(f"Cipher Round {i+1} Left Half = {bin2hex(left)} = {left}")
print(f"Cipher Round {i+1} Right Half = {bin2hex(right)} = {right}")
print(
f"Cipher Round {i+1} Key = {bin2hex(round_key_bin[i])} = {round_key_bin[i]}\n"
)
combine = left + right
print(f"Round 16 left and right combined = {bin2hex(combine)} = {combine}")
cipher_text = permute(combine, final_permutation, 64)
print(f"Combined after FP = {bin2hex(cipher_text)} = {cipher_text}")
return cipher_text
print(
"""
Sample Input/Output:
cipher_text = "C0B7A8D05F3A829C"
key = "AABB09182736CCDD"
plain_text = "123456ABCD132536"
"""
)
def main():
questions = [
{
"type": "list",
"name": "cipher_type",
"message": "[DES] What do you want to do?",
"choices": ["Encryption", "Decryption"],
},
]
answers = prompt(questions)
if answers["cipher_type"] == "Encryption":
questions = [
{
"type": "input",
"name": "plain_text",
"message": "Enter the plain text (hex): ",
},
{
"type": "input",
"name": "key",
"message": "Enter the key (hex): ",
},
]
answers = prompt(questions)
cipher_text = answers["plain_text"]
key = answers["key"]
print(f"Encryption of \n\plain text = {cipher_text} \n\tusing key = {key}")
sub_keys = sub_key_generation(key)
print([bin2hex(x) for x in sub_keys])
decrypted_text = bin2hex(encrypt(cipher_text, sub_keys))
print("\nPlain Text = ", decrypted_text)
else:
questions = [
{
"type": "input",
"name": "cipher_text",
"message": "Enter the cipher text (hex): ",
},
{
"type": "input",
"name": "key",
"message": "Enter the key (hex): ",
},
]
answers = prompt(questions)
cipher_text = answers["cipher_text"]
key = answers["key"]
print(f"Encryption of \n\cipher text = {cipher_text} \n\tusing key = {key}")
sub_keys = sub_key_generation(key)
print([bin2hex(x) for x in sub_keys])
reversed_sub_keys = sub_keys[::-1]
print([bin2hex(x) for x in reversed_sub_keys])
decrypted_text = bin2hex(encrypt(cipher_text, reversed_sub_keys, text="Cipher"))
print("\nPlain Text = ", decrypted_text)
if __name__ == "__main__":
main()