1 year ago

#351394

test-img

TAN08

Python code to encrypt/decrypt csv files with asymmetric keys(public and private)

Aim: To encrypt/decrypt csv files(of various sizes) using asymmetric method(public and private keys) Solutions tried: We have been able to generate the keys , but not able to encrypt the csv files. Also, did not try decryption yet. Below is the code we tried: Solution 1: Asymmetric encryption-

#Asymmetric encryption-

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
private_key = rsa.generate_private_key(
    public_exponent=65537,
    key_size=2048,
    backend=default_backend()
)
public_key = private_key.public_key()
from cryptography.hazmat.primitives import serialization
pem = private_key.private_bytes(
   encoding=serialization.Encoding.PEM,
   format=serialization.PrivateFormat.PKCS8,
   encryption_algorithm=serialization.NoEncryption()
)
with open('private_key.pem', 'wb') as f:
 f.write(pem)


from cryptography.hazmat.primitives import serialization
pem = public_key.public_bytes(
   encoding=serialization.Encoding.PEM,
   format=serialization.PublicFormat.SubjectPublicKeyInfo
)
with open('public_key.pem', 'wb') as f:
 f.write(pem)


from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
with open("private_key.pem", "rb") as key_file:
   private_key = serialization.load_pem_private_key(
       key_file.read(),
       password=None,
       backend=default_backend()
   )

with open("public_key.pem", "rb") as key_file:
   public_key = serialization.load_pem_public_key(
       key_file.read(),
       backend=default_backend()
   )

#Encryption
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
file = f"/data/nas/infacloud/ODSREP/Scripts/PMNONGMP_MEDI.csv"
f = open(file, 'rb')
message = f.read()
f.close()
encrypted = public_key.encrypt(
   message,
   padding.OAEP(
       mgf=padding.MGF1(algorithm=hashes.SHA256()),
       algorithm=hashes.SHA256(),
       label=None
   )
)
with open('PMNONGMP_MEDI_en', 'wb') as encrypted_file:
   encrypted_file.write(encrypted)
     
#Decryption
#from cryptography.hazmat.primitives import hashes
#from cryptography.hazmat.primitives.asymmetric import padding
#file1 = f"/data/nas/infacloud/ODSREP/Scripts/filekey_enc.key"
#f1 = open(file1, 'rb')
#message1 = f1.read()
#f1.close()
#original_message = private_key.decrypt(
#        message1,
#        padding.OAEP(
#            mgf=padding.MGF1(algorithm=hashes.SHA256()),
#            algorithm=hashes.SHA256(),
#            label=None
#        )
# )
#with open('filekey_dec.key', 'wb') as decrypted_file:
#   decrypted_file.write(original_message) 

Error: Traceback (most recent call last): File "Encrypt2902.py", line 109, in label=None File "/opt/rh/rh-python36/root/lib64/python3.6/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 537, in encrypt return _enc_dec_rsa(self._backend, self, plaintext, padding) File "/opt/rh/rh-python36/root/lib64/python3.6/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 87, in _enc_dec_rsa return _enc_dec_rsa_pkey_ctx(backend, key, data, padding_enum, padding) File "/opt/rh/rh-python36/root/lib64/python3.6/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 151, in _enc_dec_rsa_pkey_ctx raise ValueError("Encryption/decryption failed.") ValueError: Encryption/decryption failed.

Solution2:

import sys
import rsa

#generate private & public key and write to respective files(Asymmetric keys)
publicKey, privateKey = rsa.newkeys(4096)
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 file for encrytion
skey=open('PMNONGMP_MEDI.csv','rb')
key=skey.read()

#open the public key file
pkey=open('publicKey.key','rb')
pkdata=pkey.read()

#load the file
pubkey=rsa.PublicKey.load_pkcs1(pkdata)

#encrypt the file 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= open('PMNONGMP_MEDI_E.csv','wb')
ekey.write(encrypted_key)

Error: Traceback (most recent call last): File "Encrypt2103.py", line 37, in encrypted_key=rsa.encrypt(key,pubkey) File "/opt/rh/rh-python36/root/lib/python3.6/site-packages/rsa/pkcs1.py", line 194, in encrypt padded = _pad_for_encryption(message, keylength) File "/opt/rh/rh-python36/root/lib/python3.6/site-packages/rsa/pkcs1.py", line 114, in _pad_for_encryption " space for %i" % (msglength, max_msglength) OverflowError: 2248 bytes needed for message, but there is only space for 501

python

encryption

rsa

pgp

encryption-asymmetric

0 Answers

Your Answer

Accepted video resources