Created
June 9, 2012 21:44
-
-
Save emboss/2902696 to your computer and use it in GitHub Desktop.
Save RSA public keys in the pre-1.9.3 PKCS#1 format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-----" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?
`