I have a RSA private key .der file (with a bunch of binary in it). How do I convert the file to PEM format in Java?
If I were to use openssl in command line, this is what I would do:
openssl rsa -inform der -in privateKey.der -outform pem -out privateKey.pem
The privateKey.pem result looks like this:
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAyljm4IGV2JmL8amyTQz9nCSdtdJV+OGiiyefJasKuNEIVAyV
BzSNEUrJYL1LrAMM0cbenWh3M0I+RoQYfwo3J88fi/aLW5dbdU8bi001UZIeJnEB
...
...
lykp1jNHSXD8PoBNVD+pbY3zGCqH1OgPWd0/+IbMBp3Qo+HMp5Ku9w==
-----END RSA PRIVATE KEY-----
I heard that people say PEM is just Base64-encoded representation of DER plus certain header and footer. However, if I just do
cat privateKey.der | base64
I get different results from what openssl gave me:
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDKWObggZXYmYvxqbJNDP2 ... ... GndCj4cynkq73
Despite the head & footer, the PEM string is also not the same. Does anyone know why?
Are there any library to convert DER to PEM in Java like what openssl does? Been searching for a while but unfortunately, I didn't find anything that works. Any help would be greatly appreciated!