python rsa加密长度_RSA“长度必须等于密钥大小”

在使用Python密码库进行RSA加密和解密时,作者遇到一个错误:"Ciphertext length must be equal to key size."。加密过程正常,但解密时出现问题。错误出现在解密函数中,具体原因未明。代码中调用了加载公钥、私钥,加密密码,签名数据以及解密的函数。回溯错误定位到SQLAlchemy的某个文件,但实际问题可能与ORM无关,而与RSA解密的参数有关。
摘要由CSDN通过智能技术生成

我目前正在尝试使用python密码库和RSA加密和解密密码。密钥生成工作正常,加密工作正常,但在解密密码时,我会遇到以下回溯错误消息:raise ValueError("Ciphertext length must be equal to key size.")

我的加密和解密代码如下所示:

^{pr2}$

运行此脚本时出错:crypter = AppCryptography()

backend_public_key = crypter.load_public_key(dir_path + "/util/keys/backend_public_key.pem")

frontend_private_key = crypter.load_private_key(dir_path + "/util/keys/frontend_private_key.pem")

encrypted_password = crypter.encrypt_password(backend_public_key, password)

signature = crypter.sign_data(frontend_private_key, password)

backend_private_key = crypter.load_private_key(dir_path + "/util/keys/backend_private_key.pem")

cleartext = crypter.decrypt(backend_private_key, encrypted_password)

stacktrace显示错误来自解密函数,但我无法看到错误在函数定义中的位置。在File "/Users/Me/anaconda/envs/flask_dream/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 411, in _initialize_instance

return manager.original_init(*mixed[1:], **kwargs)

File "/Users/Me/anaconda/envs/flask_dream/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 650, in _declarative_constructor

setattr(self, k, kwargs[k])

File "/Users/Me/Desktop/Projects/Flask-Dream/project_app/models.py", line 114, in password

cleartext = crypter.decrypt(backend_private_key, encrypted_password)

File "/Users/Me/Desktop/Projects/Flask-Dream/project_app/util/crypto.py", line 121, in decrypt

label=None

File "/Users/Me/anaconda/envs/flask_dream/lib/python2.7/site-packages/cryptography/hazmat/backends/openssl/rsa.py", line 397, in decrypt

raise ValueError("Ciphertext length must be equal to key size.")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值