Base64和Base62的目的在都在于将需要传输的内容进行编码,尤其是一些特殊字符(如不可见字符、传输时与协议头冲突的字符)。 在安全领域,base62的编码使用较多。具体编码和解码的方式如下:
Base64编码
- private static char[] encodes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
- .toCharArray();
- private static byte[] decodes = new byte[256];
- static {
- for (int i = 0; i < encodes.length; i++) {
- decodes[encodes[i]] = (byte) i;
- }
- }
- public static StringBuffer encodeBase64(byte[] data) {
- StringBuffer sb = new StringBuffer(data.length * 2);
- int pos = 0, val = 0;
- for (int i = 0; i < data.length; i++) {
- val = (val << 8) | (data[i] & 0xFF);
- pos += 8;
- while (pos > 5) {
- sb.append(encodes[val >> (pos -= 6)]);
- val &= ((1 << pos) - 1);
- }
- }
- if (pos > 0) {
- sb.append(encodes[val << (6 - pos)]);
- }
- return sb;
- }
- public static byte[] decodeBase64(char[] data) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
- int pos = 0, val = 0;
- for (int i = 0; i < data.length; i++) {
- val = (val << 6) | decodes[data[i]];
- pos += 6;
- while (pos > 7) {
- baos.write(val >> (pos -= 8));
- val &= ((1 << pos) - 1);
- }
- }
- return baos.toByteArray();
- }
Base62编码
- public static StringBuffer encodeBase62(byte[] data) {
- StringBuffer sb = new StringBuffer(data.length * 2);
- int pos = 0, val = 0;
- for (int i = 0; i < data.length; i++) {
- val = (val << 8) | (data[i] & 0xFF);
- pos += 8;
- while (pos > 5) {
- char c = encodes[val >> (pos -= 6)];
- sb.append(
- /**/c == 'i' ? "ia" :
- /**/c == '+' ? "ib" :
- /**/c == '/' ? "ic" : c);
- val &= ((1 << pos) - 1);
- }
- }
- if (pos > 0) {
- char c = encodes[val << (6 - pos)];
- sb.append(
- /**/c == 'i' ? "ia" :
- /**/c == '+' ? "ib" :
- /**/c == '/' ? "ic" : c);
- }
- return sb;
- }
- public static byte[] decodeBase62(char[] data) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);
- int pos = 0, val = 0;
- for (int i = 0; i < data.length; i++) {
- char c = data[i];
- if (c == 'i') {
- c = data[++i];
- c =
- /**/c == 'a' ? 'i' :
- /**/c == 'b' ? '+' :
- /**/c == 'c' ? '/' : data[--i];
- }
- val = (val << 6) | decodes[c];
- pos += 6;
- while (pos > 7) {
- baos.write(val >> (pos -= 8));
- val &= ((1 << pos) - 1);
- }
- }
- return baos.toByteArray();
- }
本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/423544,如需转载请自行联系原作者