编写代码生成数字证书
并且在浏览器中安装时出现错误
无法解码文件。它不是 PKCS #12 格式、已损坏或您输入的密码不正确。
但我不知道如何添加该密码以满足 PKCS #12 format
. 怎么去呢?
public KeyPair generateKeyPair() {
KeyPair pair = null;
try {
String password = "1234";
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = Utils.createFixedRandom();
keyGen.initialize(1024, random);
pair = keyGen.generateKeyPair();
PrivateKey privkey1 = pair.getPrivate();
PublicKey pubKey1 = pair.getPublic();
byte[] privateKeyBytes = pair.getPrivate().getEncoded();
byte[] encryptedPrivateKeyBytes = passwordEncrypt(
password.toCharArray(), privateKeyBytes);
//Problem might be here
Signature dsa = Signature.getInstance("SHA1withRSA");
dsa.initSign(privkey1);
Cipher cipher = Cipher
.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey1, random);
byte[] input = new byte[] { (byte) 0xbe, (byte) 0xef };
System.out.println("input : " + Utils.toHex(input));
byte[] cipherText = cipher.doFinal(input);
System.out.println("cipher: " + Utils.toHex(cipherText));
cipher.init(Cipher.DECRYPT_MODE, privkey1);
byte[] plainText = cipher.doFinal(cipherText);
System.out.println("plain : " + Utils.toHex(plainText));
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}
return pair;
}
证书生成成功并卡在这里。
感谢您的任何提示。