2014-10-11 198 views

回答

3

通常一个.pfxpkcs12其保存公钥,私钥对的密钥库。此外,如果您在链接示例中使用带有公共证书的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

希望这有助于

-1

我有我的答案是:

KeyPair pair = generateRSAKeyPair(); 
    X509Certificate cert = generateV3Certificate(pair); 
    KeyStore ks = KeyStore.getInstance("PKCS12", "BC"); 
    char[] password = "abc".toCharArray(); 
    ks.load(null,null); 
    ks.setCertificateEntry(cert.getSerialNumber().toString(), cert); 
    ks.setKeyEntry(cert.getSerialNumber().toString(), pair.getPrivate(), password, new Certificate[]{cert,cert}); 
    FileOutputStream fos = new FileOutputStream("keystore.pfx"); 
    ks.store(fos, password); 
    fos.close(); 
相关问题