无法生成OpenSSL :: PKey对象

时间:2019-05-13 10:20:56

标签: ruby-on-rails ruby

我想使用公共密钥生成一个openSSL :: Pkey,但是我收到以下错误消息“ OpenSSL :: PKey :: RSAError:PUB密钥或PRIV密钥:嵌套asn1错误”

这是公钥:

  

----- BEGIN公钥-----   MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAttI6ImgD74PhHWVqnrqSOmStboplyL02DB3 / nc2iyDdYLzXoBIQN + NPMBPlsZlEKuKjsg5Ycfp6VjcmncM3CH9MGIr + Lmbj1HZmO / jJGJ84RPhzYOiZuElzs3seIcOtOa3BpFeqRsXJlrf1IVBKVU3erka5ACLVyrsjmp / VXMx5QjPD0qXARMGb6rDewTkyg3pGz07Y7rZgXkTl54ase + XaPegOankxdEQhVjPdrk7eSlIQS5Ni7FAcSyTOtYoPgiP7W0PolOMHvpFg96CHjc8V8xDsFdh0wbHd49WcKxiJMIfL65VUIW5aob9fw05a5FczyASa0iNtRiICy / QnCqQIDAQAB   ----- END公钥-----

这是生成错误的代码

OpenSSL::PKey::RSA.new (Base64.decode64(public_key))

任何人都知道如何修复它,并且可以告诉您密钥出了什么问题。谢谢

1 个答案:

答案 0 :(得分:0)

  1. 您不需要手动解码base64。
  2. 请确保密钥的格式正确:BEGIN PUBLIC KEYEND PUBLIC KEY应该放在单独的行上。

工作示例:

require "openssl"

key = "-----BEGIN PUBLIC KEY-----\n" \
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAttI6ImgD74PhHWVqnrqSOmStboplyL02DB3/nc2iyDdYLzXoBIQN+NPMBPlsZlEKuKjsg5Ycfp6VjcmncM3CH9MGIr+Lmbj1HZmO/jJGJ84RPhzYOiZuElzs3seIcOtOa3BpFeqRsXJlrf1IVBKVU3erka5ACLVyrsjmp/VXMx5QjPD0qXARMGb6rDewTkyg3pGz07Y7rZgXkTl54ase+XaPegOankxdEQhVjPdrk7eSlIQS5Ni7FAcSyTOtYoPgiP7W0PolOMHvpFg96CHjc8V8xDsFdh0wbHd49WcKxiJMIfL65VUIW5aob9fw05a5FczyASa0iNtRiICy/QnCqQIDAQAB\n" \
"-----END PUBLIC KEY-----"

p OpenSSL::PKey::RSA.new(key)