我正在尝试从Windows-MY密钥库获取证书和私钥并将其写在文件上,但我有一些问题让人认为Java不允许将私钥从Windows密钥库写入文件:
try {
KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
ks.load(null, null) ;
java.util.Enumeration en = ks.aliases() ;
while (en.hasMoreElements()) {
String aliasKey = (String)en.nextElement() ;
Certificate c = ks.getCertificate(aliasKey) ;
if (aliasKey.equals("myKey") ) {
PrivateKey key = (PrivateKey)ks.getKey(aliasKey, "monPassword".toCharArray());
Certificate[] chain = ks.getCertificateChain(aliasKey);
//write key on PEM file
wr1 = new FileWriter(directory.getPath() + "\\" + "key.pem");
PemWriter pWr = new PemWriter(wr1);
pemWr = new PEMWriter(wr1);
try {
pemWr.writeObject(key);
} catch (Exception e) {
e.printStackTrace();
}
pemWr.flush();
pemWr.close();
}
}
} catch (Exception ioe) {
System.err.println(ioe.getMessage());
}
我在文件上写了一个例外:
org.bouncycastle.util.io.pem.PemGenerationException: unknown object passed - can't encode.
是什么让我对某种安全保护产生了影响:
如何在文件.pem上写密钥? 谢谢 丹尼尔