Writing code to generate digital certificate
And when installing in browser getting the error
Failed to decode the file. Either it is not in PKCS #12 format, has been corrupted, or the password you entered was incorrect.
But I don't have any idea how to add that password to satisfy PKCS #12 format
. How to go with it?
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;
}
Certificate generated successfully and stuck here.
Thanks for any hints.