简体   繁体   English

如何使用RSA加密来加密长度超过53个字节的消息?

[英]How to encrypt message longer than 53 bytes using RSA encryption?

Hey guys I am using rsa module import rsa in python for encrypting message longer than 53 bytes. 大家好,我在python中使用rsa模块import rsa来加密长度超过53个字节的消息。 But seems like the message length limit for rsa.encrypt(message, private_key) is only 53 bytes. 但是似乎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"

But for length greater than 53: 但长度大于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

Is there any way I could encrypt message longer than that? 有什么办法可以使消息加密的时间更长?

Use a bigger key. 使用更大的密钥。

RSA PKCS#1 encryption is limited to ((KeySize/8) - 11) bytes of payload. RSA PKCS#1加密限制为有效负载的((KeySize / 8)-11)个字节。 Based on your numbers you are using RSA-512 (which is “too easy” to break, you should really be using 1024 or 2048-bit RSA). 根据您的数字,您正在使用RSA-512(“太容易了”,您实际上应该使用1024或2048位RSA)。

The most common use for RSA encryption is to encrypt an AES key, and then send the encrypted AES key plus the AES-encrypted message: a scheme known as hybrid encryption. RSA加密最常见的用途是先加密AES密钥,然后发送加密的AES密钥和AES加密的消息:一种称为混合加密的方案。 Since AES keys are small (16, 24, or 32 bytes) even small RSA can transport them. 由于AES密钥很小(16、24或32个字节),因此即使很小的RSA也可以传输它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM