【问题标题】:Proper way to convert a base64 encoded string to p12 file using Java Android使用Java Android将base64编码字符串转换为p12文件的正确方法
【发布时间】:2021-07-14 07:35:21
【问题描述】:

我有一个由 API 返回的 base64 编码字符串 p12 文件。我正在尝试将其保存到文件中,但在我的手机上打开时无法使用。

出了什么问题: 解码字符串并将其保存到 .p12 文件后。该文件无法打开,因为它似乎已损坏。我正在尝试寻找另一种方法来解码 base64 字符串以使其工作。

我尝试过的:

  1. 调用API获取Base64编码字符串
  2. 使用我在搜索时获得的以下代码对其进行解码(感谢所有者)。
   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. 使用此代码将其保存到文件中。
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);
        }




    }

但我似乎没有运气让它发挥作用。请帮助我正确地将 base64 字符串转换为 p12 文件。

【问题讨论】:

  • 任何人请帮助我..
  • void writeBytesToFile ??你的意思是decodeBase64AndWriteToFile 我想。
  • I have no luck to make it work 目前还不清楚什么不起作用。我喜欢你使用自己的 decode() 函数……但你知道 Android 也提供 base64 编码/解码吗?
  • ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 如果您的意图是保存到文件,那么不要首先创建一个包含整个文件的字节数组,而是在此处打开 FileOutputStream 并写入流。但无论如何……你没有说出了什么问题……
  • if(!directory.exists()){ directory.mkdir(); } 应该是:if(!directory.exists()){ if ( !directory.mkdir()) return; } 然后不仅返回,而且显示 Toast 以通知用户。

标签: java android pkcs#12 p12


【解决方案1】:

对于那些试图做同样事情的人来说,经过数小时的反复试验。我通过运行 Shell 命令使用 Java ProcessBuilder 对 base64 字符串进行解码使其工作。使用的命令:

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

我不知道为什么 base64 解码的输出与 windows 和 linux 命令行不同,但这种解决方法很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2019-02-16
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2021-12-12
    • 2018-08-05
    相关资源
    最近更新 更多