如何使用 RSA 加密对超过 53 字节的消息进行加密?
How to encrypt message longer than 53 bytes using RSA encryption?
大家好,我在 python 中使用 rsa 模块 import rsa
来加密长度超过 53 字节的消息。但似乎 rsa.encrypt(message, private_key)
的消息长度限制只有 53 个字节。
>>> rsa.encrypt(b'A'*53, private_key)
b"(\xe9\xbf\xcc?\x18'\xb4Q@\xce\xb5=\xce#\x91\xb3\xe2+QT\d\xe4\xaf\x07\xdb\x01\xe2\x83\xc6-\xfe\x03\xa5]\x9a\xad\x90\xb1L\xab\xed\xf3zWw\xccM\xa4.Yw!{\xf4\x08\x95\x9ex7\xbb\x9b\xff"
但长度大于 53 时:
>>> rsa.encrypt(b'A'*54, private_key)
Traceback (most recent call last):
File "<pyshell#216>", line 1, in <module>
rsa.encrypt(b' '*54, s_pub)
File "/usr/local/lib/python3.7/dist-packages/rsa/pkcs1.py", line 172, in encrypt
padded = _pad_for_encryption(message, keylength)
File "/usr/local/lib/python3.7/dist-packages/rsa/pkcs1.py", line 89, in _pad_for_encryption
' space for %i' % (msglength, max_msglength))
OverflowError: 54 bytes needed for message, but there is only space for 53
有没有什么方法可以加密比这更长的消息?
使用更大的密钥。
RSA PKCS#1 加密仅限于 ((KeySize/8) - 11) 字节的有效负载。根据您使用的数字,您使用的是 RSA-512(“太容易”无法破解,您实际上应该使用 1024 或 2048 位 RSA)。
RSA 加密最常见的用途是加密 AES 密钥,然后发送加密的 AES 密钥和 AES 加密的消息:一种称为混合加密的方案。由于 AES 密钥很小(16、24 或 32 字节),即使是小型 RSA 也可以传输它们。
大家好,我在 python 中使用 rsa 模块 import rsa
来加密长度超过 53 字节的消息。但似乎 rsa.encrypt(message, private_key)
的消息长度限制只有 53 个字节。
>>> rsa.encrypt(b'A'*53, private_key)
b"(\xe9\xbf\xcc?\x18'\xb4Q@\xce\xb5=\xce#\x91\xb3\xe2+QT\d\xe4\xaf\x07\xdb\x01\xe2\x83\xc6-\xfe\x03\xa5]\x9a\xad\x90\xb1L\xab\xed\xf3zWw\xccM\xa4.Yw!{\xf4\x08\x95\x9ex7\xbb\x9b\xff"
但长度大于 53 时:
>>> rsa.encrypt(b'A'*54, private_key)
Traceback (most recent call last):
File "<pyshell#216>", line 1, in <module>
rsa.encrypt(b' '*54, s_pub)
File "/usr/local/lib/python3.7/dist-packages/rsa/pkcs1.py", line 172, in encrypt
padded = _pad_for_encryption(message, keylength)
File "/usr/local/lib/python3.7/dist-packages/rsa/pkcs1.py", line 89, in _pad_for_encryption
' space for %i' % (msglength, max_msglength))
OverflowError: 54 bytes needed for message, but there is only space for 53
有没有什么方法可以加密比这更长的消息?
使用更大的密钥。
RSA PKCS#1 加密仅限于 ((KeySize/8) - 11) 字节的有效负载。根据您使用的数字,您使用的是 RSA-512(“太容易”无法破解,您实际上应该使用 1024 或 2048 位 RSA)。
RSA 加密最常见的用途是加密 AES 密钥,然后发送加密的 AES 密钥和 AES 加密的消息:一种称为混合加密的方案。由于 AES 密钥很小(16、24 或 32 字节),即使是小型 RSA 也可以传输它们。