ruby-on-rails - 如何让 Ruby 读取 .cer 公共(public) ssl key ?

标签 ruby-on-rails encryption openssl

我正在开发一个需要电子支付模块的 RoR 网站。电子支付实现要求使用他们提供的公共(public) ssl key 对 xml 数据进行编码。

我尝试用 Ruby 做什么:

public_key = OpenSSL::PKey::RSA.new(File.read(public_key_file))

如果我只是尝试单独打开文件,它工作正常。但是 RSA.new() 方法返回以下错误:

OpenSSL::PKey::RSAError: Neither PUB key nor PRIV key:: nested asn1 error
    from (irb):5:in `initialize'
    from (irb):5:in `new'
    from (irb):5

根据我在在线文档中看到的内容,使用了 .pem 文件,但我的公钥类似于 public.cer。这可能是问题所在吗? key 本身似乎没问题,因为在电子支付公司提供的 PHP 示例中,相同的 public.cer 文件工作正常。

我做错了什么?

谢谢,

最佳答案

.cer 文件很可能是以 DER 编码的 X.509 证书。不幸的是,Ruby 没有公开 OpenSSL 接口(interface)来读取 DER 中的证书。因此,您需要先将 DER 转换为 PEM。这在 Ruby 中相当容易,

b64 = Base64.encode64(File::read(cert_file))
pem = "-----BEGIN CERTIFICATE-----\n#{b64}-----END CERTIFICATE-----\n"
cert = OpenSSL::X509::Certificate.new(pem)
public_key = cert.public_key

关于ruby-on-rails - 如何让 Ruby 读取 .cer 公共(public) ssl key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1914716/

相关文章:

ruby-on-rails - 在 simpleform 表单中手动设置表单输入字段的 ID?

android - android中的PBKDF2安全 key

java - 即使输入相同,AES/CBC/NoPadding 是否会生成两个不同的密文?

java - AES 解密错误 : android pad block corrupted

ssl - nginx 加载 ssl 证书失败

python - 由于无法识别 'cl',因此无法编译 openssl

ruby-on-rails - rake 数据库 :migrate issue postgresql

ruby-on-rails - 如何在 Rails 6 中配置 Jasmine?

ruby-on-rails - 回调更改的ActiveRecord属性?

node.js - 使用公钥验证 Node.JS 加密中的签名