Skip to content

Instantly share code, notes, and snippets.

@orca-zhang
Last active May 29, 2019 02:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orca-zhang/4874c48590eeeb27dec828a81103371f to your computer and use it in GitHub Desktop.
Save orca-zhang/4874c48590eeeb27dec828a81103371f to your computer and use it in GitHub Desktop.
simple base64 decode snippet
import java.util.Arrays;
public class main {
private static byte base64_reverse_table[] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2, 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, -2, -2, -2, -2, -2,
-2, 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, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};
public static byte[] base64Decode(String data) throws Exception {
byte[] bs = new byte[(data.length() + 2)/4*3];
int k = 0;
for (int i = 0, j = 0; i < data.length(); ++i) {
char ch = data.charAt(i);
if (ch == '=') {
int tail = j & 3;
if (tail == 0 || tail == 1||
(tail == 2 && (i != data.length() - 2 || data.charAt(i + 1) != '=')) ||
(tail == 3 && i != data.length() - 1)
) {
throw new Exception("malformed base64 encoding string");
}
break;
}
byte ck = base64_reverse_table[ch];
if (ck == -1) {
continue;
} else if (ck == -2) {
throw new Exception("malformed base64 encoding string");
}
switch(j & 3) {
case 0:
bs[k] = (byte)(ck << 2);
break;
case 1:
bs[k++] |= ck >> 4;
bs[k] = (byte)((ck & 0x0f) << 4);
break;
case 2:
bs[k++] |= ck >>2;
bs[k] = (byte)((ck & 0x03) << 6);
break;
case 3:
bs[k++] |= ck;
break;
}
j++;
}
return Arrays.copyOfRange(bs, 0, k);
}
public static void main(String[] args) {
try {
System.out.println(new String(base64Decode("MQ==")));
System.out.println(new String(base64Decode("MTI=")));
System.out.println(new String(base64Decode("MTIz")));
System.out.println(new String(base64Decode("MTIzNA==")));
System.out.println(new String(base64Decode("MTIzNDU=")));
System.out.println(new String(base64Decode("5rWL6K+V5pWw5o2u")));
System.out.println(new String(base64Decode("JXU2RDRCJXU4QkQ1JXU2NTcwJXU2MzZF")));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment