0
Well, I wish I could save the private key and publish it to use later to encrypt and if someone wants to decrypt my message, but every time the program generates a new key. How can I save the keys and then use it later to encrypt or decrypt?
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import de
#gerando chave provada
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
#gerando chave publica
public_key = private_key.public_key()
#Entrada do texto
mensagem = str(input("escreva uma mensagem:"))
bytes_mensagem = mensagem.encode('utf-8')
#Criptografando o texto
texto_cifrado = public_key.encrypt(
bytes_mensagem,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)
print(texto_cifrado)
# Descriptografando o texto
texto_descriptografado = private_key.decrypt(
texto_cifrado,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA1()),
algorithm=hashes.SHA1(),
label=None
)
)
print(texto_descriptografado)