简体   繁体   中英

How to store private and public key into pem file generated by rsa module of python

I am using below module in python for rsa encryption. https://github.com/sybrenstuvel/python-rsa it can be installed by pip as follows pip3 install rsa

I have read of using this module here: https://github.com/sybrenstuvel/python-rsa/blob/master/doc/usage.rst

and I am able to generate public and private key and also able to encrypt the message using public key using the following code

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)

but I want to store the private key into some file, As you can read in the usage file of this module, they have provide the method to read the key from the .pem file so it means I should store the keys also in pem file but I am not getting how can I store the key generated by this module into the file.

When I print the private key generated by this module, It gives me the following output

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

And to store this key if i create a file key.pem and store this key into the file. The content of the file are

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

and when I try to read it it gives me error like padding error etc please tell me how can i store these keys and then read it to for decryption purpose.

The Github site of Python RSA refers via its homepage to this documentation , according to which the library has dedicated methods to export the keys in PKCS#1 format (methods rsa. PublicKey#save_pkcs1() or rsa.PrivateKey#save_pkcs1() ) or to import them (classmethods rsa.PublicKey.load_pkcs1() or rsa.PrivateKey.load_pkcs1() ). As encodings PEM (text) or DER (binary) is supported, eg:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM