Skip to content

Instantly share code, notes, and snippets.

@arthurazs
Created September 21, 2018 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurazs/6395c146d69573b23460802e348a844e to your computer and use it in GitHub Desktop.
Save arthurazs/6395c146d69573b23460802e348a844e to your computer and use it in GitHub Desktop.
# inspired by
# https://www.datasciencedata.com/2018/09/encryption-using-matrix-python.html
import numpy as np
from string import ascii_lowercase
ck = np.matrix = [[2, 1], [3, 2]] # Encryption key
dk = np.matrix = [[2, -1], [-3, 2]] # Decryption key
code_to_text = {}
text_to_code = {}
for number, letter in enumerate(ascii_lowercase):
code_to_text[number] = letter
text_to_code[letter] = number
def to_code(text):
new_text = [text_to_code[s] for s in text]
return np.asarray(new_text).reshape(2, len(new_text)//2)
def to_text(code_array):
code_list = code_array.ravel().tolist()
text_list = [code_to_text[num] for num in code_list]
return ''.join(text_list)
password = 'arthur' # passwd
code_password = to_code(password) # passwd to code
cipher = np.dot(ck, code_password) # ciphered code
decipher = np.dot(dk, cipher) # deciphered from ciphered code
text_password = to_text(decipher) # passwd from code
print(password) # original passwd
print(text_password) # deciphered passwd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment