Skip to content

Encrypting RSA private key #1449

Closed Answered by joelmarty
joelmarty asked this question in Q&A
Discussion options

You must be logged in to vote

Found the solution, several things were not correct in my example, here is a solution:

        OutputEncryptor pemEncryptor = new JcePKCSPBEOutputEncryptorBuilder(PKCS8Generator.AES_256_CBC)
            .setProvider("BC")
            .build(passphrase.toCharArray());
        JcaPKCS8Generator pemGen = new JcaPKCS8Generator(privateKey, pemEncryptor);

        StringWriter out = new StringWriter();
        JcaPEMWriter pemWriter = new JcaPEMWriter(out);
        pemWriter.writeObject(new JcaMiscPEMGenerator(pemGen));
        pemWriter.flush();
        return out.toString();

BouncyCastle provider must be explicitly set on the OutputEncryptor or the JCE provider will be used and for some reaso…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by joelmarty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant