Created
September 21, 2018 13:27
-
-
Save arthurazs/6395c146d69573b23460802e348a844e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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