1

There's a ton of documentation out there on how to create keystores and use them with java. Unfortunately that copious documentation entirely swamps anything out there on how to do encryption without using a keystore, and I've been unable to find a good starting point. I understand that most of the time keystores are a good idea. I have a very specific and oddball case (which I don't want to disclose the details of so don't ask) where I want to be able to provide a private key as text in a format similar to what is used for ssh in the .ssh/id_rsa private key file:

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,508C51E6852AFF6BE89DB2B8A185DC4A

lG/N0foobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842foobarb
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsjklfadsj
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsjklfadsj
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsjklfadsj
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsjklfadsj
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsjklfadsj
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsjklfadsj
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsjklfadsj
azfobarfoobarbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fooba
rbazfobarbazFoOB4R842foobarbazfobarbazFoOB4R842fjdsjkfdsj0b+FMFD
-----END RSA PRIVATE KEY-----

And then use that to encrypt some other data. One problem I have is that although I've set up numerous ssh accounts in the past I don't know the actual name of that particular format (is it PEM? but examples of pem I have seen while searching lack the Proc-Type and DEK-Info lines) so searching for javadoc or other advice on consuming it is not possible.

Questions are these: What is the name of the above format? What is the best way to consume that format from a Java String with the intent of using it to encrypt data (to later be decrypted with the matching public key of course)?

Gus
  • 6,719
  • 6
  • 37
  • 58
  • Wow, I expected someone would be able to identify the key format at least... Glad you like the key. I spent tens of thousands of milliseconds carefully hand crafting it. – Gus Sep 26 '13 at 23:58
  • Also see [Decrypting an OpenSSL PEM Encoded RSA private key with Java?](http://stackoverflow.com/q/35276820), [How to get the java.security.PrivateKey object from RSA Privatekey.pem file?](http://stackoverflow.com/q/7525679) and [Using a PEM encoded, encrypted private key to sign a message natively](http://stackoverflow.com/q/1580012). Some suggest to convert the private key to PKCS #8. – jww Feb 08 '16 at 23:07
  • :) and years later I now know that this is called "PEM" format... I suppose I should write that in the answer, unless you want to do it? http://serverfault.com/q/9708/109339 and https://en.wikipedia.org/wiki/Privacy-enhanced_Electronic_Mail are also helpful – Gus Feb 11 '16 at 22:31

0 Answers0