android - java.lang.IllegalArgumentException : bad base-64 异常

标签 android encoding base64 illegalargumentexception

我正在尝试使用 android.util.Base64 对字符串进行编码和解码,但它给出了错误的 base-64 错误。

有问题的代码是:

private byte[] base64ToByte(String str) throws IOException {

    Log.i("encription", str);
    byte[] returnbyteArray = Base64.decode(str, Base64.URL_SAFE);



    return returnbyteArray;
}

错误日志是:

08-09 13:02:18.589: E/AndroidRuntime(29827): Process: com.example.maptest, PID: 29827
08-09 13:02:18.589: E/AndroidRuntime(29827): java.lang.IllegalArgumentException: bad base-64
08-09 13:02:18.589: E/AndroidRuntime(29827):    at android.util.Base64.decode(Base64.java:161)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at android.util.Base64.decode(Base64.java:136)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at android.util.Base64.decode(Base64.java:118)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at com.example.maptest.security.Encription.base64ToByte(Encription.java:116)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at com.example.maptest.security.Encription.encode(Encription.java:103)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at android.os.Looper.loop(Looper.java:136)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at android.app.ActivityThread.main(ActivityThread.java:5081)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at java.lang.reflect.Method.invokeNative(Native Method)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at java.lang.reflect.Method.invoke(Method.java:515)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-09 13:02:18.589: E/AndroidRuntime(29827):    at dalvik.system.NativeStart.main(Native Method)

输入(代码中的 str 字符串)如下所示:

08-09 13:02:18.539: I/encription(29827): 26.919047981500626

它是一个转换为字符串的double,使用:

String.valueOf(number)

所有编码标志(DEFAULT、NO_WRAP 等)都存在错误,我们将不胜感激,谢谢。

最佳答案

您正在尝试解码 26.919047981500626,但您做不到。因为它不是有效的 base64 编码字符串。

当放入this online base64 decoder时,它给出了这个错误:

The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

更新:

如果您想知道编码的 base64 字符串的有效格式,请查看 wikipedia article 中的表格。还有this answer它显示了 C# 中的 base64 验证器代码。

关于android - java.lang.IllegalArgumentException : bad base-64 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25217515/

相关文章:

android - 在 Unity3D 和 ItextSharp 中使用自定义字体

java - 尝试在空对象引用上调用虚拟方法 ' '

python - 带有 % 的 Flask URL 参数未正确处理

python-3.x - 如何将图像(base64编码)上传到firebase?

php - 来自 android 的实时 javascript 请求或从 Controller Cakephp/php 调用 javascript 函数

json - 在 Go 中维护未解析的 JSON 字段的最佳方法是什么?

java - 使用 RSA 的加密套接字连接 java(IllegalArgumentException : Illegal base64 character 10)

java - Android中Base64文件上传IllegalArgument异常

postgresql - 在 GoLang 中将图像转换为 Base64

Android ACTION_IMAGE_CAPTURE Intent