0

I am working on a Java project. I need to generate a RSA keypair then use them to communicate with others.

I see that KeyStore could store KeyStore.PrivateKeyEntry but it need a Certificate[] chain. I have created certificate using my function but now I have only one certificate with the private key instead of chain of certificates ? How do i programmatically store this private key and certificate in the keystore ?

Or if there is a way to add private key and public key only instead of certificate. I can use that private key later to create my certificate.

Is there anyone could paste some example code used to store private key and public key.

Nitesh Verma
  • 1,795
  • 4
  • 27
  • 46
Ziki Khan
  • 44
  • 6
  • Why? Why not use SSL like everybody else? – user207421 Jul 27 '13 at 12:40
  • Because, I need to create private key and public key on the fly and it has to be automatic everything. So I cant use SSL. I have done everything but just addding private key to keystore is a problem :( – Ziki Khan Jul 27 '13 at 13:44
  • So how do the peers know that the public key you generate is owned by you? Have you really thought this through properly? As described, your proposed system is both inefficient and probably insecure. – user207421 Jul 28 '13 at 01:02
  • Yes EJP. I never thought about this. I will give it a thought. – Ziki Khan Jul 28 '13 at 10:30

1 Answers1

0

Actually, this isn't very difficult at all. Simply create a Certificate [] with one entry...your certificate.

This is assuming your certificates are self-signed certificates. If your certs are actually signed by another entity, then it would be best to put the entire chain of certs into the Certificate array.

ex.

X509Certificate myCert = ...

KeyStore ks = ...

Certificate [] certArray = { myCert };

ks.setKeyEntry(alias, key, certArray);
gtrig
  • 12,550
  • 5
  • 28
  • 36