如何以编程方式创建新的KeyStore?

时间:2022-08-26 00:02:21

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

我正在尝试以编程方式在Java中创建一个新的密钥库。以下代码:

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

throws a Uninitialized KeyStore exception.

抛出未初始化的KeyStore异常。

5 个解决方案

#1


45  

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.

KeyStore需要在创建后加载。 load方法要求FileInputStream读取,但如果提供null,则加载空的KeyStore。

See this link

看到这个链接

#2


63  

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:

要在Java中创建新的KeyStore,首先需要创建KeyStore文件,然后使用store(FileOutputStream,char [])方法存储它:

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.

我希望这有帮助,你可以在这里看到更多信息。

#3


4  

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;
}

#4


0  

 // 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;

#5


-9  

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);
        }
    }

#1


45  

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.

KeyStore需要在创建后加载。 load方法要求FileInputStream读取,但如果提供null,则加载空的KeyStore。

See this link

看到这个链接

#2


63  

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:

要在Java中创建新的KeyStore,首先需要创建KeyStore文件,然后使用store(FileOutputStream,char [])方法存储它:

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.

我希望这有帮助,你可以在这里看到更多信息。

#3


4  

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;
}

#4


0  

 // 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;

#5


-9  

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);
        }
    }