'Python code to decrypt an already encrypted symmetric key file with private asymmetric key
Aim: We need to encrypt multiple csv files using PGP encryption. Solutions tried: We have already tried the symmetric encryption / decryption and its working fine. We tried using only the asymmetric method and it was not working for encryption only. So we decided to go with both together i.e. 1.generating symmetric key and the asymmetric (public and private pair) keys 2.encrypting the csv files using symmetric key 3.encrypting the symmetric key with the public key 4.decrypting the symmetric key with the private key We are able to do all the first 3 steps. But, facing error in the 4th step.
Below :
-Symmetric method
import sys
import rsa
-generate symmetric keys & write to a file
from cryptography.fernet import Fernet
key=Fernet.generate_key()
k= open('symmetric.key','wb')
k.write(key)
k.close()
-generate private & public key and write to respective files(Asymmetric keys)
publicKey, privateKey = rsa.newkeys(512)
pukey=open('publicKey.key','wb')
pukey.write(publicKey.save_pkcs1('PEM'))
pukey.close()
prkey=open('privateKey.key','wb')
prkey.write(privateKey.save_pkcs1('PEM'))
prkey.close()
-read the symmetric key for encrypting purpose
skey=open('symmetric.key','rb')
key=skey.read()
-create the cipher
cipher=Fernet(key)
-open the public key file
pkey=open('publicKey.key','rb')
pkdata=pkey.read()
-load the file
pubkey=rsa.publicKey.load_pkcs1(pkdata)
-encrypt the symmetric key with public key
encrypted_key=rsa.encrypt(key,pubkey)
-write the encrypted symmetric key to a file
ekey= open('encrypted_symm_key.txt','wb')
ekey.write(encrypted_key)
-Till above, we are able to execute fine.Issue is with decryption.
-Decryption of the symmetric key and writing to a file.
-load the file to decrypt using the private key
prkey=open('privateKey.key','rb')
prkdata=prkey.read()
privatekey=rsa.privateKey.load_pkcs1(prkdata)
-read the encrypted key file and decrypt using private key
e=open('encrypted_symm_key.txt','rb')
ekey=e.read()
dpubkey=rsa.decrypt(ekey,privatekey)
cipher=Fernet(dpubkey)
-Writing the decrypted data to file
decrypted_Data=cipher.decrypt(ekey)
print(decrypted_Data.decode())
dkey= open('decrypted_symm_key.txt','wb')
dkey.write(dpubkey)
Error message: Traceback (most recent call last): File "Encrypt2103.py", line 47, in privatekey=rsa.privateKey.load_pkcs1(prkdata) AttributeError: module 'rsa' has no attribute 'privateKey'
Solution 1:[1]
Try rsa.PrivateKey.load_pkcs1(prkdata, format='PEM'). It is a class method, it is not a method that is directly in the rsa module, and class names are case sensitive.
Instead, privateKey with a small cap is an object instance of that class. But you'd normally only get such an instance after calling load_pkcs1.
I've added format='PEM' as you should already know it is PEM, so there is no need for a detection routine that might go wrong.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
