@qtip2001/

# Hill Cipher

## No description

Files
• main.py
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
```
```# Quentin Bishop
# 22 Oct 2018

def prepare_string(s, alphabet):
s = s.upper()
k = list(s)
for char in k:
if char not in alphabet:
k.remove(char)
return "".join(k).upper().replace(" ", "")

def c2i(c, alphabet):
if c in alphabet:
return alphabet.index(c)
else:
print("Error")

def i2c(i, alphabet):
return alphabet[i]

def mod_inverse(a, m):
for x in range(m):
if (x*a)%m == 1:
return x
return -1

def matrix_inverse(matrix, mod):
det = matrix[0]*matrix[3] - matrix[1]*matrix[2]
inv = mod_inverse(det, mod)
if inv == -1:
#return [-1, -1, -1, -1]
#print("Error")
return 0
else:
new_mat = [None, None, None, None]
new_mat[0] = ( inv * matrix[3]) % mod
new_mat[2] = (-inv * matrix[2]) % mod
new_mat[1] = (-inv * matrix[1]) % mod
new_mat[3] = ( inv * matrix[0]) % mod
return new_mat

def mult_2x1(matrix1, matrix2, mod):
new_mat = [None, None]
new_mat[0] = (matrix1[0]*matrix2[0]+matrix1[1]*matrix2[1]) % mod
new_mat[1] = (matrix1[2]*matrix2[0]+matrix1[3]*matrix2[1]) % mod
return new_mat

def mult_2x2(matrix1, matrix2, mod):
new_mat = [None, None, None, None]
new_mat[0] = (matrix1[0]*matrix2[0]+matrix1[1]*matrix2[2]) % mod
new_mat[1] = (matrix1[0]*matrix2[1]+matrix1[1]*matrix2[3]) % mod
new_mat[2] = (matrix1[2]*matrix2[0]+matrix1[3]*matrix2[2]) % mod
new_mat[3] = (matrix1[2]*matrix2[1]+matrix1[3]*matrix2[3]) % mod
return new_mat

def hill_encode(plaintext, matrix, mod, alphabet):
if len(plaintext)%2 == 1:
plaintext = plaintext+"X"
encoded = []
for x in range(0, int(len(plaintext)/2)):
d = [c2i(plaintext[2*x], alphabet), c2i(plaintext[2*x+1], alphabet)]
k = mult_2x1(matrix, d, mod)
encoded.append(i2c(k[0], alphabet))
encoded.append(i2c(k[1], alphabet))
return "".join(encoded)

def hill_decode(ciphertext, matrix, mod, alphabet):
inv = matrix_inverse(matrix, mod)
if inv == 0:
return 0
decoded = []
for x in range(0, int(len(ciphertext)/2)):
d = [c2i(ciphertext[2*x], alphabet), c2i(ciphertext[2*x+1], alphabet)]
k = mult_2x1(inv, d, mod)
decoded.append(i2c(k[0], alphabet))
decoded.append(i2c(k[1], alphabet))
return "".join(decoded)

def determine_mat(plain, cipher, mod, alphabet):
matrix = [c2i(plain[0], alphabet),c2i(plain[2], alphabet),c2i(plain[1], alphabet),c2i(plain[3], alphabet)]
answer = [c2i(cipher[0], alphabet),c2i(cipher[2], alphabet),c2i(cipher[1], alphabet),c2i(cipher[3], alphabet)]
mi = matrix_inverse(matrix, mod)
if mi == 0:
return 0

cipher = "JYnbMGhQXyUo?OAUuCtwWFPNckxtjsANHeJ.uyCFDSMGZvuCvKJYnbMGhQXyUo?Of!'',BAI.G'd''Z.SpvHfqz:erXy'GWFHWeoTfNXZ:yqcSfteWPBPQUowF"

m = determine_mat("help", "okay", len(alpha), alpha)
#print(m)
#print(hill_decode("okay", m, len(alpha), alpha))
#print(hill_encode(cipher, [27,13,5,14],len(alpha),alpha))
print(matrix_inverse([2,5,3,20], 256))

'''alphabet = ""
for x in range(33, 94):
alphabet = alphabet + chr(x)

users = {}
common = []

line = line.strip()
(w1,w2) = line.split()
users[w1] = w2
pss.close()

line = line.strip()
common.append(line.upper())
com.close()

matrices = []
best = []'''

'''for a in users:
print(a)
for b in common:
if len(b) > 4:
y = determine_mat(b,users[a],len(alphabet),alphabet)
if y != 0:
x = hill_decode(users[a], y, len(alphabet), alphabet)
if x != 0:
if x.startswith(b):
#print(x)
#print(x)
if y in matrices:
best.append(y)
print(x)
matrices.append(y)'''

'''for x in best:
print(x)
for a in users:
print(hill_decode(users[a],x,len(alphabet), alphabet))'''

'''for b in mx:
q = input()
print(b)
for a in users:
print(hill_decode(users[a],  b,  len(alphabet), alphabet))'''

'''for a in users:
x = hill_decode(users[a], [32, 17, 27, 13], len(alphabet),alphabet)
print(a,x)'''```
Fetching token