-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryption.py
62 lines (50 loc) · 1.36 KB
/
encryption.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
import cipher
import numpy as np
# Encryption mode:
# -l linear
# -nl nearly linear
# -n non linear
def encryption(u, K, mode):
x = [] #cipher text
v=[] #output of k_i+u additon
y=[] #output of substitution
z=[] #output of transposition
p = 11
k_i = cipher.subkey_generation(K, mode)
w = u
n = 5 #number of round
for i in range(0,n):
v = cipher.subkey_sum(k_i[i],w,p)
if(mode == "l"):
y = cipher.linear_substitution(v, p)
elif(mode == "nl"):
y = cipher.nearlyLinear_substitution(v)
elif(mode == "n"):
y = cipher.nonLinear_substitution(v, p)
else:
print("Invalid mode")
return -1
z = cipher.transposition(y)
if(i<n-1): w = cipher.linear(z, p)
x = cipher.subkey_sum(k_i[n],z,p)
return x
if __name__ == "__main__":
K = [1,0,0,0,0,0,0,0] #key space
u = [1,0,0,0,0,0,0,0] #plain text
print()
print("[*] Linear encryption")
print(" u:", u)
print(" K:", K)
print(" x:", encryption(u, K, "l"))
print()
print("[*] Nearly linear encryption")
print(" u:", u)
print(" K:", K)
print(" x:", encryption(u, K, "nl"))
print()
K = [1,0,0,0]
print("[*] Non linear encryption")
print(" u:", u)
print(" K:", K)
print(" x:", encryption(u, K, "n"))
print()