From f94f122e2e68eefd1d3c7ed450ffda95462f09f2 Mon Sep 17 00:00:00 2001 From: Szymon P Date: Wed, 3 Dec 2025 16:53:57 +0100 Subject: [PATCH] lab --- .../laby/szyfrhilla.py | 76 +++++++++++++------ 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfrhilla.py b/uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfrhilla.py index 3dd8ecf..09cbe90 100644 --- a/uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfrhilla.py +++ b/uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfrhilla.py @@ -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) \ No newline at end of file + 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)