gpt4 book ai didi

python - 使用python-rsa加密消息时出现溢出异常

转载 作者:行者123 更新时间:2023-12-05 06:35:37 26 4
gpt4 key购买 nike

我使用的是 Python 3 和 Python-rsa ( https://stuvel.eu/rsa ),在加密消息时我总是遇到同样的错误:

> Traceback (most recent call last):
``File "client.py", line 65, in <module>
msgg = encrypt_text(pubkey, msg)
File "client.py", line 54, in encrypt_text
return rsa.encrypt(msg.encode(), pubkey)
File "C:\Users\N0t_an_admin\AppData\Local\Programs\Python\Python36-32\lib\s
-packages\rsa\pkcs1.py", line 170, in encrypt
padded = _pad_for_encryption(message, keylength)
File "C:\Users\N0t_an_admin\AppData\Local\Programs\Python\Python36-32\lib\s
-packages\rsa\pkcs1.py", line 87, in _pad_for_encryption
' space for %i' % (msglength, max_msglength))
OverflowError: 1 bytes needed for message, but there is only space for -10

代码:

def get_server_pub():
pubkey = listener("serverpub").decode("utf-8")
pubkey = pubkey.strip(";")
xd = rsa.PublicKey(n= int(pubkey[0]), e= int(pubkey[1]))
return xd

def encrypt_text(pubkey, msg):
return rsa.encrypt(msg.encode(), pubkey)


if __name__ == '__main__':
print("Hello")
if os.path.isfile('ckeys.json') == False:
keys = createkeys()
write_to_json_file('ckeys.json', keys)
pubkey = get_server_pub()
while True:
msg = input("Message: ")
msgg = encrypt_text(pubkey, msg)
recived = listener(msgg)
if not recived:
continue
print(recived)

文档说:

OverflowError – when the message is too large to fit in the padded block.

最佳答案

检查那个库的代码,应该归咎于这个部分

def _pad_for_encryption(message, target_length):
r"""Pads the message for encryption, returning the padded message.
:return: 00 02 RANDOM_DATA 00 MESSAGE
>>> block = _pad_for_encryption(b'hello', 16)
>>> len(block)
16
>>> block[0:2]
b'\x00\x02'
>>> block[-6:]
b'\x00hello'
"""

max_msglength = target_length - 11
msglength = len(message)

if msglength > max_msglength:
raise OverflowError('%i bytes needed for message, but there is only'
' space for %i' % (msglength, max_msglength))

这个方法在这里被调用

keylength = common.byte_size(pub_key.n)
padded = _pad_for_encryption(message, keylength)

似乎 key 长度计算不正确,因此最大长度为 -10。检查您是否正在获取公钥。

关于python - 使用python-rsa加密消息时出现溢出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49677614/

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