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

Base64 does not encode the entire string

当我对 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

因为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 的第一行。

也看看 answer of Mohammad Adil:

On android, Use Base64.NO_WRAP instead of Base64.DEFAULT