简体   繁体   中英

Illegal Argument Exception: Illegal base64 character 3a when decoding String value using Base64.getDecode()

The string value I'm decoding is "ed:1234" but it is throwing an error of IllegalArgumentException. Would greatly appreciate if someone knows why I have this error.

Code:

String authInfo = "ed:1234";
byte[] bytes = Base64.getDecoder().decode(authInfo);

Error:

java.lang.IllegalArgumentException: Illegal base64 character 3a

The issue is the : ( ascii decimal 58 or hex 3a) is only valid in one (of several) Base64 encoding schemes, you want Base64.getMimeDecoder() . Like,

byte[] bytes = Base64.getMimeDecoder().decode(authInfo);
System.out.println(Arrays.toString(bytes));

which outputs (with no other changes)

[121, -35, 118, -33]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM