python - 解密 RSA 会话密钥的正确方法
问题描述
我正在使用 pycryptodomex 进行 RSA 加密、解密。 我关注这篇文章
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import AES, PKCS1_OAEP
file_in = open("encrypted_data.bin", "rb")
private_key = RSA.import_key(open("private.pem").read())
enc_session_key, nonce, tag, ciphertext = \
[ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]
# Decrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(private_key)
session_key = cipher_rsa.decrypt(enc_session_key)
# Decrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
data = cipher.decrypt_and_verify(ciphertext, tag)
它显示以下错误
ValueError Traceback (most recent call last) in 11 # 用公钥解密会话密钥 12 cipher_rsa = PKCS1_OAEP.new(private_key) ---> 13 session_key = cipher_rsa.decrypt(enc_session_key) 14 15 # 用公钥解密数据AES 会话密钥
C:\ProgramData\Anaconda3\lib\site-packages\Cryptodome\Cipher\PKCS1_OAEP.py in decrypt(self, ciphertext) 165 # Step 1b and 1c 166 if len(ciphertext) != k or k 167 raise ValueError("Ciphertext长度不正确。") 168 # 步骤 2a (O2SIP) 169 ct_int = bytes_to_long(ciphertext)
ValueError:长度不正确的密文。
代码有什么问题吗?
解决方案
推荐阅读
- typescript - 是否可以让 TS Vite 使用 ESM 并让 TS Gulp 在同一个项目中使用 Common JS?
- javascript - 在没有数据库的情况下,在 JavaScript 中存储和处理数据的最佳解决方案是什么?
- amazon-web-services - 通过 API 创建 AWS 安全组的问题
- c# - Linux/MacOS 上的 C# 绝对文件路径
- html - 单击播放按钮时歌曲未播放
- python - 如何使用 sqlite3 删除数据库中的关键字及其旁边的所有内容
- datastage - 我如何将昨天的值设置为 datastage 中数字参数的默认值?
- react-native - 世博会相机暂停和恢复视频录制
- r - 连接到 Google Analytics API 时 R 中的错误(curl 中的错误,已达到超时)
- amazon-web-services - (InvalidParameterValueException) 调用 CreateFunction 操作时:为函数定义的角色不能由 Lambda 承担