加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

来自指数和模数字符串的Ruby RSA

发布时间:2020-12-16 21:08:18 所属栏目:百科 来源:网络整理
导读:我有一个RSA公钥模数和指数字符串. 我想从这两个字符串创建一个OpenSSL :: PKey :: RSA. 基本上他们来自: n =’长字符串’ e =’4字符串’ 我将如何在Ruby中执行此操作? 最终目标是将其带到JWT宝石. 更新 我目前在Ruby 2.3.1中,所以这有效: key = OpenSSL
我有一个RSA公钥模数和指数字符串.

我想从这两个字符串创建一个OpenSSL :: PKey :: RSA.

基本上他们来自:

> n =’长字符串’
> e =’4字符串’

我将如何在Ruby中执行此操作?
最终目标是将其带到JWT宝石.

更新

我目前在Ruby 2.3.1中,所以这有效:

key = OpenSSL::PKey::RSA.new
key.e = OpenSSL::BN.new(Base64.decode64(e),2)
key.n = OpenSSL::BN.new(Base64.decode64(n),2)

但是,它在升级期间不起作用.

解决方法

基于这个python实现,我以这种方式工作:

https://github.com/jpf/okta-jwks-to-pem/blob/master/jwks_to_pem.py

key = OpenSSL::PKey::RSA.new
    exponent = kid_header['e']
    modulus = kid_header['n']


    # Voila !
    key.set_key(base64_to_long(modulus),base64_to_long(exponent),nil)

    def base64_to_long(data)
      decoded_with_padding = Base64.urlsafe_decode64(data) + Base64.decode64('==')
      decoded_with_padding.to_s.unpack('C*').map do |byte|
        to_hex(byte)
      end.join.to_i(16)
    end

    def to_hex(int)
      int < 16 ? '0' + int.to_s(16) : int.to_s(16)
    end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读