update jakiś chyba nwm, nie pamiętam

This commit is contained in:
2026-01-03 23:45:56 +01:00
parent 35ab230e4f
commit 110d8ee27f
6 changed files with 5 additions and 5 deletions

View File

@@ -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)

View File

@@ -0,0 +1,57 @@
def cypher(text, offset):
n_text = ""
for char in text:
if char.isalpha() and char.islower():
pos = ((ord(char) - ord('a') + offset) % 26) + ord('a')
n_text += chr(pos)
if char.isalpha() and char.isupper():
pos = ((ord(char) - ord('A') + offset) % 26) + ord('A')
n_text += chr(pos)
return n_text
def decypher(text, offset):
n_text = ""
for char in text:
if char.isalpha() and char.islower():
pos = ((ord(char) - ord('a') - offset) % 26) + ord('a')
n_text += chr(pos)
if char.isalpha() and char.isupper():
pos = ((ord(char) - ord('A') - offset) % 26) + ord('A')
n_text += chr(pos)
return n_text
def force_decypher(text):
n_text = ""
print("Możliwe wyniki: ")
for offset in range(1, 26):
print("Przesunięcie", offset, ":")
for char in text:
if char.isalpha() and char.islower():
pos = ((ord(char) - ord('a') + offset) % 26) + ord('a')
n_text += chr(pos)
if char.isalpha() and char.isupper():
pos = ((ord(char) - ord('A') + offset) % 26) + ord('A')
n_text += chr(pos)
print(n_text)
n_text = ""
print("------")
if __name__ == "__main__":
tryb = input("Wybierz tryb:\ns - szyfrowanie\nd - deszyfrowanie\nf - siłowe odszyfrowanie\n")
offset = 3
match tryb:
case "s":
text = input("Podaj słowo do zaszyfrowania: ")
print("Słowo przed zaszyfrowaniem: ", text)
print("Aktualne przesunięcie: ", offset)
print("Zaszyfrowane słowo: ", cypher(text, offset))
case "d":
text = input("Podaj słowo do odszyfrowania: ")
print("Słowo przed odszyfrowaniem: ", text)
print("Aktualne przesunięcie: ", offset)
print("Odszyfrowane słowo: ", decypher(text, offset))
case "f":
text = input("Podaj słowo do odszyfrowania: ")
force_decypher(text)
case _:
print("Niepoprawny wybór!")

View File

@@ -0,0 +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 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"
nums = [letter_to_num(letter) for letter in text_upper]
mat = np.array(nums).reshape(-1, 2).T
c = (key @ mat) % 26
return "".join(num_to_letter(n) for n in c.T.flatten())
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"
nums = [letter_to_num(letter) for letter in text_upper]
mat = np.array(nums).reshape(-1, 2).T
p = (key_inv @ mat) % 26
return "".join(num_to_letter(n) for n in p.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
if __name__ == "__main__":
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)