Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically create a new KeyStore?

Tags:

java

keystore

I'm trying to programmatically create a new keystore in Java. The following code:

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.setCertificateEntry("alias", cert); 

throws a Uninitialized KeyStore exception.

like image 640
Καrτhικ Avatar asked Mar 15 '11 13:03

Καrτhικ


People also ask

How do I create my own keystore?

To create a custom key store, you must specify an active AWS CloudHSM cluster that is not already associated with another key store. You also need to create a dedicated crypto user (CU) in the cluster's HSMs that AWS KMS can use to create and manage keys on your behalf.

Where keystore file is created in Java?

By default, Java has a keystore file located at JAVA_HOME/jre/lib/security/cacerts.

Can I create a keystore without password?

You cannot create a keystore with a blank password with keytool since a while, but you can still do it programmatically.

What is used to generate a keystore and key?

Use the standard JDK keytool utility to generate and load a new key and a self-signed certificate. When prompted, supply the certificate and password information. Doing so protects the keystore file and the keys within in the file.


1 Answers

To create a new KeyStore in Java you first need to create the KeyStore file and then store it using the store(FileOutputStream, char[]) method:

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());  char[] password = "some password".toCharArray(); ks.load(null, password);  // Store away the keystore. FileOutputStream fos = new FileOutputStream("newKeyStoreFileName"); ks.store(fos, password); fos.close(); 

I hope this helps, you can see more info here.

like image 53
Assaf Gamliel Avatar answered Oct 05 '22 22:10

Assaf Gamliel