Base64不会对整个字符串进行编码

时间:2019-02-18 11:50:00

标签: java android base64 encode

当我编码tex时,由于某种原因,它会截断一部分字符串...可能是什么问题?

        DateFormat dateFormat =
                new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a", Locale.ENGLISH);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR,+ 9);
        String server_time = dateFormat.format(calendar.getTime());
        String wmsAuthSign = "server_time=" + server_time + "&hash_value=U2QK9TLB55JWTZr3OKZHtg==&validminutes=120";
        wmsAuthSign = "?wmsAuthSign=" + Base64.encodeToString(wmsAuthSign.getBytes(), Base64.DEFAULT);

我正在提交这样的内容:

server_time=02/18/2019 23:38:43 PM&hash_value=U2QK9TLB55JWTZr3OKZHtg==&validminutes=120

如果解码编码的文本,则会得到修剪后的结果:

server_time=02/18/2019 23:38:43 PM&hash_value=U2QK9TLB55J

1 个答案:

答案 0 :(得分:3)

由于RFC-2045

(5)   (Soft Line Breaks) The Quoted-Printable encoding
      REQUIRES that encoded lines be no more than 76
      characters long.  If longer lines are to be encoded
      with the Quoted-Printable encoding, "soft" line breaks

源数据字符串:

server_time=02/18/2019 23:38:43 PM&hash_value=U2QK9TLB55JWTZr3OKZHtg==&validminutes=120

Base64编码为字符串:

c2VydmVyX3RpbWU9MDIvMTgvMjAxOSAyMzoxMjo1NiBQTSZoYXNoX3ZhbHVlPVUyUUs5VExCNTVK
V1RacjNPS1pIdGc9PSZ2YWxpZG1pbnV0ZXM9MTIw

与上面显示的完全一样:带有换行符。但是在接收方,您可能只解码第一行

c2VydmVyX3RpbWU9MDIvMTgvMjAxOSAyMzoxMjo1NiBQTSZoYXNoX3ZhbHVlPVUyUUs5VExCNTVK

server_time=02/18/2019 23:12:21 PM&hash_value=U2QK9TLB55J

因此,不仅在第一行,还要在接收方解码整个接收到的数据。

或者您可能只将编码的Base64的第一行发送到接收方。

还要看看thisMohammad Adil答案:

  

在android上,使用Base64.NO_WRAP代替Base64.DEFAULT