How do I programmatically create a new KeyStore?

前端 未结 5 2081
鱼传尺愫
鱼传尺愫 2020-12-01 06:10

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

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keySt         


        
相关标签:
5条回答
  • 2020-12-01 06:26
     // 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;
    
    0 讨论(0)
  • 2020-12-01 06:28

    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

    0 讨论(0)
  • 2020-12-01 06:28

    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;
    }
    
    0 讨论(0)
  • 2020-12-01 06:38

    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.

    0 讨论(0)
  • 2020-12-01 06:47
    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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题