RSA加密溢出错误

问题描述 投票:0回答:2

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

它引发“溢出错误:消息需要 6304 字节,但只有 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()
python blockchain
2个回答
2
投票

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

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

跟进

这是修改为以 245 字节块执行操作的代码。

请注意,您的

get_keys
返回
(private,public)
,但您的代码都期望
(public,private)
。我不知道你想用双 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 ) )

-1
投票

我相信它会引发大小限制错误,因为您创建的密钥能够加密有限大小的字符串数据。您可以通过实施其他加密算法来避免这种情况,或者可以结合对称和非对称加密来安全、更快地传输批量数据。

您可以查看以下文章了解更多详情 -

https://alifurqan.in/Implement-fast-secure-cryption.html

© www.soinside.com 2019 - 2024. All rights reserved.