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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:25:32+00:00 2026-05-23T11:25:32+00:00

What I’m planning to do is writing a client-server application that uses a SSL

  • 0

What I’m planning to do is writing a client-server application that uses a SSL (TLS) connection to exchange data.

As the client is downloadable and I can not guarantee access to the keystore I’m looking for a way to import certificates at runtime.

What I need:

  • A way to import the server’s public key/certificate into the client application
  • A way to import the server’s private key/certificate into the server application

What I found out so far:

// load the server's public crt (pem), exported from a https website
CertificateFactory cf = CertificateFactory.getInstance("X509");
X509Certificate cert = (X509Certificate)cf.generateCertificate(new
FileInputStream("C:\\certificate.crt"));

// load the pkcs12 key generated with openssl out of the server.crt and server.key (private) (if the private key is stored in the pkcs file, this is not a solution as I need to ship it with my application)
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream("C:\\certificate.pkcs"), "password".toCharArray());
ks.setCertificateEntry("Alias", cert);

TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);

KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "password".toCharArray());

// create SSLContext to establish the secure connection
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

This does not work for me as I’m getting an error:

java.security.KeyStoreException: TrustedCertEntry not supported at ks.setCertificateEntry(“Alias”, cert);

Also, I think pkcs12 is used to store private keys which is not what I want.

I’m new to java and I’m really stuck with that problem now.

Thanks in advance,

Kazuo

  • 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-23T11:25:32+00:00Added an answer on May 23, 2026 at 11:25 am

    Sun’s implementation of #PKCS12 does not allow to store trusted certificates if are not part of the chain of the private key.
    If you need to use #PKCS12 you have to switch to a different provider e.g. Bouncy Castle supports this.
    If you do not have a requirement on keystore type you can switch to JKS which is java’s keystore and allows to set trusted certificates (i.e. not part of the private key).
    For JKS you can use the default provider i.e. SUN.
    UPDATE:
    So your code would have to change as follows:

    //Create a temp keystore with the server certificate  
    
    KeyStore ksTemp = KeyStore.getInstance("JKS");    
    ksTemp.load(null, null);//Initialize it  
    ksTemp.setCertificateEntry("Alias", cert);   
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();  
    // save the temp keystore
    ks.store(bOut, password);  
    
    //Now create the keystore to be used by jsse   
    Keystore store = KeyStore.getInstance("JKS");   
    store.load(new ByteArrayInputStream(bOut.toByteArray()), password);  
    

    Now you use the keystore store in your code which has the server’s trusted certificate and not the private key.
    From the comments in the code I noticed that you have a PKCS12 created using OpenSSL?
    If you already have a p12 then you can not use “JKS” for KeyManager.
    You will have to use PKCS12 and load it as PKCS12 to use it in the kmf.
    So you will have to use 2 types in your app

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.