Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python加密包RSA——将私钥保存到数据库_Python_Encryption_Cryptography - Fatal编程技术网

Python加密包RSA——将私钥保存到数据库

Python加密包RSA——将私钥保存到数据库,python,encryption,cryptography,Python,Encryption,Cryptography,我想用pythoncryptographylibrary中的RSA加密一些东西。 () 首先,我有我的秘密消息和两种类型的密钥(公共密钥和私有密钥): 现在我可以用公钥加密msg了: from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding ciphertext = public_key.encrypt( SECERT,

我想用python
cryptography
library中的RSA加密一些东西。 ()

首先,我有我的秘密消息和两种类型的密钥(公共密钥和私有密钥):

现在我可以用公钥加密msg了:

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding

ciphertext = public_key.encrypt(
    SECERT,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA1()),
        algorithm=hashes.SHA1(),
        label=None
    )
)
太好了!但由于要解密此消息,我需要使用
私钥

plaintext = private_key.decrypt(
    ciphertext,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA1()),
        algorithm=hashes.SHA1(),
        label=None
    )
)
一切正常,唯一的问题是——我需要将私钥保存到数据库中,然后解密消息。不能为此目的使用RSA类实例

也许我使用了错误的工具,或者只是不太了解这个库,但到目前为止,我还没有在文档中找到答案


非常感谢您的帮助:)

您可以在不加密的情况下序列化私钥

pem = private_key.private_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PrivateFormat.TraditionalOpenSSL,
    encryption_algorithm=serialization.NoEncryption()
)
pem_data = pem.splitlines()[0]
将pem_数据存储到数据库中,并在需要时作为私钥从pem重新加载

pem = private_key.private_bytes(
    encoding=serialization.Encoding.PEM,
    format=serialization.PrivateFormat.TraditionalOpenSSL,
    encryption_algorithm=serialization.NoEncryption()
)
pem_data = pem.splitlines()[0]