def decryprionMethodMatrix(file, key): try: with open(file, 'r') as fn: msg = fn.read() fn.close() matrix = readMatrixFile(key) archivo_desencriptado = decipher_hill(msg, matrix) with open(file, 'w') as fn: fn.write(archivo_desencriptado) except ValueError as e: print("Error Tecnico: " + str(e))
def test_decipher_hill(): A = Matrix(2, 2, [1, 2, 3, 5]) assert decipher_hill("CFIV", A) == "ABCD" A = Matrix(2, 2, [1, 0, 0, 1]) assert decipher_hill("ABCD", A) == "ABCD" assert decipher_hill("ABCD", A, symbols="ABCD") == "ABCD" A = Matrix(2, 2, [1, 2, 3, 5]) assert decipher_hill("CBAB", A, symbols="ABCD") == "ABCD" assert decipher_hill("CB", A, symbols="ABCD") == "AB" # n does not need to be a multiple of k assert decipher_hill("CFA", A) == "ABAA"
# Hill Cipher # over the hill - icectf-2016 # can also use sage like in this other chall: https://github.com/ctfs/write-ups-2015/tree/master/ghost-in-the-shellcode-2015/crypto/nikoli from sympy.crypto.crypto import encipher_hill, decipher_hill from sympy import Matrix alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789_{}" ciphertext = "7Nv7}dI9hD9qGmP}CR_5wJDdkj4CKxd45rko1cj51DpHPnNDb__EXDotSRCP8ZCQ" matrix = Matrix([[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]]) print decipher_hill(ciphertext, matrix, alphabet)
#pip install SymPy from sympy.crypto.crypto import encipher_hill, decipher_hill from sympy import Matrix def getMatrix(msg): matrix=[[0 for x in range(3)] for y in range(3)] k=0 for i in range(3): for j in range(3): matrix[i][j] = ord(msg[k])%65 k+=1 return matrix message=input("Enter text: ") key = Matrix(getMatrix("GYBNQKURP")) cipher = encipher_hill(message, key) print("Ciphered: ", cipher) print("Deciphered:", decipher_hill(cipher, key))
from sympy.crypto.crypto import encipher_hill, decipher_hill from sympy import Matrix key = Matrix([[1, 2], [3, 5]]) a = raw_input("Write the pure text: ") b = encipher_hill(a, key) c = decipher_hill(b, key) print b print c
def hill(msg, key, **kwargs): et = encipher_hill(msg, key, symbols=symbols, pad="0") dt = decipher_hill(et, key, symbols=symbols) return et, dt