RSA加密溢出错误

RSA encryption OverflowError

我目前正在为一个列表做RSA加密功能。但是我在加密时遇到了一些错误。我该如何解决?

提高'OverflowError: 6304 bytes needed for message, but there is only space for 245'

完整追溯:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ryan2\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\ryan2\Documents\GitHub\Fake_product_identification_system\blockchain_project\blockchain_project\system.py", line 116, in <lambda>
    bChain.RSA_encryption(bChain.blockchain_Apple.chain)
  File "c:\Users\ryan2\Documents\GitHub\Fake_product_identification_system\blockchain_project\blockchain_project\blockchain.py", line 106, in RSA_encryption
    result = rsa.encrypt(txt.encode("ascii"), public_key)
  File "C:\Users\ryan2\AppData\Local\Programs\Python\Python310\lib\site-packages\rsa\pkcs1.py", line 194, in encrypt
    padded = _pad_for_encryption(message, keylength)
  File "C:\Users\ryan2\AppData\Local\Programs\Python\Python310\lib\site-packages\rsa\pkcs1.py", line 112, in _pad_for_encryption
    raise OverflowError(
OverflowError: 6304 bytes needed for message, but there is only space for 245
def Gen_key():
    (public_key, private_key) = rsa.newkeys(2048)
    with open(
        r".\blockchain_project\blockchain_project\key\publicKey.pem", "wb"
    ) as key:
        key.write(public_key.save_pkcs1("PEM"))
    with open(
        r".\blockchain_project\blockchain_project\key\privateKey.pem", "wb"
    ) as key:
        key.write(private_key.save_pkcs1("PEM"))


def RSA_encryption(txt):
    (public_key, private_key) = get_keys()
    txt = json.dumps(json.dumps(txt))
    result = rsa.encrypt(txt.encode("ascii"), public_key)
    return result


def RSA_decryption(RSA_content):
    try:
        (public_key, private_key) = get_keys()
        result = rsa.decrypt(RSA_content, private_key).decode("ascii")
        result = json.loads(json.loads(result))
        return result
    except:
        return False


def get_keys():
    with open(
        r".\blockchain_project\blockchain_project\key\publicKey.pem", "rb"
    ) as key:
        publicKey = rsa.PublicKey.load_pkcs1(key.read())
    with open(
        r".\blockchain_project\blockchain_project\key\privateKey.pem", "rb"
    ) as key:
        privateKey = rsa.PrivateKey.load_pkcs1(key.read())
    return privateKey, publicKey


Gen_key()

2048 位密钥的长度为 256 字节。由于开销,它一次只能编码 245 个字节。您可以将明文切成一定大小的块,或者使用另一种算法来加密整个消息和 RSA-encrypt 密钥。

https://stuvel.eu/python-rsa-doc/usage.html

跟进

这是您的代码,修改后以 245 字节块的形式执行操作。

请注意,您的 get_keys returns (private,public),但您的代码都符合预期 (public,private)。我不知道你想用 double-JSON 编码做什么,但我已经删除了它:

import rsa
import json

def Gen_key():
    (public_key, private_key) = rsa.newkeys(2048)
    with open( "publicKey.pem", "wb") as key:
        key.write(public_key.save_pkcs1("PEM"))
    with open( "privateKey.pem", "wb") as key:
        key.write(private_key.save_pkcs1("PEM"))


def RSA_encryption(txt):
    (public_key, private_key) = get_keys()
    txt = json.dumps(txt)
    result = []
    for n in range(0,len(txt),245):
        part = txt[n:n+245]
        result.append( rsa.encrypt(part.encode("ascii"), public_key) )
    print(len(result),len(result[0]))
    return b''.join(result)


def RSA_decryption(RSA_content):
    (public_key, private_key) = get_keys()
    result = []
    for n in range(0,len(RSA_content),256):
        part = RSA_content[n:n+256]
        result.append( rsa.decrypt(part, private_key).decode("ascii") )
    result = json.loads(''.join(result))
    return result


def get_keys():
    with open( "publicKey.pem", "rb") as key:
        publicKey = rsa.PublicKey.load_pkcs1(key.read())
    with open( "privateKey.pem", "rb") as key:
        privateKey = rsa.PrivateKey.load_pkcs1(key.read())
    return publicKey, privateKey


Gen_key()
r = RSA_encryption( { 'x': open('x.py').read() } )
print( r )
print( RSA_decryption( r ) )