def get_key_from_text(cleartext, cryptedtext): # Y: crypted text, X: clear text, A: Key # AX = Y # A = Y.X^-1 cleartext = cleartext.upper() cryptedtext = cryptedtext.upper() clear_array = [] for e in cleartext: clear_array.append(ord(e)-65) crypted_array = [] for e in cryptedtext: crypted_array.append(ord(e)-65) print crypted_array matrix_y = [[crypted_array[0], crypted_array[1]], [crypted_array[2], crypted_array[3]]] found = False count = 0 while not found: print "\n\ntry : " + str((count % 4) + 1) matrix_x = [[clear_array[count + 0], clear_array[count + 1]], [clear_array[count + 2], clear_array[count + 3]]] print "matrix x" print matrix_x print "matrix y" print matrix_y import numpy try: x_inverse = tools.modMatInv(matrix_x, 27) print "x inverse" print x_inverse y_dot_xinv = numpy.dot(matrix_y, x_inverse) print "y_dot_xinv = " print y_dot_xinv print "K = " print numpy.mod(y_dot_xinv, 27) found = True except ValueError: count += 4 print " valueError"
def decrypt_text(message, key): # Pour decrypter il suffit d'inverser la matrice clé return encrypt_text(message, tools.modMatInv(key, 27))
def decrypt_image(input_filename, output_filename, key): encrypt_image(input_filename, output_filename, tools.modMatInv(key, 256))