【问题标题】:How to store private and public key into pem file generated by rsa module of python如何将私钥和公钥存储到python的rsa模块生成的pem文件中
【发布时间】:2021-01-06 23:47:00
【问题描述】:

我在 python 中使用下面的模块进行rsa 加密。 https://github.com/sybrenstuvel/python-rsa 可以通过pip安装如下pip3 install rsa

我在这里读过使用这个模块:https://github.com/sybrenstuvel/python-rsa/blob/master/doc/usage.rst

我能够生成公钥和私钥,还可以使用以下代码使用公钥加密消息

import rsa
pubKey, priKey = rsa.newkeys(1024)
print("Public key: ",pubKey)
print("Private key: ",priKey)

msg = "vinay kumar shukla".encode('utf8')
print("Message without encryption:",msg)

msgEnc = rsa.encrypt(msg, pubKey)
print("encrypted message:",msgEnc)

但我想将私钥存储到某个文件中,正如您可以在此模块的使用文件中看到的那样,他们提供了从.pem 文件中读取密钥的方法,因此这意味着我应该存储密钥也在pem 文件中,但我不知道如何将此模块生成的密钥存储到文件中。

当我打印这个模块生成的私钥时,它给了我以下输出

PrivateKey(8647494628885176696347621451257950700244697066080136004727560667550523829374958314554596272099668415472356485265099323795859891239567212729331504592847959, 65537, 80620400968137738399200401402545247384675983145016755403642821122013062179228267902734675759971390409853232911734749752372599473758015795215587460952985, 6068766834338315918835399238932380729501892761840591316766866213831546939444337723, 1424917922362067845458877023625137340152906787710542109910805914692739733)

如果我创建文件key.pem 并将此密钥存储到文件中,则存储此密钥。该文件的内容是

-----BEGIN RSA PRIVATE KEY-----
PrivateKey(8647494628885176696347621451257950700244697066080136004727560667550523829374958314554596272099668415472356485265099323795859891239567212729331504592847959, 65537, 80620400968137738399200401402545247384675983145016755403642821122013062179228267902734675759971390409853232911734749752372599473758015795215587460952985, 6068766834338315918835399238932380729501892761840591316766866213831546939444337723, 1424917922362067845458877023625137340152906787710542109910805914692739733)
-----END RSA PRIVATE KEY-----

当我尝试读取它时,它给了我padding error etc 之类的错误,请告诉我如何存储这些密钥,然后读取它以进行解密。

【问题讨论】:

    标签: python encryption rsa


    【解决方案1】:

    Python RSA 的 Github 站点通过其homepage 引用此documentation,根据该库具有以 PKCS#1 格式导出密钥的专用方法(方法 rsa. PublicKey#save_pkcs1()rsa.PrivateKey#save_pkcs1())或导入它们(类方法rsa.PublicKey.load_pkcs1()rsa.PrivateKey.load_pkcs1())。支持 PEM(文本)或 DER(二进制)编码,例如:

    import rsa
    
    # Use at least 2048 bit keys nowadays, see e.g. https://www.keylength.com/en/4/
    publicKey, privateKey = rsa.newkeys(2048) 
    
    # Export public key in PKCS#1 format, PEM encoded 
    publicKeyPkcs1PEM = publicKey.save_pkcs1().decode('utf8') 
    print(publicKeyPkcs1PEM)
    # Export private key in PKCS#1 format, PEM encoded 
    privateKeyPkcs1PEM = privateKey.save_pkcs1().decode('utf8') 
    print(privateKeyPkcs1PEM)
    
    # Save and load the PEM encoded keys as you like
    
    # Import public key in PKCS#1 format, PEM encoded 
    publicKeyReloaded = rsa.PublicKey.load_pkcs1(publicKeyPkcs1PEM.encode('utf8')) 
    # Import private key in PKCS#1 format, PEM encoded 
    privateKeyReloaded = rsa.PrivateKey.load_pkcs1(privateKeyPkcs1PEM.encode('utf8')) 
    
    plaintext = "vinay kumar shukla".encode('utf8')
    print("Plaintext: ", plaintext)
    
    ciphertext = rsa.encrypt(plaintext, publicKeyReloaded)
    print("Ciphertext: ", ciphertext)
     
    decryptedMessage = rsa.decrypt(ciphertext, privateKeyReloaded)
    print("Decrypted message: ", decryptedMessage)
    

    【讨论】:

      猜你喜欢
      • 2017-12-23
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2010-11-21
      相关资源
      最近更新 更多