如何在java中创建证书pfx文件?
我需要用java创建带有密码的证书pfx文件。我在谷歌搜索,可以创建证书“cer”文件 我在main中添加代码如何在java中创建证书pfx文件?,java,security,certificate,pfx,Java,Security,Certificate,Pfx,我需要用java创建带有密码的证书pfx文件。我在谷歌搜索,可以创建证书“cer”文件 我在main中添加代码 FileOutputStream fos = null; fos = new FileOutputStream("public.cer"); fos.write(cert.getEncoded()); fos.close(); 这只在没有密码的情况下工作 通常是.pfx或pkcs12它是保存公钥和私钥对的密钥库。此外,如果您在链接示例中使用带有公共证书的RSA密钥对,通常此证书必须由
FileOutputStream fos = null;
fos = new FileOutputStream("public.cer");
fos.write(cert.getEncoded());
fos.close();
这只在没有密码的情况下工作 通常是
.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(字符串别名,字节[]键,证书[]链)
或setKeyEntry(字符串别名,密钥密钥,字符[]密码,证书[]链)
但是使用上面的代码,您可以在密钥库中存储证书,并使用密码保护它。有关更多信息,请查看:
希望这有帮助