Skip to content

Instantly share code, notes, and snippets.

@emboss

emboss/gist:2902696

Created Jun 9, 2012
Embed
What would you like to do?
Save RSA public keys in the pre-1.9.3 PKCS#1 format
require 'openssl'
require 'base64'
rsa = OpenSSL::PKey::RSA.new(2048)
modulus = rsa.n
exponent = rsa.e
ary = [OpenSSL::ASN1::Integer.new(modulus), OpenSSL::ASN1::Integer.new(exponent)]
pub_key = OpenSSL::ASN1::Sequence.new(ary)
base64 = Base64.encode64(pub_key.to_der)
#This is the equivalent to the PKCS#1 encoding used before 1.9.3
pem = "-----BEGIN RSA PUBLIC KEY-----\n#{base64}-----END RSA PUBLIC KEY-----"
@rabajaj0509

This comment has been minimized.

Copy link

@rabajaj0509 rabajaj0509 commented Mar 20, 2019

Hello, this is useful but i am facing this issue:

`[20] pry(#)> ary = [OpenSSL::ASN1::Integer.new(rsa_public['n']), OpenSSL::ASN1::Integer]
14:00:41 rails.1 | => [#<OpenSSL::ASN1::Integer:0x00007fd881238988
14:00:41 rails.1 | @indefinite_length=false,
14:00:41 rails.1 | @tag=2,
14:00:41 rails.1 | @tag_class=:UNIVERSAL,
14:00:41 rails.1 | @tagging=nil,
14:00:41 rails.1 | @value=
14:00:41 rails.1 | "oc7PjsJehmf7z_FCQWKbm1v8QddVDC5dZu9mM_DtmhHmuGeD5DGTUf2tAZSDG_hj6tX8FPAKP7n0dGCsDtVrKj-_x3ck9pMreAcoSaCP0YHZOArecyymSZIL6-RY4Fm-r_fF_c9JDEkftp7l_El_aui0hD400V9VkFiSCDnJYES7wSfsWmcUv7hx_wb1jrseGoIMFtA5CiRhSwaCeHHrFRVd6zkuCZ6ACp9OL0dCVOVRosKioRFH9hkyzixAdIEqRYU2YxaIPgPV2hNoLAKoXiVtYV2yB0RR6hwHeGLPijGX5B9xe4Tee5qpeXlaELG6kKaUKn5ZrH185GEMQlKQ8Q">,
14:00:41 rails.1 | #<OpenSSL::ASN1::Integer:0x00007fd881238960
14:00:41 rails.1 | @indefinite_length=false,
14:00:41 rails.1 | @tag=2,
14:00:41 rails.1 | @tag_class=:UNIVERSAL,
14:00:41 rails.1 | @tagging=nil,
14:00:41 rails.1 | @value="AQAB">]

14:01:11 rails.1 | [21] pry(#)> pub_key = OpenSSL::ASN1::Sequence.new(ary)
14:01:11 rails.1 | => #<OpenSSL::ASN1::Sequence:0x00007fd88b990988
14:01:11 rails.1 | @indefinite_length=false,
14:01:11 rails.1 | @tag=16,
14:01:11 rails.1 | @tag_class=:UNIVERSAL,
14:01:11 rails.1 | @tagging=nil,
14:01:11 rails.1 | @value=
14:01:11 rails.1 | [#<OpenSSL::ASN1::Integer:0x00007fd881238988
14:01:11 rails.1 | @indefinite_length=false,
14:01:11 rails.1 | @tag=2,
14:01:11 rails.1 | @tag_class=:UNIVERSAL,
14:01:11 rails.1 | @tagging=nil,
14:01:11 rails.1 | @value=
14:01:11 rails.1 | "oc7PjsJehmf7z_FCQWKbm1v8QddVDC5dZu9mM_DtmhHmuGeD5DGTUf2tAZSDG_hj6tX8FPAKP7n0dGCsDtVrKj-_x3ck9pMreAcoSaCP0YHZOArecyymSZIL6-RY4Fm-r_fF_c9JDEkftp7l_El_aui0hD400V9VkFiSCDnJYES7wSfsWmcUv7hx_wb1jrseGoIMFtA5CiRhSwaCeHHrFRVd6zkuCZ6ACp9OL0dCVOVRosKioRFH9hkyzixAdIEqRYU2YxaIPgPV2hNoLAKoXiVtYV2yB0RR6hwHeGLPijGX5B9xe4Tee5qpeXlaELG6kKaUKn5ZrH185GEMQlKQ8Q">,
14:01:11 rails.1 | #<OpenSSL::ASN1::Integer:0x00007fd881238960
14:01:11 rails.1 | @indefinite_length=false,
14:01:11 rails.1 | @tag=2,
14:01:11 rails.1 | @tag_class=:UNIVERSAL,
14:01:11 rails.1 | @tagging=nil,
14:01:11 rails.1 | @value="AQAB">]>

14:01:23 rails.1 | [22] pry(#)> base64 = Base64.encode64(pub_key.to_der)
14:01:23 rails.1 | TypeError: Cannot convert into OpenSSL::BN
14:01:23 rails.1 | from (pry):13:in `to_der'

Any idea how i can resolve this?
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.