Skip to content

Instantly share code, notes, and snippets.

@chenrui333
Last active July 20, 2016 14:02
Show Gist options
  • Save chenrui333/0837b214a6824ae20d120492f791833a to your computer and use it in GitHub Desktop.
Save chenrui333/0837b214a6824ae20d120492f791833a to your computer and use it in GitHub Desktop.

Problem statement:

We still used sun proprietary API (sun.misc.BASE64Decoder, sun.misc.BASE64Encoder), which gives compilation warnings. So someday, I decide it to the codec replacement.

BASE64 codec choices:

  • apache commons
  • javax.xml.bind
  • java8 BASE64 API

illegal argument issue

whitespace issue http://stackoverflow.com/questions/3092019/can-a-base64-encoded-string-contain-whitespace

No it can't. See Base64 for the allowed character repository used by base64, which are the characters A-Z, a-z, 0-9, + and / (the last two may differ depending on the implementation) as well as the padding character = (but that's also implementation dependent as some implementations don't use padding at all).

.replaceAll("\\s", "") on the string which contains whitespace will do the work

byte[] decordedValue = Base64.getDecoder().decode(valueToDecrypt.replaceAll("\\s", ""));

Misc. java.util.Base64 vs android.util.Base64

SO Post

On android, Use Base64.NO_WRAP instead of Base64.DEFAULT

	@Override
	protected String encode(byte[] bytes) {
	    return Base64.encodeToString(bytes, Base64.NO_WRAP);
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment