64

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.

Καrτhικ
  • 3,833
  • 2
  • 29
  • 42

6 Answers6

90

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.

Assaf Gamliel
  • 11,935
  • 5
  • 41
  • 56
66

The KeyStore needs to be loaded after it has been created. The load method asks for a FileInputStream to read from but if you supply a null one, an empty KeyStore is loaded.

See this link

DJClayworth
  • 26,349
  • 9
  • 53
  • 79
charisis
  • 1,732
  • 14
  • 14
  • 5
    ^ Link: http://download.oracle.com/javase/6/docs/api/java/security/KeyStore.html#load%28java.io.InputStream,%20char[]%29 – Matt Wonlaw Mar 15 '11 at 13:56
8

I use this code, it works, hope it can help.

public static KeyStore createKeyStore() throws Exception {
    File file = new File("/Users/keyserverstore.keystore");
    KeyStore keyStore = KeyStore.getInstance("JKS");
    if (file.exists()) {
        // if exists, load
        keyStore.load(new FileInputStream(file), "123456".toCharArray());
    } else {
        // if not exists, create
        keyStore.load(null, null);
        keyStore.store(new FileOutputStream(file), "123456".toCharArray());
    }
    return keyStore;
}
Jay
  • 99
  • 1
  • 2
  • not creating a new store, I need to have different stores depending on the call I am making, this changes the default. – dezzer10 Oct 24 '17 at 18:34
-1
 // load the keystore
 KeyStore p12 = KeyStore.getInstance("pkcs12");
 p12.load(new FileInputStream("KEYSTORE.p12"), "passwd".toCharArray());

// load the private key entry from the keystore  
 Key key = p12.getKey("mykey", "passwd".toCharArray()); 
 PrivateKey privKey = (PrivateKey) key;
-2

If you want to use a bean (Spring Boot 2.4.x):

import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class KeyStoreConfiguration {

    private static final String KEY_STORE = "keystore.p12";
    private static final String KEY_STORE_TYPE = "PKCS12";
    private static final String KEY_STORE_PASSWORD = "password";
    
    @Bean
    public KeyStore keyStore() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
        KeyStore keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
        keyStore.load(new ClassPathResource(KEY_STORE).getInputStream(), KEY_STORE_PASSWORD.toCharArray());
        
        return keyStore;
    }
}
jumping_monkey
  • 5,941
  • 2
  • 43
  • 58
-11
public static void main(String[] args) {
    // Load the JDK's cacerts keystore file
    String filename = System.getProperty("java.home") + "/lib/security/cacerts".replace('/', File.separatorChar);
    FileInputStream is = new FileInputStream(filename);
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    char[] password = "changeit".toCharArray();
    //keystore.load(is, password.toCharArray());
    keystore.load(is, password);

    // This class retrieves the most-trusted CAs from the keystore
    PKIXParameters params = new PKIXParameters(keystore);
    // Get the set of trust anchors, which contain the most-trusted CA certificates
    java.security.cert.Certificate sapcert = keystore.getCertificate("SAPNetCA");
    PublicKey sapcertKey =  sapcert.getPublicKey();
    System.out.println(sapcertKey);
    Enumeration<String> aliases = keystore.aliases();
    while (aliases.hasMoreElements()) {
       String alias = aliases.nextElement();
        //System.out.println("alias certificates :"+alias);
       if (keystore.isKeyEntry(alias)) {
            keystore.getKey(alias, password);
        }
    }
Nazik
  • 8,696
  • 27
  • 77
  • 123
ramesh v
  • 17
  • 1