8

I am trying to encode and decode a String using android.util.Base64, but it gives a bad base-64 error.

The code with the problem is:

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

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



    return returnbyteArray;
}

The error logcat is:

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)

The input(the str String from the code) looks like this:

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

It is a double converted to a string, using:

String.valueOf(number)

The error is persistent with all encoding flags (DEFAULT, NO_WRAP, etc), any help would be apreciated, thank you.

Alexandru Patriche
  • 135
  • 1
  • 2
  • 6

3 Answers3

9

You are trying to decode 26.919047981500626 which you can't. Because it's not a valid base64 encoded string.

When put into this online base64 decoder, it gives this error:

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.

Update:

If you want to know the valid formatting of a encoded base64 string, take a look at the table in the wikipedia article and also this answer which shows a base64 validator code in C#.

Community
  • 1
  • 1
Mahm00d
  • 3,881
  • 8
  • 44
  • 83
  • Ok i see, but why is it not a valid Base64 string? and if not what can i use instead to fix this problem? – Alexandru Patriche Aug 09 '14 at 13:21
  • See my update. But I still don't understand what you're trying to achieve here... If you want a valid Base64-encoded string, just put an arbitrary string in [this site](http://www.base64encode.org/) to encode it for you. – Mahm00d Aug 10 '14 at 07:40
9

remove the prefix string " data:image/png;base64, ", get the string of after "data:image/png;base64,", it can decode

edo
  • 171
  • 2
  • 2
0

It hit me, if in any case your are splitting the data & the encoding it, then decode it first before putting it back together.. Stupidly I was doing decode after collation of data that left me "bad-base-64" as each block of encode data had file enders(==).

This is Linkis handy anyways to validate the encoding and decoding.

Abhijeet
  • 8,561
  • 5
  • 70
  • 76