0

I have tried the same using java but the resulting byte array is not correct:

public void  publicKeyConvert{
    final String PUBLIC_KEY_STRING="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxl4aRnRMBB9GZk";
try{
    System.out.println("Public Key in byte[] :"+ Arrays.toString(Base64.decode(PUBLIC_KEY_STRING, Base64.DEFAULT)));
} catch (Exception ex) {
    Log.e("Error converting key", ex.getMessage());
}

Rather than converting the string as I tried in my example I am looking for a solution where I can input the public key file (.pem format) and receive the byte array as output preferably using OpenSSL.

jww
  • 97,681
  • 90
  • 411
  • 885
Praj
  • 1
  • 1
  • 2
  • Here's a similar duplicate for OpenSSL: [How to pass PEM certificate as first arg of i2d_X509](http://stackoverflow.com/q/29970497). You read the PEM encoded certificate with `PEM_read_bio_X509`, then you write in DER with `i2d_X509_bio`. Use the key functions rather than the certificate functions. – jww May 20 '16 at 16:15

1 Answers1

0

Here is how you can convert your String to a byte array:

String PUBLIC_KEY_STRING = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxl4aRnRMBB9GZk";
byte[] bytes = DatatypeConverter.parseBase64Binary(PUBLIC_KEY_STRING);
System.out.println(Arrays.toString(bytes));
Mifeet
  • 12,949
  • 5
  • 60
  • 108