Bug in Base64.decode()

21 views
Skip to first unread message

Thomas

unread,
Apr 24, 2018, 9:07:41 PM4/24/18
to CodenameOne Discussions
When I do:

```
String encoded = "eyJ1c2VySWQiOiIxMSIsImlhdCI6MTUyNDYxNzU3MywiZXhwIjoxNTI0NzAzOTczLCJhdWQiOiJodHRwczovL3lvdXJkb21haW4uY29tIiwiaXNzIjoiZmVhdGhlcnMiLCJzdWIiOiJhbm9ueW1vdXMiLCJqdGkiOiIwMjYyYzM4MS05Njg4LTRjMDItYWIxOS0wNDQ4MTdjMjkxMmYifQ";
System.out.println("Decoded :"+new String(Base64.decode(encoded.getBytes())));
```
the result I get is:

```
Decoded :{"userId":"11","iat":1524617573,"exp":1524703973,"aud":"https://yourdomain.com","iss":"feathers","sub":"anonymous","jti":"0262c381-9688-4c02-ab19-044817c2912f"
```

whereas the correct base64 decoding (see https://www.base64decode.org/ for example) should be:

```
{"userId":"11","iat":1524617573,"exp":1524703973,"aud":"https://yourdomain.com","iss":"feathers","sub":"anonymous","jti":"0262c381-9688-4c02-ab19-044817c2912f" }
```
Notice the extra '}' at the end of the decoded String.

So it looks like the Base64.decode() function is bugged and strip some final characters in some cases...


IDE: Eclipse
Windows 10 x64
Simulator

Shai Almog

unread,
Apr 25, 2018, 1:33:31 AM4/25/18
to CodenameOne Discussions
You need to terminate a base64 string with ==. It can work without it in some decoders but that's the standard.

Thomas

unread,
Apr 25, 2018, 1:39:41 AM4/25/18
to CodenameOne Discussions
OK Thanks. Didn't know that. I am using Base64.decode() to decode the different parts of a jwt token that do not have the `==` suffix. I will just add this at the end of all my jwt parts before decoding them as it seems to fix the issue.
Reply all
Reply to author
Forward
0 new messages