gpt4 book ai didi

python - 即使只有一个字符数据,PyCrypto Cyphertext 的长度也不正确

转载 作者:行者123 更新时间:2023-11-28 18:03:49 25 4
gpt4 key购买 nike

我想加密/解密 .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.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] for i in range(0, len(rep), n)]

这一行将我的数据按 n 个字符组分隔

这是我解密数据的代码,它引发了:

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()

我尝试对示例文件进行编码,但它引发了这个 ValueError 。然后我尝试直接在 Python 解释器上处理一个只包含一个字符的文件。加密工作正常,但解密出现与上述相同的错误。

最佳答案

代码的主要问题是使用换行符来分隔加密数据 block 。加密数据可能已经包含换行符,因此尝试将加密数据拆分成行可能会产生部分块,这将引发您在解密时看到的 ValueError

第二个问题是加密文件以文本模式打开。处理加密数据时,以二进制模式打开文件。加密的 bytes 不太可能被解码为 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 字节的 block 从文件中读取,因为加密过程似乎为每个输入 block 生成 256 字节。我不是密码学家,所以这可能并不总是正确的。在这种情况下,一步加密(或解密)所有数据可能会更好。

关于python - 即使只有一个字符数据,PyCrypto Cyphertext 的长度也不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54752085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com