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 4032004
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:40:02+00:00 2026-05-20T11:40:02+00:00

If I wanted to store both a private and a public key in a

  • 0

If I wanted to store both a private and a public key in a single file, what would be the easiest format to use? Especially if I’m planning to use the BouncyCastle library for Java?

  • 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-20T11:40:03+00:00Added an answer on May 20, 2026 at 11:40 am

    On a theoretical point of view, the public key can be recomputed from the private key (computational cost for that is slightly lower than the cost for producing a single ECDSA signature, or doing half of ECDH, so it is fast). Therefore, conceptually, you only have to store the private key, and the standard format for that is PKCS#8, which is supported by Java with java.security.spec.PKCS8EncodedKeySpec. Moreover, the PKCS#8 format includes provisions for optionally encoding the public key along the private key in the same blob, so this really looks like what you are looking for.

    The tricky thing, however, is to convince the cryptographic provider (e.g. BouncyCastle) to extract the public key as such and/or recompute it. Apparently, if you create a PKCS8EncodedKeySpec from a PKCS#8-encoded EC private key which also contains the public key, BouncyCastle will be kind enough to internally keep a copy of the encoded public key and write it back if you decide to reencode the private key in PKCS#8 format. However, it does nothing else with it; it handles it as an opaque blob.

    Hence you must recompute the public key. Wading through the JCE and BouncyCastle API and unimplemented bits, I found the following, which appears to work (JDK 1.6.0_24, BouncyCastle 1.46):

    import java.security.KeyFactory;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Provider;
    import java.security.spec.PKCS8EncodedKeySpec;
    import org.bouncycastle.jce.provider.BouncyCastleProvider;
    import org.bouncycastle.jce.provider.JCEECPrivateKey;
    import org.bouncycastle.jce.provider.JCEECPublicKey;
    import org.bouncycastle.jce.spec.ECParameterSpec;
    import org.bouncycastle.jce.spec.ECPublicKeySpec;
    
    // Create the provider and an appropriate key factory.
    Provider pp = new BouncyCastleProvider();
    KeyFactory kf = KeyFactory.getInstance("EC", pp);
    
    // Decode the private key (read as a byte[] called 'buf').
    PKCS8EncodedKeySpec ks = new PKCS8EncodedKeySpec(buf);
    PrivateKey sk = kf.generatePrivate(ks);
    
    // Recompute public key.
    JCEECPrivateKey priv = (JCEECPrivateKey)sk;
    ECParameterSpec params = priv.getParameters();
    ECPublicKeySpec pubKS = new ECPublicKeySpec(
        params.getG().multiply(priv.getD()), params);
    PublicKey pk = kf.generatePublic(pubKS);
    
    // To reencode the private key.
    buf = kf.getKeySpec(sk, PKCS8EncodedKeySpec.class).getEncoded();
    

    Conceptually, I should use kf.getkeySpec() with org.bouncycastle.jce.spec.ECPrivateKeySpec instead of ruthlessly casting the private key to the JCEECPrivateKey class, but the clean method appears not to be implemented yet in BouncyCastle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Subtitle: Can EventHandlerList key's Type be something else than object? I wanted to use
I wanted to store 10 in 4th position of array of 5 positions. How
i have several classes with members called 'Id'. Originally i wanted to store these
i wanted to know where we should store the db in our project and
I needed to store various strings in a map but I wanted to keep
We have a multi-store set up and I wanted to change the template slightly
Somewhat misleading title, I know. Never actually wanted to store TimeZoneInfo object themselves: rather,
I have a json file. I am using it to store information, and as
I'm about to push my app to the app store and wanted to make
I have a stored procedure and i wanted to combine both the firstname and

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.