【问题标题】:KeyStore not saving to fileKeyStore 不保存到文件
【发布时间】:2018-01-07 07:38:53
【问题描述】:

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

当我将某些内容存储到 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 文件文件中?

【问题讨论】:

    标签: java security keystore private-key


    【解决方案1】:

    问题在于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);
    

    【讨论】:

    • 什么getFile2方法?您的问题中没有这种方法。这个问题似乎不适合 stackoverflow,因为它确实没有提供足够的信息。
    • FileInputStream构造函数中的getOrSetPrivateKey方法中
    • 但它是在哪里定义的?
    • 方法不在构造函数中。答案不完整。
    • 你在说什么? getFile2 方法只是返回一个文件,不需要展示如何做到这一点,它与问题无关。答案是完整的,所有注意到的变化都是解决问题的原因。所以拿走反对票。
    【解决方案2】:

    您将附加到现有密钥库而不是替换它,因为您将“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));
    

    【讨论】:

    • 密钥仍未保存在文件中。
    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 2017-11-18
    • 2013-12-06
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多