wayne_bai
wayne_bai

Reputation: 1238

How to store Private Key and Public Key into KeyStore

All.I am working on an android project. I need to generate a RSA keypair then use them to communicate with others.I need to store the private key and public key in a secure place and I find KeyStore could be used.

I see that KeyStore could store KeyStore.PrivateKeyEntry but it need a Certificate[] chain. I tried to create it but failed...

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

Thanks so much!

Upvotes: 8

Views: 22897

Answers (1)

sperumal
sperumal

Reputation: 1519

Like you said, in order to store the Private key into the keystore, you need the Private key (which you have) and the Certificate chain for the corresponding public key. What you have is just the public key, you need to obtain a certificate from an authority based on your public key. Yes, you can self-sign the certificate. But I don't think there is any built Java API to to create and self-sign a certificate programmatically.

There was similar discussion on this thread. The accepted solution describes storing private key and public key outside of keystore in a protected file.

You can read more about Java Cryptography architecture here http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html

Hope this helps.

Upvotes: 6

Related Questions