-
Hi, I am trying to use the following code to encrypt a RSA private key. Basically do in java code the equivalent of I am using the following code to do this: static String encryptPrivateKey(PrivateKey privateKey, String passphrase) throws Exception {
OutputEncryptor pemEncryptor = new JcePKCSPBEOutputEncryptorBuilder(PKCS8Generator.AES_256_CBC)
.build(passphrase.toCharArray());
JcaPKCS8Generator pemGen = new JcaPKCS8Generator(privateKey, pemEncryptor);
StringWriter out = new StringWriter();
new PemWriter(out).writeObject(pemGen);
return out.toString();
} However the method fails with the following stacktrace:
I don't understand why I am getting that "Input length not multiple of 16 bytes" error: if I interpret the mentioned code in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
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 |
Beta Was this translation helpful? Give feedback.
Found the solution, several things were not correct in my example, here is a solution:
BouncyCastle provider must be explicitly set on the
OutputEncryptor
or the JCE provider will be used and for some reaso…