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

  • SEARCH
  • Home
  • 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 1075885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:20:01+00:00 2026-05-16T21:20:01+00:00

I’m looking for a way to programmatically create ssh compatible id_rsa and id_rsa.pub files

  • 0

I’m looking for a way to programmatically create ssh compatible id_rsa and id_rsa.pub files in Java.

I got as far as creating the KeyPair:

KeyPairGenerator generator;
generator = KeyPairGenerator.getInstance("RSA");
// or: generator = KeyPairGenerator.getInstance("DSA");
generator.initialize(2048);
keyPair = generator.genKeyPair();

I can’t figure out however how to create the String representation of the PrivateKey and PublicKey in the KeyPair.

  • 1 1 Answer
  • 1 View
  • 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-16T21:20:01+00:00Added an answer on May 16, 2026 at 9:20 pm

    The key format used by ssh is defined in the RFC #4253. The format for RSA public key is the following :

      string    "ssh-rsa"
      mpint     e /* key public exponent */
      mpint     n /* key modulus */
    

    All data type encoding is defined in the section #5 of RFC #4251. string and mpint (multiple precision integer) types are encoded this way :

      4-bytes word: data length (unsigned big-endian 32 bits integer)
      n bytes     : binary representation of the data
    

    for instance, the encoding of the string “ssh-rsa” is:

      byte[] data = new byte[] {0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a'};
    

    To encode the public :

       public byte[] encodePublicKey(RSAPublicKey key) throws IOException
       {
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           /* encode the "ssh-rsa" string */
           byte[] sshrsa = new byte[] {0, 0, 0, 7, 's', 's', 'h', '-', 'r', 's', 'a'};
           out.write(sshrsa);
           /* Encode the public exponent */
           BigInteger e = key.getPublicExponent();
           byte[] data = e.toByteArray();
           encodeUInt32(data.length, out);
           out.write(data);
           /* Encode the modulus */
           BigInteger m = key.getModulus();
           data = m.toByteArray();
           encodeUInt32(data.length, out);
           out.write(data);
           return out.toByteArray();
       }
    
       public void encodeUInt32(int value, OutputStream out) throws IOException
       {
           byte[] tmp = new byte[4];
           tmp[0] = (byte)((value >>> 24) & 0xff);
           tmp[1] = (byte)((value >>> 16) & 0xff);
           tmp[2] = (byte)((value >>> 8) & 0xff);
           tmp[3] = (byte)(value & 0xff);
           out.write(tmp);
       }
    

    To have a string représentation of the key just encode the returned byte array in Base64.

    For the private key encoding there is two cases:

    1. the private key is not protected by a password. In that case the private key is encoded according to the PKCS#8 standard and then encoded with Base64. It is possible to get the PKCS8 encoding of the private key by calling getEncoded on RSAPrivateKey.
    2. the private key is protected by a password. In that case the key encoding is an OpenSSH dedicated format. I don’t know if there is any documentation on this format (except the OpenSSH source code of course)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.