-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths_des.py
59 lines (47 loc) · 1.75 KB
/
s_des.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
class SDES:
def perFirst(self, plaintext : str) -> str :
table = [2, 6, 3, 1, 4, 8, 5, 7]
p_box = ""
for i in table:
p_box += plaintext[i-1]
return p_box
def perLast(self, plaintext : str) -> str :
table = [4, 1, 3, 5, 7, 2, 8, 6]
p_box = ""
for i in table:
p_box += plaintext[i-1]
return p_box
def p_expansion_box(self, plaintext : str) -> str :
table = [4, 1, 2, 3, 2, 3, 4, 1]
p_box = ""
for i in table:
p_box += plaintext[i-1]
return p_box
def p_straight_box(self, plaintext : str) -> str :
table = [2, 4, 3, 1]
p_box = ""
for i in table:
p_box += plaintext[i-1]
return p_box
def s_box(self, plaintext : str) -> str :
s1 = [["01", "00", "11", "10"],
["11", "10", "01", "00"],
["00", "10", "01", "11"],
["11", "01", "11", "10"]]
s2 = [["00", "01", "10", "11"],
["10", "00", "01", "11"],
["11", "00", "01", "00"],
["10", "01", "00", "11"]]
row_s1 = int(plaintext[0] + plaintext[3], 2)
col_s1 = int(plaintext[1] + plaintext[2], 2)
row_s2 = int(plaintext[4] + plaintext[7], 2)
col_s2 = int(plaintext[5] + plaintext[6], 2)
return s1[row_s1][col_s1] + s2[row_s2][col_s2]
def roundFunction(self, plaintext : str, R1 : str) -> str :
expanded = self.p_expansion_box(plaintext)
xor = ""
for i in range(len(expanded)):
xor += str(int(expanded[i]) ^ int(R1[i]))
sbox = self.s_box(xor)
pstraight = self.p_straight_box(sbox)
return pstraight