Мне нужна помощь с использованием RSA-шифрования и расшифровки в Python.
Я создаю пару частных/открытых ключей, шифруя сообщение с ключами и записывая сообщение в файл. Затем я читаю зашифрованный текст из файла и дешифруя текст с помощью ключа.
У меня возникают проблемы с частью расшифровки. Как вы можете видеть в моем коде ниже, когда я помещаю в decrypted = key.decrypt(message)
, что программа работает, но дешифрованное сообщение снова зашифровывается. Похоже, что он не читает зашифрованный текст из файла.
Может ли кто-нибудь помочь мне написать этот код, чтобы расшифровка считывала зашифрованный текст из файла и затем использовала ключ для дешифрования зашифрованного текста?
import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate public and private keys
publickey = key.publickey # pub key export for exchange
encrypted = publickey.encrypt('encrypt this message', 32)
#message to encrypt is in the above line 'encrypt this message'
print 'encrypted message:', encrypted #ciphertext
f = open ('encryption.txt', 'w'w)
f.write(str(encrypted)) #write ciphertext to file
f.close()
#decrypted code below
f = open ('encryption.txt', 'r')
message = f.read()
decrypted = key.decrypt(message)
print 'decrypted', decrypted
f = open ('encryption.txt', 'w')
f.write(str(message))
f.write(str(decrypted))
f.close()