How do I programmatically create a new KeyStore?

前端 未结 5 2084
鱼传尺愫
鱼传尺愫 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: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 aliases = keystore.aliases();
        while (aliases.hasMoreElements()) {
           String alias = aliases.nextElement();
            //System.out.println("alias certificates :"+alias);
           if (keystore.isKeyEntry(alias)) {
                keystore.getKey(alias, password);
            }
        }
    

提交回复
热议问题