【发布时间】:2018-04-05 16:50:06
【问题描述】:
我正在使用 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 – 当消息太大而无法放入填充块时。
【问题讨论】:
-
你用什么作为消息?
-
@Alfabravo 一个字符串。来自 msg = input("消息:")