python - 值错误: Ciphertext with incorrect length

标签 python rsa pycryptodome

我遇到一个问题: “假设您的 RSA 公钥因子为 p = 6323 和 q = 2833,并且 公共(public)指数 e 为 31。假设您收到密文 6627708。编写一个程序,将上述参数作为输入,并实现 RSA 解密函数以恢复明文。”

当尝试解密密文时,我收到错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-30-bb484f24f99a> in <module>
----> 1 cipher.decrypt((str(ciphertext)))

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Crypto/Cipher/PKCS1_OAEP.py in decrypt(self, ciphertext)
    165         # Step 1b and 1c
    166         if len(ciphertext) != k or k<hLen+2:
--> 167             raise ValueError("Ciphertext with incorrect length.")
    168         # Step 2a (O2SIP)
    169         ct_int = bytes_to_long(ciphertext)

ValueError: Ciphertext with incorrect length.

我的代码目前如下所示:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

n = 17913059
e = 31
p = 6323
q = 2833
d = 13861087
ciphertext = 6627708

key = RSA.construct(rsa_components=(n,e,d,p,q))
cipher = PKCS1_OAEP.new(key)

cipher.decrypt((str(ciphertext)))

我更想知道我是否走在正确的轨道上,或者完全偏离了轨道。我不太确定如何修复长度错误。我在想也许我需要像 AES 那样进行填充,但我不太确定。预先感谢您的帮助!

最佳答案

如果您有 cdn,则可以使用 RSA formula获取密文:

>>> pow(ciphertext, d, n)
205

这似乎是一条格式错误的消息(它们通常是十六进制或 ASCII 值),因此这可能只是一个示例问题。

您的问题源于pycryptodome's implementationRFC 7.1.2 ,其中指出:

C: ciphertext to be decrypted, an octet string of length k, where k = 2hLen + 2

地点:

hLen denotes the length in octets of the hash function output

因此,从技术上讲,您的密文太短,无法被 RSA 解密。

关于python - 值错误: Ciphertext with incorrect length,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58550102/

相关文章:

c++ - 使用 libxmlsec 从内存中加载 RSA 私钥

python - 使用故意弱私钥解密消息无限期挂起

python - 错误 : Failed building wheel for pycryptodome

python - Windows Python 上的加密解密编码

python - 让用户最近的城市的最佳方式? python / Django

python - 使用 open() 内部循环理解 - 获取目录中所有文件的文本内容列表

python - 如何正确地将 python 捆绑安装到 Windows 用户

security - 存储加密的 PEM block 安全吗?

java - 在java中验证C#中的rsa签名

python - 将字符串转换为列表 Python