这是我的程序。我收到错误“打开密钥文件时出错”。请帮助我在哪里出错。如何使用java sun security从.p12文件中提取私钥
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.util.Base64;
public class ExtractionClass {
public static void main(String args[]) {
String p12Password = "PassWord";
try{
FileInputStream file = new FileInputStream("C:/Users/Aarti_Jangid/Desktop/p12/certChain.p12");
System.out.println("FILE "+ file);
KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE");
keystore.load(file, p12Password.toCharArray());
String alias = keystore.aliases().nextElement();
PrivateKey key = (PrivateKey)keystore.getKey(alias, p12Password.toCharArray());
System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
} catch(KeyStoreException kse) {
kse.printStackTrace();
System.err.println("Error getting the key");
} catch (Exception e) {
System.err.println("Error opening the key file");
e.printStackTrace();
}
}
}
请复制以及 –
我觉得你的.p12文件有什么不对粘贴在这里堆栈跟踪,如果您使用有效的证书,则您的代码可以在[此处](https://itv.mit-xperts.com/clientssl/issue/dload/index.php?id=1411134668)上找到有效的证书。 – user6904265
如果以编程方式创建.p12文件,请查看[这里](http://stackoverflow.com/questions/5312559/how-do-i-programmatically-create-a-new-keystore) – user6904265