codice:
public class Base64Code
{
private static char[] alphabet =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
};
private static char padding = '=';
public static char[] encodeChar (byte[] data)
{
if (data == null)
throw new NullPointerException ("'data' parameter cannot be null");
int i = 0, j = 0, len = data.length, tmp;
char[] buf = new char[4*((len*8+23)/24)];
while (len > 0)
{
tmp = (data[i++] & 0xFF) << 16;
if (len >= 2)
tmp |= (data[i++] & 0xFF) << 8;
if (len >= 3)
tmp |= data[i++] & 0xFF;
buf[j++] = alphabet[tmp >>> 18];
buf[j++] = alphabet[tmp >>> 12 & 63];
buf[j++] = len >= 2 ? alphabet[tmp >>> 6 & 63] : padding;
buf[j++] = len >= 3 ? alphabet[tmp & 63] : padding;
len -= 3;
}
return buf;
}
public static String encodeString (byte[] data)
{
return new String (encodeChar (data));
}
}
Non ho fatto il decode ... semplicemente perché sono pigro.