How can I insert a key pair into a key store using Java? I want to do exactly what this keytool command does, but using Java:
keytool -genkeypair -alias keyRSA -keyalg RSA -keysize 2048 -storetype JCEKS -keystore .keystore
When executing this command it asks for certificate details like:
- What is your first and last Name?
- What is the name of your the organizational unit?
- What is the name of your organization?
- ...
I want to set those certificate details using java. The certificate format is X509
Java code below
File keyStoreFile = new File(".keystore");
KeyStore keyStore = KeyStore.getInstance("JCEKS");
//Create empty keystore
keyStore.load(null, null);
keyStore.store(new FileOutputStream(keyStoreFile), "12345".toCharArray());
//Get keyPair from somewhere
KeyPair keyPair = ...
//HOW TO GET CERTIFICATE?
Certificate[] chain = ... ???
//Insert key pair entry into key store
keyStore.setKeyEntry("keyRSA", keyPair.getPrivate(), "12345".toCharArray(), certificateChain);
keyStore.store(new FileOutputStream(keyStoreFile), "12345".toCharArray());