Base64 Reviews


Base64 is a freeware way of encoding 8-bit characters using
only ASCII printable characters similar to UUENCODE.
UUENCODE embeds a filename where BASE64 does not. You will
see BASE64 used in encoding digital certificates, in
encoding user:password string in an Authorization: header
for HTTP. The spec is described in RFC 2045.

Don’t confuse Base64 with x-www-form-urlencoded which
is handled by java.net.URLEncoder.encode/decode or
Base64u.

Base64 armouring uses only the characters A-Z a-z 0-9 +/=.
This makes it suitable for encoding binary data as SQL
strings, that will work no matter what the encoding.
Unfortunately + / and = all have special meaning in URLs.

Base64u gets around this problem. It is a variant on Base64
that uses – _ and * in preference to + / and =, so that it
can be used in URLEncoded contexts with or without
URLEncoding.

Use base64 like this:

// Base64 armouring
import com.mindprod.base64.Base64;

// sample byte array to encode
byte[] toSend = { (byte)0xfc, (byte)0x0f, (byte)0xc0};

// create encoder object
Base64 base64 = new Base64();
base64.setLineLength( 72 ); // default

// encoding a byte[]
String send = base64.encoder( toSend );

// decoding a byte[]
byte[] reconstituted = base64.decoder( sent );

use Base64u the same way:

// Base64u armouring
import com.mindprod.base64.Base64u;

// sample byte array to encode
byte[] toSend = { (byte)0xfc, (byte)0x0f, (byte)0xc0};

// create encoder object
Base64u base64u = new Base64u();
base64u.setLineLength( 72 ); // default

// encoding a byte[]
String send = base64u.encoder( toSend );

// decoding a byte[]
byte[] reconstituted = base64u.decoder( sent );

For an example that starts and ends with a String, see
Example.java

to run:
java.exe com.mindprod.base64.Example


Leave a Reply

Your email address will not be published. Required fields are marked *