0

I have a question about the conversion of a private key (.pem) in Base64. I saw that to generate a PrivateKey in Java/Scala through KeyFactory, I have to delete "-----BEGIN PRIVATE KEY-----" and "-----END PRIVATE KEY-----" lines manually before to convert privateKey.pem to Base64 format. Is there some function/method/class that executes this deletion automatically?

P.S.: Unfortunately, I can't use BouncyCastle library at the moment.

Sree.Bh
  • 1,751
  • 2
  • 19
  • 25
C.S.
  • 77
  • 2
  • 9
  • Is not the private key represented as Base64 in the key text? Any binary data is normally converted to ASCII text as Base64 (`A-Za-z0-9+/`) – Joop Eggen Jun 08 '20 at 11:16
  • When I convert the whole private key string from PEM file with Base64.getDecoder().decode() without delete "-----BEGIN PRIVATE KEY-----" and "-----END PRIVATE KEY-----" lines before, I have the following error: illegal base64 2d character, where "2d" is "-" character in the line "-----BEGIN PRIVATE KEY-----", so I have to delete those lines to execute conversion successfully – C.S. Jun 08 '20 at 12:23
  • I misunderstood _"convert ... to Base64"_, So it is _from Base64_, which makes more sense. Unfortunately I could not find an other utility function either https://stackoverflow.com/questions/11787571/how-to-read-pem-file-to-get-private-and-public-key – Joop Eggen Jun 08 '20 at 13:22
  • Sorry, my error! However I have already read this list of solutions, thanks anyway for the help! – C.S. Jun 08 '20 at 15:32
  • 1
    No, there isn't such a method, but it only takes a few lines of code to delete the header and footer and base64 decode the middle into the acceptable DER format. – President James K. Polk Jun 08 '20 at 21:02

0 Answers0