-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_vulnerability.py
51 lines (44 loc) · 1.19 KB
/
linear_vulnerability.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
import encryption as e
import utils
import numpy as np
# x = E(K, u) = AK + Bu mod p
def detect_vulnerability():
p = 11
A, B =utils.get_matrices("l")
u = np.array([1,0,0,0,0,0,0,0])
K = np.array([1,0,0,0,0,0,0,0])
x = (np.dot(A,K) + np.dot(B,u)) % p
print()
print("Task 3: identify linear relationship")
print("x = E(K, u) = AK + Bu mod p")
print()
print("Matrix A:\n", A)
print()
print("Matrix B:\n", B)
print()
print("u:", u)
print("k:", K)
print("x:",x)
print("x_prime: ",e.encryption(u,K,"l"))
print("x == x_prime => linear relationship proof")
print()
def linear_KPA(x, u):
p = 11
A, B = utils.get_matrices("l")
A_inv = utils.matrix_inverse(A,p)
K = np.around(np.dot(A_inv, (x - np.dot(B,u))))%p
return K
if __name__ == "__main__":
detect_vulnerability()
U, X = utils.read_file("KPAdataAerosmith/KPApairsAerosmith_linear.txt")
print("Task 4: linear cryptanalysis KPA")
print()
K = []
for u,x in zip(U,X):
print("u:", u)
print("x:",x)
K = linear_KPA(x, u)
print("k:",K)
print()
print("All couples have the same key which is: ")
print(K)