1

I need to convert a Base 64 encoded public key file in .pem format to byte array using openssl.

Need to convert public key from the below format:

----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxl4aRnRMBB9GZrNgy6sy
seyIA3Ek3a1xg0b8gKrCSgnSjH+lqtBGwtzKqM020piE1q0YrzGpfQxNJ8HPRc0F
OlII7VKuCFGqEuLH2+kFJPUUdRqBr0iN/D8ADp9rLEL2Q4F7qM1t0z0IW6zD

-----END PUBLIC KEY----- 

to this format:

48, -127, -97, 48, 13, 6, 9, 42, -122, 72, -122, -9, 13,...
6
sed -e '/----.*PUBLIC KEY----\|^[[:space:]]*$/d' praj.pem | 
    base64 -d | 
    hexdump -v -e '/1 "%02d, "' ; echo

This uses sed to remove the blank lines and lines containing the BEGIN and END markers for the public key, then pipes that into base64 -d to decode it, and then pipes that into hexdump with a custom format to print the bytes as comma-and-space separated decimal numbers. Finally echo is used to make sure the output finishes with a newline (\n).

Output:

48, -126, 01, 34, 48, 13, 06, 09, 42, -122, 72, -122, -9, 13, 01, 01, 01, 05, ...
  • on macOS, you need a -D or --decode, for the Base64 decode part of the command.. – rustyMagnet Jul 5 '18 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.