I have to protect a Certificate with a password in a pem file with java like I protected a private key but I can not find a method to make it. How can I protect a certificate with a password in pem file in java?
public static void writeKeyCertPairToOnePemFile(PrivateKey pkey, String pathPemFile, String namePemFile,String password, Certificate... certChain) throws Exception {
PemMethods.checkExistingCreatePem(pathPemFile, namePemFile);
PemObject pemObject = null;
PemWriter pemWriter = null;
OutputEncryptor oe = null;
JcaPKCS8Generator gen = null;
try {
JceOpenSSLPKCS8EncryptorBuilder encryptorBuilder = new JceOpenSSLPKCS8EncryptorBuilder(PKCS8Generator.PBE_SHA1_3DES);
encryptorBuilder.setRandom(new SecureRandom());
encryptorBuilder.setPasssword(password.toCharArray());
oe = encryptorBuilder.build();
gen = new JcaPKCS8Generator(pkey,oe);
pemObject = gen.generate();
pemWriter = new PemWriter(new OutputStreamWriter(new FileOutputStream(pathPemFile+namePemFile)));
pemWriter.writeObject(pemObject);
for (Certificate certificate : certChain) {
pemObject = new PemObject("CERTIFICATE", certificate.getEncoded());
pemWriter.writeObject(pemObject);
}
} finally {
pemWriter.close();
}
LOGGER.info(String.format("%s has been written in file %s successfully !", "Certificate(s) and Private Key", pathPemFile+namePemFile));
}
question from:
https://stackoverflow.com/questions/65882270/how-to-protect-a-certificate-with-a-password-in-pem-file-in-java 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…