通常一个.pfx
或pkcs12
其保存公钥,私钥对的密钥库。此外,如果您在链接示例中使用带有公共证书的RSA
密钥对,通常此证书必须由证书颁发机构颁发,无论如何,我认为您尝试保存自签名证书,为此,您必须使用java.security.KeyStore
类不是FileOutputStream
直接,我给你一个例子:
import java.io.FileOutputStream;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
....
X509Certificate cert = // your certificate...
// generate a keystore instance
KeyStore ks = KeyStore.getInstance("PKCS12");
// save your cert inside the keystore
ks.setCertificateEntry("YourCertAlias", cert);
// create the outputstream to store the keystore
FileOutputStream fos = new FileOutputStream("/your_path/keystore.pfx");
// store the keystore protected with password
ks.store(fos, "yourPassword".toCharArray());
....
正如我在您存储密钥对的密钥库通常说,正常使用:setKeyEntry(String alias, byte[] key, Certificate[] chain)
或setKeyEntry(String alias, Key key, char[] password, Certificate[] chain)
但是与上面的代码,你可以存储证书在密钥库中用密码保护它。欲了解更多信息,请看:java keystore api。
希望这有助于