Я пытаюсь зашифровать сообщение, используя заданный открытый ключ pubkey
, который я считал из файла.Когда я пытаюсь запустить мою программу, я получаю сообщение об ошибке, в котором говорится, что у объекта
'str' нет атрибута 'encrypt'.
Я уже пытался зашифровать открытый ключ, но все еще получаю ту же ошибку.Поскольку мне нужно использовать этот конкретный открытый ключ, я не могу сгенерировать другой случайный открытый ключ.
Ниже приведен мой код:
from Crypto.PublicKey import RSA
import math
def text_to_int(text):
"""
Converts text into an integer for use in encryption.
input: text - a plaintext message
output: integer - an integer encoding text
"""
integer = 0
for char in text:
if integer > 0:
integer = integer*256
integer = integer + ord(char)
return integer
#Read public key
with open('C:\\Users\\alan9\\Downloads\\Assignement2\\Supporting_Files\\HLand_Key.pub', 'rb') as f:
read_key = f.read()
# use the PUBLIC KEY to encrypt a message:
message = "attack early next week"
message_int = text_to_int(message)
ciphertext = read_key.encrypt(message_int, None)
#Write ciphertext to file
with open('C:\\Users\\alan9\\Downloads\\Assignement2\\Supporting_Files\\cipher.txt', 'w') as f_write:
f_write.write(ciphertext)
Я ожидаю, что выходные данные вернут зашифрованное сообщение в текстовом файле с именем cipher, используяуказанный открытый ключ.
Любая помощь будет оценена !!!