mirror of
https://github.com/szymon-jozef/ZUT-notatki.git
synced 2026-09-07 12:10:40 +02:00
lab
This commit is contained in:
@@ -1,50 +1,82 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
def letter_to_num(letter):
|
||||
return ord(letter.upper()) - ord('A')
|
||||
|
||||
|
||||
def num_to_letter(num):
|
||||
return chr(num + ord('A'))
|
||||
|
||||
|
||||
def cipher(text: str):
|
||||
k = np.array([[3, 3], [2, 5]])
|
||||
def matrix_mod_inv(key):
|
||||
det = int(round(np.linalg.det(key)))
|
||||
det_mod = det % 26
|
||||
det_inv = pow(det_mod, -1, 26)
|
||||
|
||||
adj = np.array([[key[1, 1], -key[0, 1]],
|
||||
[-key[1, 0], key[0, 0]]])
|
||||
|
||||
return (det_inv * adj) % 26
|
||||
|
||||
|
||||
def cipher(text: str, key: int):
|
||||
text_upper = text.upper().replace(' ', '')
|
||||
|
||||
if not validate_key(key):
|
||||
print("Macierz klucz nie jest odwracalna, wybierz inną")
|
||||
exit(1)
|
||||
|
||||
if len(text_upper) % 2 != 0:
|
||||
text_upper += "X"
|
||||
|
||||
text_number = [letter_to_num(letter) for letter in text_upper]
|
||||
nums = [letter_to_num(letter) for letter in text_upper]
|
||||
mat = np.array(nums).reshape(-1, 2).T
|
||||
|
||||
text_matrix = np.array(text_number).reshape(-1, 2).T
|
||||
c = (key @ mat) % 26
|
||||
return "".join(num_to_letter(n) for n in c.T.flatten())
|
||||
|
||||
c = (k @ text_matrix) % 26
|
||||
|
||||
cipher_text = ''.join(num_to_letter(num) for num in c.T.flatten())
|
||||
|
||||
return cipher_text
|
||||
|
||||
def decipher(text: str):
|
||||
k = np.array([[3, 3], [2, 5]])
|
||||
k_inv = np.linalg.inv(k)
|
||||
def decipher(text: str, key: int):
|
||||
text_upper = text.upper().replace(' ', '')
|
||||
if not validate_key(key):
|
||||
print("Macierz klucz nie jest odwracalna, wybierz inną")
|
||||
exit(1)
|
||||
|
||||
key_inv = matrix_mod_inv(key)
|
||||
|
||||
if len(text_upper) % 2 != 0:
|
||||
text_upper += "X"
|
||||
|
||||
text_number = [letter_to_num(letter) for letter in text_upper]
|
||||
nums = [letter_to_num(letter) for letter in text_upper]
|
||||
mat = np.array(nums).reshape(-1, 2).T
|
||||
|
||||
text_matrix = np.array(text_number).reshape(-1, 2).T
|
||||
p = (key_inv @ mat) % 26
|
||||
return "".join(num_to_letter(n) for n in p.T.flatten())
|
||||
|
||||
c = (k_inv @ text_matrix) % 26
|
||||
|
||||
cipher_text = ''.join(num_to_letter(num) for num in c.T.flatten())
|
||||
def validate_key(key) -> bool:
|
||||
key = np.asarray(key)
|
||||
|
||||
if key.shape != (2, 2):
|
||||
return False
|
||||
|
||||
if not np.all((0 <= key) & (key < 26)):
|
||||
return False
|
||||
|
||||
det = int(round(np.linalg.det(key)))
|
||||
det_mod = det % 26
|
||||
|
||||
return np.gcd(det_mod, 26) == 1
|
||||
|
||||
return cipher_text
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_text = "Test"
|
||||
cipher = cipher(test_text)
|
||||
#decipher = decipher(cipher)
|
||||
print("Text: ", test_text)
|
||||
print("cipher: ", cipher)
|
||||
key = np.array([[3, 3],
|
||||
[2, 5]])
|
||||
|
||||
test_text = "TEST"
|
||||
ciphered_text = cipher(test_text, key)
|
||||
deciphered_text = decipher(ciphered_text, key)
|
||||
print("Text:", test_text)
|
||||
print("Cipher:", ciphered_text)
|
||||
print("Decipher:", deciphered_text)
|
||||
|
||||
Reference in New Issue
Block a user