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
| import math import sympy from sympy import init_printing, pprint from sympy import Matrix from sympy.vector import matrix_to_vector, CoordSysCartesian init_printing()
def decrypt(matrix, words): cipher = '' M = Matrix(matrix) M = M.inv_mod(64) length = len(M) d = {} d2 = {} alph = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789_{}"
for x in range(len(alph)): d[alph[x]] = x d2[x] = alph[x] count = 0 l = []
for ch in words: if (count+1) % (8+1) == 0: m = Matrix(l) dot_pr_m = M*m n = [] for i in dot_pr_m: cipher += d2[i % 64] count = 0 l = [] l.append(d[ch]) count += 1 if (count+1) % (8+1) == 0: m = Matrix(l) dot_pr_m = M*m n = [] for i in dot_pr_m: cipher += d2[i % 64] return cipher
if __name__ == '__main__': secret = [[54, 53, 28, 20, 54, 15, 12, 7], [32, 14, 24, 5, 63, 12, 50, 52], [63, 59, 40, 18, 55, 33, 17, 3], [63, 34, 5, 4, 56, 10, 53, 16], [35, 43, 45, 53, 12, 42, 35, 37], [20, 59, 42, 10, 46, 56, 12, 61], [26, 39, 27, 59, 44, 54, 23, 56], [32, 31, 56, 47, 31, 2, 29, 41]] ciphertext = "7Nv7}dI9hD9qGmP}CR_5wJDdkj4CKxd45rko1cj51DpHPnNDb__EXDotSRCP8ZCQ" print(ciphertext) print(decrypt(secret, ciphertext))
|