I want to encrypt/decrypt a set of data which is contained in a .csv file. I generate my RSA public/private keys with this code :
import Crypto
from Crypto.PublicKey import RSA
key = RSA.generate(2048)
k = key.exportKey('PEM')
p = key.publickey().exportKey('PEM')
with open('private.pem', 'w') as kf:
kf.write(k.decode())
kf.close()
with open('public.pem', 'w') as pf:
pf.write(p.decode())
pf.close()
with open('private.pem','r') as fk:
priv = fk.read()
fk.close()
with open('public.pem','r') as fp:
pub = fp.read()
fp.close()
privat = RSA.importKey(priv)
public = RSA.importKey(pub)
if key == privat:
print('Private key has been successfuly write')
if key.publickey() == public:
print('Public key has been successfuly write')
Then I encrypt with this code without any problem:
import Crypto
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
with open('public.pem','r') as fp:
pub = fp.read()
fp.close()
public = RSA.importKey(pub)
#stockage du fichier dans une variable rep
fichier = open('test.csv', 'r')
rep = fichier.read()
fichier.close()
#eliminations des spaces
rep = rep.replace(' ', '')
#encodage pour type bytes
rep = rep.encode()
#decoupage en mot de 10 chars
rep = [rep[i:i+10] for i in range(0, len(rep), 10)]
cipher = PKCS1_OAEP.new(public)
fichier2 = open('encrypted.csv', 'a')
for i in rep:
encrypted_line = cipher.encrypt(i)
fichier2.write(str(encrypted_line))
fichier2.write('\n')
fichier2.close()
I can modify how my data are separate by modifying this line :
rep = [rep[i:i+n] for i in range(0, len(rep), n)]
This line separate my data by groups of n chars
Here my code to decrypt the data, it raises:
ValueError: Ciphertext with incorrect length.
import Crypto
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
with open('private.pem','r') as fk:
priv = fk.read()
fk.close()
private = RSA.importKey(priv)
fichier = open('encrypted.csv', 'r')
rep = fichier.read().splitlines()
fichier.close()
cipher = PKCS1_OAEP.new(private)
fichier2 = open('decrypted.csv', 'a')
for i in rep:
decrypted_line = cipher.decrypt(i)
decrypted_line = decrypted_line.decode('utf-8')
fichier2.write(str(encrypted_line))
fichier2.close()
I tried to encode a sample file and it raised this ValueError . Then I try to work with a file which contain only one char directly on the Python interpreter. Encryption worked well but decryption broke with the same error as above.