KeyStore没有保存到文件

问题描述:

我想使用Java KeyStore库在JKS文件中存储多个私钥。我创建了一个写入和读取JKS文件的方法,但私钥没有保存在文件中。KeyStore没有保存到文件

当我将某些东西存储到KeyStore中时,我可以获取密钥库中的所有别名,并且新密钥在那里。一旦该方法关闭,并试图拉同样的钥匙,它不会找到钥匙。

Main.java

public static void main(String[] args) throws Exception { 
    //Create keys 
    main m = new main(); 
    m.getOrSetPrivateKey("123", "123", privateKey, false); 

    PrivateKey p = m.getOrSetPrivateKey("123", "123", null, true); 

    if (p.equals(c.getPriv_key())) 
     System.err.println("Equal"); 
    else 
     System.err.println("Not equal !!!!!!!!"); 

} 


private synchronized PrivateKey getOrSetPrivateKey(String alias, String id, PrivateKey c, boolean read) throws InterruptedException, IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, InvalidKeySpecException, NotSupportedException, UnrecoverableKeyException { 
    PrivateKey key = null; 

    InputStream inpusStream = new FileInputStream(getFile2(Constants.JKS_PRIVATE_FILE_NAME)); 
    KeyStore keyStore = null; 
    try { 
     keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     keyStore.load(inpusStream, Constants.JKS_PRIVATE_FILE_PASSWORD); 
    } finally { 
     if (inpusStream != null) 
      inpusStream.close(); 
    } 
    Enumeration<String> s = keyStore.aliases(); 

    while (s.hasMoreElements()) 
     System.err.println("[ " + s.nextElement() + " ]"); 

    //Generate password for this private key 
    char [] pass = getKeyPassword(c, alias, id); 


    if (read == true) { //If reading/getting private key from file store 
     boolean isKeyEntry = keyStore.isKeyEntry(alias);//Check if there is a key with the alias deviceSerialnumber 
     if (!isKeyEntry) {//No key with this alias exists 
      throw new KeyStoreException("No key with alias " + alias + " exists!"); 
     } 

     key = (PrivateKey) keyStore.getKey(alias, pass); 

    } else { //Writing/ saving key to the file store 
     keyStore.setKeyEntry(alias, c , pass, new Certificate[] { createCertificate() }); 
     FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME), true); 
     try { 
      keyStore.store(out, pass); 

      System.out.println("Alias exists = " + keyStore.containsAlias(alias)); 
     } finally { 
      if (out != null) 
       out.close(); 
     } 
    } 

    s = keyStore.aliases(); 

    while (s.hasMoreElements()) 
     System.err.println("(" + s.nextElement() + ")"); 

    return key; 
} 

输出:

[ mykey ] 
(123) 
(mykey) 
Alias exists = true 
[ mykey ] 
Exception in thread "main" java.security.KeyStoreException: No key with alias 123 exists! 

为什么键没有被保存到JKS文件的文件?

的问题是在FileOutputStream被指向了错误的文件。

FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME), true); 

应该使用getFile2方法是这样的:

FileOutputStream out = new FileOutputStream(getFile2(Constants.JKS_PRIVATE_FILE_NAME)); 

由于Palamino指出,并不需要包括在FileOutputStream构造true

此外,密钥存储应该使用JKS文件密码,而不是由getKeyPassword()生成的密码。

改变了这个:

keyStore.store(out, pass); 

要使用JKS文件的密码,如:

keyStore.store(out, Constants.JKS_PRIVATE_FILE_PASSWORD); 
+1

什么getFile2方法?你的问题中没有这样的方法。这个问题似乎不适合在*,因为它实际上不提供足够的信息。 – eis

+0

它在'FileInputStream'构造函数 – develop1

+1

中的'getOrSetPrivateKey'方法中,但在何处定义? – eis

您正在追加到现有密钥库,而不是替换它,因为您正在将“true”传递给FileOutputStream构造函数。

FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME), true); 

替换行上面如下:

FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME)); 
+0

关键还是没有被保存在文件中。 – develop1