-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecryption.py
65 lines (53 loc) · 1.43 KB
/
decryption.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
import cipher
import encryption as e
# Decryption mode:
# -l linear
# -nl nearly linear
# -n non linear
def decryption(x, K, mode):
v=[]
y=[]
z=[]
p = 11
k_i = cipher.subkey_generation(K, mode)
n = 5
v = x
z = cipher.subkey_sub(k_i[n],v,p)
for i in range(n-1,-1,-1):
if(i<n-1):
z = cipher.linear_inverse(w,p)
y = cipher.transposition(z)
if(mode == "l"):
v = cipher.linear_substitution_inverse(y,p)
elif(mode == "nl"):
v = cipher.nearlyLinear_substitution_inverse(y)
elif(mode == "n"):
v = cipher.nonLinear_substitution_inverse(y,p)
w = cipher.subkey_sub(k_i[i],v,p)
u = w
return u
if __name__ == "__main__":
K = [1,0,0,0,0,0,0,0] #key space
x = [4,0,0,9,7,0,0,3] #plain text
print()
print("[*] Linear decryption")
print(" x:", x)
print(" K:", K)
print(" u:", decryption(x, K, "l"))
print()
K = [1,0,0,0,0,0,0,0] #key space
x = e.encryption([1,2,3,4,5,6,7,8],K,"nl") #plain text
print()
print("[*] Nearly linear decryption")
print(" x:", x)
print(" K:", K)
print(" u:", decryption(x, K, "nl"))
print()
K = [1,0,0,0] #key space
x = e.encryption([1,2,3,4,5,6,7,8],K,"n") #plain text
print()
print("[*] Non linear decryption")
print(" x:", x)
print(" K:", K)
print(" u:", decryption(x, K, "n"))
print()