Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 892985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:08:53+00:00 2026-05-15T14:08:53+00:00

I have id_rsa.pub key generated by ssh-keygen. How can I programmatically convert id_rsa.pub files

  • 0

I have id_rsa.pub key generated by ssh-keygen.
How can I programmatically convert id_rsa.pub files to RSA DER formatted keys?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:08:53+00:00Added an answer on May 15, 2026 at 2:08 pm

    If you use ssh-keygen to generate a key:

    $ ssh-keygen
    

    Then you can just use openssl to pull out the public key and write it in the DER format like this:

    $ openssl rsa -in id_rsa -out pub.der -outform DER -pubout
    writing RSA key
    

    You can view the DER output as PEM like this:

    $ openssl rsa -in pub.der -inform DER -pubin -text
    

    I don’t use Ruby, so I don’t know how easy it is to use OpenSSL from Ruby.

    Edit: I answered too quickly — you wrote id_rsa.pub and you may not have the id_rsa itself. Another Stack Overflow question is for the reverse conversion, but the source code found there might help: Convert pem key to ssh-rsa format Once you have PEM you can use openssl to convert the PEM to DER.

    Edit, May 2014: Ruby has become my favorite programming language, and the original question (since edited) asked about Ruby. Here is code to read the id_rsa.pub (public key) and write an OpenSSL-generated, DER-formatted public key:

    require 'openssl'
    require 'base64'
    
    def read_length(s)
        # four bytes, big-endian
        length = s[0..3].unpack('N')[0]
    end
    
    def read_integer(s, length)
        # shift all bytes into one integer
        s[4..3 + length].unpack('C*').inject { |n, b| (n << 8) + b }
    end
    
    def cut(s, length)
        s[4 + length..-1]
    end
    
    def decode_pub(pub)
        # the second field is the Base64 piece needed
        s = Base64.decode64(pub.split[1])
    
        # first field reading "ssh-rsa" is ignored
        i = read_length(s)
        s = cut(s, i)
    
        # public exponent e
        i = read_length(s)
        e = read_integer(s, i)
        s = cut(s, i)
    
        # modulus n
        i = read_length(s)
        n = read_integer(s, i)
    
        [ e, n ]
    end
    
    def make_asn1(e, n)
        # Simple RSA public key in ASN.1
        e0 = OpenSSL::ASN1::Integer.new(e)
        n1 = OpenSSL::ASN1::Integer.new(n)
        OpenSSL::ASN1::Sequence.new([ e0, n1 ])
    end
    
    pub = File.read('id_rsa.pub')
    
    asn1 = make_asn1(*decode_pub(pub))
    
    # Let OpenSSL deal with converting from the simple ASN.1
    key = OpenSSL::PKey::RSA.new(asn1.to_der)
    
    # Write out the public key in both PEM and DER formats
    File.open('id_rsa.pem', 'w') { |f| f.write key.to_pem }
    File.open('id_rsa.der', 'w') { |f| f.write key.to_der }
    

    You can check the output with these openssl commands in the shell:

    $ openssl rsa -pubin -text -in id_rsa.pem
    $ openssl rsa -pubin -text -inform DER -in id_rsa.der
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such section in my ~/.hgrc config file [auth] repo.prefix = ssh://hguser@192.168.132.72/repos repo.key
I have just start using git and i can't get it to remember my
I have a table called rent on MySQL id, int (5), autoincrement PRIMARY KEY
Say I have an ID row (int) in a database set as the primary
In SQL Server 2005 I have an id field in a table that has
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
Hi I want to have two tables each have an INT id column which
I have two tables say, t1 and t2 that have t1.t1_id and t2.t2_id as
Right now, I have SELECT gp_id FROM gp.keywords WHERE keyword_id = 15 AND (SELECT
In a table, I have three columns - id, name, and count. A good

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.