-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHill Cipher.py
More file actions
80 lines (71 loc) · 2.15 KB
/
Hill Cipher.py
File metadata and controls
80 lines (71 loc) · 2.15 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
# -*- coding: utf-8 -*-
# @Time : 2017/5/12 17:40
# @Author : set3rnal
# @Site :
# @File : Hill Cipher.py
# @Software: PyCharm
from numpy import *
Dic = {chr(i + 97): i for i in range(26)}
def decode(pwd, org):
temp = []
result = []
while True:
if len(pwd) % 3 != 0:
pwd.append(pwd[-1])
else:
break
for i in pwd:
temp.append(Dic.get(i))
temp = array(temp)
temp = temp.reshape(len(pwd) / 3, 3)
print "temp:"
print temp
xx = matrix(temp) * org
for j in range(len(pwd) / 3):
for i in range(3):
if (int(xx[j, i]) >= 26):
result.append(chr(xx[j, i] % 26 + 97))
# print xx[j, i] % 26
else:
# print xx[j, i]
result.append(chr(xx[j, i] + 97))
return result
def get_vmatrix(org):
org_adjoin = org.I * linalg.det(org) # .I 返回矩阵的逆矩阵 linalg.det 返回矩阵的行列式
print "org_adjoin"
print org_adjoin
org_det = int(str(abs(linalg.det(org))).split('.')[0])
print "org_det"
print org_det
for i in range(1, 26):
if i * org_det % 26 == 1:
break
org_mod = -org_adjoin * i % 26
org_mod = matrix(org_mod)
print "org_mod"
print org_mod
temp = []
for i in range(org_mod.shape[0]):
for j in range(org_mod.shape[1]):
temp.append(int(str(org_mod[i, j]).split('.')[0]))
org_final = matrix(temp).reshape(org_mod.shape[0], org_mod.shape[1])
print "temp"
print temp
print "org_final"
print org_final
return org_final
if __name__ == '__main__':
pwd = list("fin")
org = matrix(array([[6, 24, 1], [13, 16, 10], [20, 17, 15]]))
org_vm = get_vmatrix(org)
result = decode(pwd, org_vm)
print "".join(result)
# deorg = matrix(array([[8, 5, 10], [21 , 8, 21], [21, 12, 8]]))
# result = decode(result, deorg)
# print "".join(result)
# pwd = "wjamdbkdeibr"
# pwd = list(pwd)
# org = matrix(array([[1, 2, 3], [4, 5, 6], [7, 8, 10]]))
# org_vm = get_vmatrix(org)
# print org_vm
# print "Your flag is :" + "".join(decode(pwd, org_vm))