mat = [[11, 3], [7, 4]]
inv_mat = [[16, 1], [11, 5]]
abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def pos(l):
return abc.index(l)
def letter(x):
return abc[x % 26]
def hill(m, s):
if len(s) % 2 == 1:
s += 'A'
ans = ""
for i in range(0, len(s), 2):
x1, x2 = pos(s[i]), pos(s[i + 1])
y1, y2 = m[0][0] * x1 + m[0][1] * x2, m[1][0] * x1 + m[1][1] * x2
s1, s2 = letter(y1), letter(y2)
ans += s1 + s2
return ans
# Chiffrement
print(hill(mat, "ST"))
wiki = "TEXTEACRYPTER"
wiki_mat = [[3, 5], [6, 17]]
print(hill(wiki_mat, wiki))
# Défi!!!!!!!!!!!!
secret = "RFYYVURFPUVOGATDNJGUCROIOXHCVBDUDEGANCSQVBPWMWMWMW"
key = [[7, 11], [19, 14]]
def decode(m, s):
[[a, b], [c, d]] = m
det = a * d - b * c
inv_det = pow(det, -1, 26)
mat = [[d * inv_det, - b * inv_det], [-c * inv_det, a * inv_det]]
return hill(mat, s)
msg = decode(key, secret)
print(msg)
To embed this program on your website, copy the following code and paste it into your website's HTML: