How to convert sun.misc.BASE64Encoder to org.apache.commons.codec.binary.Base64

Stephane Grenier picture Stephane Grenier · Apr 16, 2013 · Viewed 27.4k times · Source

I have the following code for sun.misc.BASE64Encoder:

BASE64Decoder decoder = new BASE64Decoder();
byte[] saltArray = decoder.decodeBuffer(saltD);
byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);

and would like to convert it to org.apache.commons.codec.binary.Base64. I've gone through the APIs, the docs, etc., but I can't find something that seems to match and give the same resulting values.

Answer

Stephane Grenier picture Stephane Grenier · Apr 16, 2013

It's actually almost the exact same:

Base64 decoder = new Base64();
byte[] saltArray = decoder.decode(saltD);
byte[] ciphertextArray = decoder.decode(ciphertext);

For decoding:

String saltString = encoder.encodeToString(salt);
String ciphertextString = encoder.encodeToString(ciphertext);

This last one was tougher because you use "toString" at the end.