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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:37:29+00:00 2026-06-08T17:37:29+00:00

How do I programmatically create a private key and use it in a SSL

  • 0

How do I programmatically create a private key and use it in a SSL socket?

I put a commented exception below where I’m trying to add the key to the keystore but I do not have a certificate chain.

    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(1024, new SecureRandom());
    KeyPair keypair = keyGen.generateKeyPair();

    System.setProperty("javax.net.ssl.keyStore", System.getProperty("user.home")
        + File.separator +
            + "/keystore.jks");
    System.setProperty("javax.net.ssl.keyStorePassword", "xyz");

    KeyManagerFactory keyManagerFactory = KeyManagerFactory
            .getInstance("SunX509");
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(null, "xyz".toCharArray());

    //setKeyEntry parameter 3 can not be null: 
    //IllegalArgumentException: Private key must be accompanied by certificate chain
    keyStore.setKeyEntry("alias", keypair.getPrivate(),
            "xyz".toCharArray(), null);

    keyManagerFactory.init(keyStore, "xyz".toCharArray());
    // keyStore.load
    SSLContext context = SSLContext.getInstance("TLS");// "SSLv3"
    context.init(keyManagerFactory.getKeyManagers(), null,
            new SecureRandom());
    ServerSocketFactory socketFactory = context.getServerSocketFactory();
    ServerSocket ssocket = socketFactory.createServerSocket(1443);
    Socket socket = ssocket.accept();
  • 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-06-08T17:37:30+00:00Added an answer on June 8, 2026 at 5:37 pm

    How do I programmatically create a private key and use it in a SSL
    socket?

    There is little point creating only a private key, or even just a private/public key pair. What you need to set up on your server is a certificate associated with this private key.

    X.509 certificates are rather complex structures, based on ASN.1 syntax. I’d strongly suggest you use BouncyCastle (more or less as described in the blog article you link to). Doing it all by hand is no simple task. (If you’re not convinced, look at the source code of the BouncyCastle classes.)

    In addition, there’s little point in creating this certificate (and its associated private key) dynamically. The point of the certificate is to allow the client to verify the identity of the server it’s talking to. The client does this by checking the server cert against a list of trusted certificates it has (either using a PKI or by individual comparison against specific server certificates that have been manually configured).

    If you generate a self-signed certificate dynamically, there is no way for the client to compare it against something it would have known in advance. It could potentially be useful if your server is also a CA (for applications similar to MITM-proxy servers).

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

Sidebar

Related Questions

I'm trying to programmatically create a window with my own custom OpenGL NSView as
I am trying to programmatically create a PowerPoint from graphs in Access. Ideally, when
I want to programmatically create a new log4j ConsoleAppender and add it as an
I am trying to programmatically create a number of PDF documents with a watermark
I'm trying to programmatically create an unordered list using the asp.net HTMLGenericControl. It properly
I'm trying to display a recordset that is created programmatically. But I'm having problems
I am trying to programmatically pass focus to a newly created TextField , but
I need to be able to programmatically create new tabs on a TabControl, add
I am trying to programmatically create a Radar Chart in my app with BIRT.
I'm trying to create a grid programmatically and appending a custom control as a

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.