【问题标题】:How to encrypt message longer than 53 bytes using RSA encryption?如何使用 RSA 加密对超过 53 个字节的消息进行加密?
【发布时间】:2019-09-09 03:22:37
【问题描述】:

大家好,我在 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

有什么方法可以让消息加密的时间更长?

【问题讨论】:

  • 如果您只使用 128000 位密钥,您可以加密长达 15989 字节的消息。
  • 为什么使用 RSA 加密?

标签: python encryption rsa


【解决方案1】:

使用更大的键。

RSA PKCS#1 加密仅限于 ((KeySize/8) - 11) 字节的有效负载。根据您的数字,您使用的是 RSA-512(这“太容易”破解,您应该使用 1024 或 2048 位 RSA)。

RSA 加密最常见的用途是加密 AES 密钥,然后发送加密的 AES 密钥和 AES 加密的消息:这种方案称为混合加密。由于 AES 密钥很小(16、24 或 32 字节),即使是小型 RSA 也可以传输它们。

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2011-03-01
    • 1970-01-01
    • 2014-01-31
    • 2011-02-08
    • 2017-11-08
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多