Java 密钥库未保存到文件

Java 密钥库未保存到文件,java,security,keystore,private-key,Java,Security,Keystore,Private Key,我试图使用Java密钥库将多个私钥存储在一个JKS文件中。我创建了一个写入和读取JKS文件的方法,但是私钥没有保存在文件中 当我将某些内容存储到密钥库中时,我可以获得密钥库中的所有别名,并且新密钥就在那里。一旦关闭该方法并尝试拉取相同的键,它将找不到该键 Main.java public static void main(String[] args) throws Exception { //Create keys main m = new main(); m.getOr

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

当我将某些内容存储到密钥库中时,我可以获得密钥库中的所有别名,并且新密钥就在那里。一旦关闭该方法并尝试拉取相同的键,它将找不到该键

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

为什么没有将密钥保存到JKS文件?

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

您正在将“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));

问题出在
文件输出流中
指向错误的文件

FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME), true);
应该使用如下的
getFile2
方法:

FileOutputStream out = new FileOutputStream(getFile2(Constants.JKS_PRIVATE_FILE_NAME));
keyStore.store(out, Constants.JKS_PRIVATE_FILE_PASSWORD);
正如Palamino指出的,不需要在
FileOutputStream
构造函数中包含
true

FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME), true);
此外,密钥存储应该使用JKS文件密码,而不是由
getKeyPassword()
生成的密码

更改为:

keyStore.store(out, pass);
要使用JKS文件密码,如下所示:

FileOutputStream out = new FileOutputStream(getFile2(Constants.JKS_PRIVATE_FILE_NAME));
keyStore.store(out, Constants.JKS_PRIVATE_FILE_PASSWORD);

问题出在
文件输出流中
指向错误的文件

FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME), true);
应该使用如下的
getFile2
方法:

FileOutputStream out = new FileOutputStream(getFile2(Constants.JKS_PRIVATE_FILE_NAME));
keyStore.store(out, Constants.JKS_PRIVATE_FILE_PASSWORD);
正如Palamino指出的,不需要在
FileOutputStream
构造函数中包含
true

FileOutputStream out = new FileOutputStream(new File(Constants.JKS_PRIVATE_FILE_NAME), true);
此外,密钥存储应该使用JKS文件密码,而不是由
getKeyPassword()
生成的密码

更改为:

keyStore.store(out, pass);
要使用JKS文件密码,如下所示:

FileOutputStream out = new FileOutputStream(getFile2(Constants.JKS_PRIVATE_FILE_NAME));
keyStore.store(out, Constants.JKS_PRIVATE_FILE_PASSWORD);

密钥仍然没有保存在文件中。密钥仍然没有保存在文件中。什么getFile2方法?在你的问题中没有这样的方法。这个问题似乎不适合stackoverflow,因为它确实没有提供足够的信息。它位于
FileInputStream
Constructor中的
getOrSetPrivateKey
方法中,但它是在哪里定义的?方法不在构造函数中。答案不完整。你在说什么?getFile2方法只是返回一个文件,不需要说明如何返回,它与问题无关。答案是完整的,注意到的所有变化都解决了问题。因此,取消否决票。什么getFile2方法?在你的问题中没有这样的方法。这个问题似乎不适合stackoverflow,因为它确实没有提供足够的信息。它位于
FileInputStream
Constructor中的
getOrSetPrivateKey
方法中,但它是在哪里定义的?方法不在构造函数中。答案不完整。你在说什么?getFile2方法只是返回一个文件,不需要说明如何返回,它与问题无关。答案是完整的,注意到的所有变化都解决了问题。因此,取消否决票。