我正在尝试将PrivateKey对象转换为pem格式字符串而不使用bouncycastle。 以前我这样做是这样的:
StringWriter str = new StringWriter();
PEMWriter pemWriter = new PEMWriter(str);
pemWriter.writeObject(privKey);
pemWriter.close();
str.close();
return str.toString().getBytes();
现在我不想使用bouncycastle。如果我使用
String code = "-----BEGIN RSA PRIVATE KEY-----\n";
String codenew =Base64.encodeBase64String(privKey.getEncoded());
String myOutput = "";
for (String substring : Splitter.fixedLength(64).split(codenew)) {
myOutput += substring + "\n";
}
code += myOutput.substring(0, myOutput.length() - 1);
code += "\n-----END RSA PRIVATE KEY-----";
return code.getBytes();
上面的代码可以工作,我可以通过传递这个byte []来使用openssl解密。 现在,我的问题是,如果我打印上述两段代码的pem字符串值,它们就不同了。 如果我使用下面的openssl命令
$ openssl pkcs12 -in receiver.p12 -out receiver.pem -nodes
生成的pem字符串与PEMWriter的pem字符串相同,但不是另一个字符串。我想知道我可以稍微修改我的代码,以便打印时的字符串匹配openssl命令或PEMWriter生成的字符串。
由openssl / PEMWriter生成的pem:
-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEA9rbxqq+Zi70nRFAZe7SCTB6VgzP1PhkiUm0PmbwFmROSlSSy
yMPSyIzaQqwELyOSQTZtsT3jhd6MCFPBZntym63/GwDuethGSjE9y8rt/9yr+T3I
zz+6ABnZXHJ38tdGYataF1Ndi3CsY5NXGszVFv1Is17P5mbYWQgJ7QzI/a5mPKa+
9pVXsDQthEV3BVUawIEJJnS0THD5XZQJ/MX6F4RPn+2MC9i/RbcA0RVnLPmt2eiy
NV3+55sKdd7GpdMmEbRv9HZyW2xJNyu1xYbwU9YIP88dHCgvqoOgkAX2HLxCJOy6
2gvsS8J7HEbohD98dxPJX7P8w9juORi6Hpsq0wIDAQABAoIBAQDXStIdJtuRC+GG
RXfXca/6iP3j3qV2KSzATRe+CkvAR0o1CC9T7z6zb+bPI5kLIblxWvPiJaW0nn4I
jj5JFhTvMalagTeaz7yW5d2NR2rlSkZwW7Au2ueP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