如何在 java 中正确创建和访问 KeyStore 以存储加密密钥?
How does one correctly create and access a KeyStore in java to store an encryption key?
我一直在混合和匹配代码,尝试通过示例学习如何使用 KeyStores。
我有这个 createKeyStore 方法:
private static KeyStore createKeyStore(String fileName, String pw) throws Exception
{
File file = new File(fileName);
final KeyStore keyStore = KeyStore.getInstance("JCEKS");
if (file.exists())
{
// .keystore file already exists => load it
keyStore.load(new FileInputStream(file), pw.toCharArray());
}
else
{
// .keystore file not created yet => create it
keyStore.load(null, null);
keyStore.store(new FileOutputStream(fileName), pw.toCharArray());
}
return keyStore;
}`
似乎有效,没有抛出任何错误。
然后我尝试通过以下方式访问代码:
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(new FileInputStream(keystorePath), pass.toCharArray());
String alias = "alias";
char[] password = pass.toCharArray();
Certificate cert = keystore.getCertificate(alias);
keystore.setCertificateEntry(alias, cert);
// Save the new keystore contents
FileOutputStream out = new FileOutputStream(keystoreFile);
keystore.store(out, password);
out.close();
但是我对 keystore.load 的调用抛出了一个无效的密钥库格式异常。
我尝试将 FileInputStream 替换为 null,但似乎在设置证书时出错。
TL;DR: 我只是想在此密钥库中存储一些加密密钥,但我似乎无法正确访问它。
感谢阅读!
你有:
final KeyStore keyStore = KeyStore.getInstance("JCEKS");
和
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
更改这些以便他们同意。
这个:
Certificate cert = keystore.getCertificate(alias);
keystore.setCertificateEntry(alias, cert);
毫无意义。如果密钥库中没有这样的证书,它将失败,如果有,它只会用自己替换它。重点是什么?
I tried to replace the FileInputStream with null
我无法想象为什么。 Javadoc 中没有任何内容表明这会起作用。
如果您查看文档,它说要创建一个空的密钥库,请使用 null 作为文件输入参数而不是密码参数调用 load() 方法。在您的 else 子句中将 null 作为密码参数传递会导致稍后从文件加载密钥库时出现空指针异常。
keystore.load(null, pass.toCharArray());
我一直在混合和匹配代码,尝试通过示例学习如何使用 KeyStores。
我有这个 createKeyStore 方法:
private static KeyStore createKeyStore(String fileName, String pw) throws Exception
{
File file = new File(fileName);
final KeyStore keyStore = KeyStore.getInstance("JCEKS");
if (file.exists())
{
// .keystore file already exists => load it
keyStore.load(new FileInputStream(file), pw.toCharArray());
}
else
{
// .keystore file not created yet => create it
keyStore.load(null, null);
keyStore.store(new FileOutputStream(fileName), pw.toCharArray());
}
return keyStore;
}`
似乎有效,没有抛出任何错误。
然后我尝试通过以下方式访问代码:
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
keystore.load(new FileInputStream(keystorePath), pass.toCharArray());
String alias = "alias";
char[] password = pass.toCharArray();
Certificate cert = keystore.getCertificate(alias);
keystore.setCertificateEntry(alias, cert);
// Save the new keystore contents
FileOutputStream out = new FileOutputStream(keystoreFile);
keystore.store(out, password);
out.close();
但是我对 keystore.load 的调用抛出了一个无效的密钥库格式异常。 我尝试将 FileInputStream 替换为 null,但似乎在设置证书时出错。
TL;DR: 我只是想在此密钥库中存储一些加密密钥,但我似乎无法正确访问它。
感谢阅读!
你有:
final KeyStore keyStore = KeyStore.getInstance("JCEKS");
和
KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
更改这些以便他们同意。
这个:
Certificate cert = keystore.getCertificate(alias);
keystore.setCertificateEntry(alias, cert);
毫无意义。如果密钥库中没有这样的证书,它将失败,如果有,它只会用自己替换它。重点是什么?
I tried to replace the FileInputStream with null
我无法想象为什么。 Javadoc 中没有任何内容表明这会起作用。
如果您查看文档,它说要创建一个空的密钥库,请使用 null 作为文件输入参数而不是密码参数调用 load() 方法。在您的 else 子句中将 null 作为密码参数传递会导致稍后从文件加载密钥库时出现空指针异常。
keystore.load(null, pass.toCharArray());