python - 由于填充检查失败,Ruby OpenSSL RSA 解密失败

标签 python ruby encryption cryptography rsa

我正在尝试在 ruby​​ 中复制以下 python 代码。 python 代码工作正常,ruby 代码由于“填充检查失败”而失败。我可以在 Ruby 中使用私钥加密和解密字符串,但我需要处理的加密数据是从其他地方检索的。我不确定用于加密它的库甚至语言,但 Python 代码有效。

工作的 Python 代码:

def decrypt_secret(encrypted_base64, private_key):
  key = RSA.importKey(open(private_key, "r").read())
  pkey = PKCS1_OAEP.new(key, hashAlgo=SHA256)
  encrypted_blob = base64.urlsafe_b64decode(encrypted_base64)
  decrypted_string = pkey.decrypt(encrypted_blob)
  return decrypted_string

Ruby 代码失败“填充检查失败”

def decrypt_secret(encrypted_base64, private_key)
  key = File.read(private_key)
  pkey = OpenSSL::PKey::RSA.new(key)
  encrypted_blob = Base64.urlsafe_decode64(encrypted_base64)
  decrypted_string = pkey.private_decrypt(encrypted_blob, OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING)
  return decrypted_string
end 

最佳答案

我在 JOSE gem 中找到了所需的函数支持 SHA256 OAEP。以下代码完成这项工作:

require 'jose'
def decrypt_secret(encrypted_base64, private_key)
  key = File.read(private_key)
  pkey = OpenSSL::PKey::RSA.new(key)
  encrypted_blob = Base64.urlsafe_decode64(encrypted_base64)
  decrypted_string = JOSE::JWA::PKCS1::rsaes_oaep_decrypt(OpenSSL::Digest::SHA256, encrypted_blob, pkey)
  return decrypted_string
end

关于python - 由于填充检查失败,Ruby OpenSSL RSA 解密失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124464/

相关文章:

ssl - TLS 1.2 SHA384 : What should be the length of verify_data in "Finished" message?

python - 值错误: Unknown label type: 'continuous' while using Logistical Regression

ruby - 使用 Capybara 选择提示

security - 从 stdin 读取加密 key 是一种糟糕的安全做法吗?

c - 如何在 C 中实现 RSA 加密?

ruby - VIM:Ruby 方法与 ?和 !到底

python - 用Python的statsmodels的OLS线性回归进行曲线拟合时,公式中的常数如何选择?

python - 如何连接到我的远程 SQL 服务器

python - 遍历 jinja2 中的一个元组

ruby - 使用 WIN32OLE 的 Word 自动化