From d99a77c790f6ac74c7d1dc5522f092429185dffb Mon Sep 17 00:00:00 2001 From: Szymon <98268045+czarnyspajdi@users.noreply.github.com> Date: Wed, 26 Nov 2025 11:13:56 +0100 Subject: [PATCH] Create szyfr_anachroniczny.py --- .../laby/szyfr_anachroniczny.py | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfr_anachroniczny.py diff --git a/uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfr_anachroniczny.py b/uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfr_anachroniczny.py new file mode 100644 index 0000000..60fe4b2 --- /dev/null +++ b/uczelnia/1_semestr/wprowadzenie_do_informatyki/laby/szyfr_anachroniczny.py @@ -0,0 +1,50 @@ +from math import gcd + +def cipher(text, a = 5, b = 8): + text = text.replace(" ", "").upper() + cipher_text = '' + for letter in text: + x = ord(letter) - ord('A') + y = (a * x + b) % 26 + encrypter_letter = chr(y + ord('A')) + cipher_text += encrypter_letter + + return cipher_text + +def decipher(text, a = 5, b = 8): + text = text.replace(" ", "").upper() + decipher_text = '' + for letter in text: + a_inv = 0 + found = False + for i in range(1, 26): + if (a * i) % 26 == 1: + found = True + a_inv = i + break + if not found: + print("Nie znaleziono odwrotności a modulo 26. Zamykanie programu...") + exit(1) + + y = ord(letter) - ord('A') + x = (a_inv * (y - b)) % 26 + plain_letter = chr(x + ord('A')) + + decipher_text += plain_letter + return decipher_text + +def validate_key(a): + if gcd(a, 26) != 1: + print("Niepoprawny klucz - brak odwrotności modulo 26") + return + print("Poprawny klucz!") + +if __name__ == '__main__': + text = input("Podaj tekst do zaszyfrowania: ") + print("Tekst przed szyfrowaniem: ", text) + cipher = cipher(text) + print("Tekst po zaszyfrowaniu: ", cipher) + decipher = decipher(cipher) + print("Tekst po odszyfrowaniu: ", decipher) + a = int(input("Podaj wartości klucza a: ")) + validate_key(a)