Proper way to convert a base64 encoded string to p12 file using Java Android

1.3k views Asked by At

I have a base64 encoded String p12 file which is return by an API. I am trying to save it to file but won't work when I am opening it on my mobile phone.

What went wrong: After decoding the string and saving it to a .p12 file. The file cannot be opened as it seems to be corrupted. I am trying to find another way to decode a base64 string to make it work.

What I've tried:

  1. Call the API to get the Base64 encoded String
  2. Decode it using the following code I got while searching (Credits to the owner).
   public static byte[] decode(String data)
     {
       int[] tbl = {
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
               55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2,
               3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
               20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30,
               31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
               48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
               -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
       byte[] bytes = data.getBytes();
       ByteArrayOutputStream buffer = new ByteArrayOutputStream();
       for (int i = 0; i < bytes.length; ) {
           int b = 0;
           if (tbl[bytes[i]] != -1) {
               b = (tbl[bytes[i]] & 0xFF) << 18;
           }
           // skip unknown characters
           else {
               i++;
               continue;
           }

           int num = 0;
           if (i + 1 < bytes.length && tbl[bytes[i+1]] != -1) {
               b = b | ((tbl[bytes[i+1]] & 0xFF) << 12);
               num++;
           }
           if (i + 2 < bytes.length && tbl[bytes[i+2]] != -1) {
               b = b | ((tbl[bytes[i+2]] & 0xFF) << 6);
               num++;
           }
           if (i + 3 < bytes.length && tbl[bytes[i+3]] != -1) {
               b = b | (tbl[bytes[i+3]] & 0xFF);
               num++;
           }

           while (num > 0) {
               int c = (b & 0xFF0000) >> 16;
               buffer.write((char)c);
               b <<= 8;
               num--;
           }
           i += 4;
       }
       return buffer.toByteArray();
   }
  1. Save it in to file using this code.
public void writeBytesToFile(String encodedString)
            throws IOException {


        byte[] decodedBytes =  decode(encodedString);


        File directory = new File("storage/emulated/0/", "Certificates");
        if(!directory.exists()){
            directory.mkdir();
        }
        Log.d("BYTS", new String(decodedBytes));



        String fileFilname = certTypeStr.concat("p12file.pem");
        //Toast.makeText(MyApplication.getAppContext(), fileFilname, Toast.LENGTH_SHORT).show();
        File file = new File("storage/emulated/0/Certificates", fileFilname);

        if(!file.exists()){
            boolean createfile = file.createNewFile();
        }else{
            boolean deletefile = file.delete();
            boolean createfile = file.createNewFile();
        }


        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(decodedBytes);
        }




    }

but seems I have no luck to make it work. Pls help me to properly convert a base64 string to a p12 file.

1

There are 1 answers

0
Angelo Gabriel Dayao On

After many hours of trial and error for those who are trying to do the same thing. I made it work by running a Shell Command to decode the base64 string using Java ProcessBuilder. The command used :

base64 -d test.txt | base64 -d > test.p12

I don't know why the output of base64 decode differs from windows and linux commandline but this workaround is useful.