Skip to content

Instantly share code, notes, and snippets.

@scizzr
Forked from EmilHernvall/Base64.java
Last active December 18, 2015 00:19
Show Gist options
  • Save scizzr/5695546 to your computer and use it in GitHub Desktop.
Save scizzr/5695546 to your computer and use it in GitHub Desktop.
public class Base64
{
public static String encode(byte[] data)
{
String encoded = "";
String split = "";
String tbl = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
byte[] data;
//try to use the appropriate encoding string, but fallback to default locale
try { data = string.getBytes("UTF-8");
} catch (Exception ex) { data = string.getBytes(); }
//determine how many padding bytes to add to the end
int paddingCount = (3 - (data.length % 3)) % 3;
//add any necessary padding to the input
byte[] padded = new byte[data.length + paddingCount];
System.arraycopy(data, 0, padded, 0, data.length);
data = padded;
//proces 3 bytes at a time, output 4 bytes at a time
//worry about CRLF later
for (int i = 0; i < data.length; i += 3) {
int j = ((data[i] & 0xff) << 16) + ((data[i + 1] & 0xff) << 8) + (data[i + 2] & 0xff);
encoded = encoded + tbl.charAt((j >> 18) & 0x3f) + tbl.charAt((j >> 12) & 0x3f) + tbl.charAt((j >> 6) & 0x3f) + tbl.charAt(j & 0x3f);
}
//replace encoded padding nulls with "="
encoded = encoded.substring(0, encoded.length() - paddingCount) + "==".substring(0, paddingCount);
//split into multiple lines
for (int i = 0; i < encoded.length(); i += 76) {
split += encoded.substring(i, Math.min(encoded.length(), i + 76)) + "\r\n";
}
//trim the new line at the end of the string and return the result
return split.trim();
}
public static byte[] decode(String data)
{
int[] tbl = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
byte[] bytes = data.getBytes();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < bytes.length; ) {
int b = 0;
if (tbl[bytes[i]] != -1) {
b = (tbl[bytes[i]] & 0xFF) << 18;
}
// skip unknown characters
else {
i++;
continue;
}
if (i + 1 < bytes.length && tbl[bytes[i+1]] != -1) {
b = b | ((tbl[bytes[i+1]] & 0xFF) << 12);
}
if (i + 2 < bytes.length && tbl[bytes[i+2]] != -1) {
b = b | ((tbl[bytes[i+2]] & 0xFF) << 6);
}
if (i + 3 < bytes.length && tbl[bytes[i+3]] != -1) {
b = b | (tbl[bytes[i+3]] & 0xFF);
}
while ((b & 0xFFFFFF) != 0) {
int c = (b & 0xFF0000) >> 16;
buffer.append((char)c);
b <<= 8;
}
i += 4;
}
return buffer.toString().getBytes();
}
}
# script to generate the reverse lookup table
s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
res = [(ord(c), i) for i,c in enumerate(s)]
lookup = dict(res)
f = []
for i in xrange(0,255):
if lookup.has_key(i):
n = str(lookup[i])
if len(n) == 1:
n = " " + n
f.append(" " + n)
else:
f.append(" " + str(-1))
print len(f)
print ",".join(f)
@Edgarins29
Copy link

Hi,
I tried using decoding part of code, but couldn't get working decode, so it decodes:
String stru = "c8SBbHM=";
It returns "sÄ�ls", but it should return "sāls", it is probably some problem with UTF-8.

I also tried to incorporate some conversion from bytes to String but still I'm stuck.

Encoding part is working fine with UTF-8, thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment