import random def gcd(a, b): while b != 0: a, b = b, a % b return a def coprime(a, b): return gcd(a, b) == 1 def primes(N): primes = [] for target_num in range(2, N): is_prime = True for i in range(2, target_num): if target_num % i == 0: is_prime = False if is_prime: primes.append(target_num) return primes list_of_primes = primes(100) p, q = 0, 0 while p == q: p = random.choice(list_of_primes) q = random.choice(list_of_primes) N = p * q F = (p - 1) * (q - 1) while True: e = random.randint(2, F - 1) if coprime(e, F): break d = 0 while True: d += 1 if (d * e) % F == 1 and d != e: break message = input('Введите сообщение: ') enc_set = [ord(char) for char in message] # Шифрование rsa_enc = [pow(char, e, N) for char in enc_set] rsa_enc_text = ''.join(chr(char) for char in rsa_enc) print('Зашифрованное сообщение: ', rsa_enc_text) # Дешифрование decryption = [pow(char, d, N) for char in rsa_enc] decryption_set = ''.join(chr(char) for char in decryption) print('Расшифрованное сообщение: ', decryption_set)