Python PyCrypto Cyphertext即使使用一个字符的数据,长度也不正确
我想加密/解密包含在.csv文件中的一组数据。 我使用以下代码生成RSA公钥/私钥:Python PyCrypto Cyphertext即使使用一个字符的数据,长度也不正确,python,python-3.x,encryption,pycrypto,pycryptodome,Python,Python 3.x,Encryption,Pycrypto,Pycryptodome,我想加密/解密包含在.csv文件中的一组数据。 我使用以下代码生成RSA公钥/私钥: 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.c
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')
然后我用这段代码加密,没有任何问题:
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()
我可以通过修改此行来修改数据的分离方式:
rep=[rep[i:i+n]表示范围内的i(0,len(rep),n)]
这行按n个字符组分隔我的数据
这里是我解密数据的代码,它引发:
ValueError:长度不正确的密文
我试图对一个示例文件进行编码,但它引发了这个错误。然后我尝试直接在Python解释器上处理一个只包含一个字符的文件。加密工作正常,但解密失败,错误与上述相同。代码的主要问题是使用换行符来分隔加密数据块。加密数据可能已经包含换行符,因此尝试将加密数据拆分为行可能会产生部分块,这将导致解密时出现的
ValueError
第二个问题是加密文件是以文本模式打开的。处理加密数据时,以二进制模式打开文件。加密的字节
不太可能解码为str
,因此使用文本模式将导致编码或解码错误
此版本的代码适用于:
import functools
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
if __name__ == '__main__':
# Encrypt
with open('public.pem', 'r') as fp:
pub = fp.read()
fp.close()
public = RSA.importKey(pub)
# tockage du fichier dans une variable rep
with open('test.csv', 'r') as fichier:
rep = fichier.read()
# liminations des spaces
rep = rep.replace(' ', '')
# ncodage pour type bytes
rep = rep.encode()
cipher = PKCS1_OAEP.new(public)
# decoupage en mot de 10 chars
rep = [rep[i:i+10] for i in range(0, len(rep), 10)]
# Open the file in binary mode so we can write bytes.
with open('encrypted.csv', 'wb') as fichier2:
for i in rep:
fichier2.write(cipher.encrypt(i))
# Decrypt
with open('private.pem', 'r') as fk:
priv = fk.read()
private = RSA.importKey(priv)
CHUNK_SIZE = 256
# Open the file in binary mode so we can read bytes.
with open('encrypted.csv', 'rb') as fichier:
# Create an iterator that will return chunks of the correct size.
chunker = iter(functools.partial(fichier.read, CHUNK_SIZE), b'')
rep = list(chunker)
cipher = PKCS1_OAEP.new(private)
with open('decrypted.csv', 'w') as fichier2:
for i in rep:
decrypted_line = cipher.decrypt(i)
fichier2.write(decrypted_line.decode())
这段代码不是在换行符上拆分加密数据,而是以256字节的块从文件中读取,因为加密过程似乎为每个输入块生成256字节。我不是密码学家,所以这可能并不总是正确的。在这种情况下,最好一步加密(或解密)所有数据。谢谢,我不知道functools.partial与iter的结合使用。我不认为我的文件是以“字节”模式打开的。事实上,它也必须改变数据。如果有人想使用此代码,请小心,如果不在范围(0,len(rep),10)]中的i使用行
rep=[rep[i:i+10],
,则在这种情况下必须强制rep的类型,否则在最终循环中将其解释为int。
import functools
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
if __name__ == '__main__':
# Encrypt
with open('public.pem', 'r') as fp:
pub = fp.read()
fp.close()
public = RSA.importKey(pub)
# tockage du fichier dans une variable rep
with open('test.csv', 'r') as fichier:
rep = fichier.read()
# liminations des spaces
rep = rep.replace(' ', '')
# ncodage pour type bytes
rep = rep.encode()
cipher = PKCS1_OAEP.new(public)
# decoupage en mot de 10 chars
rep = [rep[i:i+10] for i in range(0, len(rep), 10)]
# Open the file in binary mode so we can write bytes.
with open('encrypted.csv', 'wb') as fichier2:
for i in rep:
fichier2.write(cipher.encrypt(i))
# Decrypt
with open('private.pem', 'r') as fk:
priv = fk.read()
private = RSA.importKey(priv)
CHUNK_SIZE = 256
# Open the file in binary mode so we can read bytes.
with open('encrypted.csv', 'rb') as fichier:
# Create an iterator that will return chunks of the correct size.
chunker = iter(functools.partial(fichier.read, CHUNK_SIZE), b'')
rep = list(chunker)
cipher = PKCS1_OAEP.new(private)
with open('decrypted.csv', 'w') as fichier2:
for i in rep:
decrypted_line = cipher.decrypt(i)
fichier2.write(decrypted_line.decode())