Base64不编码整个字符串

问题描述 投票:1回答:1

当我编码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
java android base64 encode
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.