我使用以下命令从PKCS12文件中创建了私钥:
openssl pkcs12 -in test.p12 -nocerts -out privateKey.pem
现在我该如何从privateKey.pem
文件创建PrivateKey对象呢?
我尝试使用以下代码将PKCS12文件本身转换:
KeyStore pfx = KeyStore.getInstance("pkcs12");
pfx.load(new FileInputStream(P12), "123456".toCharArray());
final Enumeration<String> aliases = pfx.aliases(); //this is empty
pfx.aliases()
- 这个函数是空的,我使用keytool验证了它确实是空的,没有任何条目。
keytool -v -list -storetype pkcs12 -keystore test.p12
Keystore type: PKCS12
Keystore provider: SunJSSE
Your keystore contains 0 entries
我的问题是如何使用以下代码创建私钥:
public static RSAPrivateKey getPrivateKey(File privateKeyFile) throws IOException {
byte[] keyBytes = new byte[(int) privateKeyFile.length()];
FileInputStream fis = new FileInputStream(privateKeyFile);
fis.read(keyBytes);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);// it works only with PKCS8
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(spec);
return privKey;
}
这段代码的问题在于它只适用于PKCS8,而我需要一些类似于PKCS12的东西。