一、前言
关于grizzly-nio-framework-1.9.15b源码包中com.sun.grizzly.util.buf.Base64Utils加解密工具类,基于base64算法实现常用应用场景的加解密,详情源码说明。
二、源码说明
package com.sun.grizzly.util.buf;@b@@b@import java.util.Arrays;@b@@b@public class Base64Utils@b@{@b@ private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();@b@ private static final int[] IA = new int[256];@b@@b@ public static final char[] encodeToChar(byte[] sArr, boolean lineSep)@b@ {@b@ int sLen = (sArr != null) ? sArr.length : 0;@b@ if (sLen == 0)@b@ return new char[0];@b@@b@ int eLen = sLen / 3 * 3;@b@ int cCnt = (sLen - 1) / 3 + 1 << 2;@b@ int dLen = cCnt + ((lineSep) ? (cCnt - 1) / 76 << 1 : 0);@b@ char[] dArr = new char[dLen];@b@@b@ int s = 0; int d = 0; for (int cc = 0; s < eLen; )@b@ {@b@ int i = (sArr[(s++)] & 0xFF) << 16 | (sArr[(s++)] & 0xFF) << 8 | sArr[(s++)] & 0xFF;@b@@b@ dArr[(d++)] = CA[(i >>> 18 & 0x3F)];@b@ dArr[(d++)] = CA[(i >>> 12 & 0x3F)];@b@ dArr[(d++)] = CA[(i >>> 6 & 0x3F)];@b@ dArr[(d++)] = CA[(i & 0x3F)];@b@@b@ if ((lineSep) && (++cc == 19) && (d < dLen - 2)) {@b@ dArr[(d++)] = '\r';@b@ dArr[(d++)] = '\n';@b@ cc = 0;@b@ }@b@@b@ }@b@@b@ int left = sLen - eLen;@b@ if (left > 0)@b@ {@b@ int i = (sArr[eLen] & 0xFF) << 10 | ((left == 2) ? (sArr[(sLen - 1)] & 0xFF) << 2 : 0);@b@@b@ dArr[(dLen - 4)] = CA[(i >> 12)];@b@ dArr[(dLen - 3)] = CA[(i >>> 6 & 0x3F)];@b@ dArr[(dLen - 2)] = ((left == 2) ? CA[(i & 0x3F)] : '=');@b@ dArr[(dLen - 1)] = '=';@b@ }@b@ return dArr;@b@ }@b@@b@ public static final byte[] decode(char[] sArr)@b@ {@b@ int sLen = (sArr != null) ? sArr.length : 0;@b@ if (sLen == 0) {@b@ return new byte[0];@b@ }@b@@b@ int sepCnt = 0;@b@ for (int i = 0; i < sLen; ++i)@b@ if (IA[sArr[i]] < 0)@b@ ++sepCnt;@b@@b@@b@ if ((sLen - sepCnt) % 4 != 0)@b@ return null;@b@@b@ int pad = 0;@b@ int i = sLen;@b@ while (true) { do if ((i <= 1) || (IA[sArr[(--i)]] > 0)) break label97;@b@ while (sArr[i] != '=');@b@ ++pad;@b@ }@b@ label97: int len = ((sLen - sepCnt) * 6 >> 3) - pad;@b@@b@ byte[] dArr = new byte[len];@b@@b@ int s = 0; for (int d = 0; d < len; )@b@ {@b@ int i = 0;@b@ for (int j = 0; j < 4; ++j) {@b@ int c = IA[sArr[(s++)]];@b@ if (c >= 0)@b@ i |= c << 18 - j * 6;@b@ else@b@ --j;@b@ }@b@@b@ dArr[(d++)] = (byte)(i >> 16);@b@ if (d < len) {@b@ dArr[(d++)] = (byte)(i >> 8);@b@ if (d < len)@b@ dArr[(d++)] = (byte)i;@b@ }@b@ }@b@ return dArr;@b@ }@b@@b@ public static final byte[] decodeFast(char[] sArr)@b@ {@b@ int i;@b@ int r;@b@ int sLen = sArr.length;@b@ if (sLen == 0)@b@ return new byte[0];@b@@b@ int sIx = 0; int eIx = sLen - 1;@b@@b@ while ((sIx < eIx) && (IA[sArr[sIx]] < 0)) {@b@ ++sIx;@b@ }@b@@b@ while ((eIx > 0) && (IA[sArr[eIx]] < 0)) {@b@ --eIx;@b@ }@b@@b@ int pad = (sArr[eIx] == '=') ? 1 : (sArr[(eIx - 1)] == '=') ? 2 : 0;@b@ int cCnt = eIx - sIx + 1;@b@ int sepCnt = (sLen > 76) ? ((sArr[76] == '\r') ? cCnt / 78 : 0) << 1 : 0;@b@@b@ int len = ((cCnt - sepCnt) * 6 >> 3) - pad;@b@ byte[] dArr = new byte[len];@b@@b@ int d = 0;@b@ int cc = 0; for (int eLen = len / 3 * 3; d < eLen; )@b@ {@b@ int i = IA[sArr[(sIx++)]] << 18 | IA[sArr[(sIx++)]] << 12 | IA[sArr[(sIx++)]] << 6 | IA[sArr[(sIx++)]];@b@@b@ dArr[(d++)] = (byte)(i >> 16);@b@ dArr[(d++)] = (byte)(i >> 8);@b@ dArr[(d++)] = (byte)i;@b@@b@ if ((sepCnt > 0) && (++cc == 19)) {@b@ sIx += 2;@b@ cc = 0;@b@ }@b@ }@b@@b@ if (d < len)@b@ {@b@ i = 0;@b@ for (int j = 0; sIx <= eIx - pad; ++j)@b@ i |= IA[sArr[(sIx++)]] << 18 - j * 6;@b@@b@ for (r = 16; d < len; r -= 8)@b@ dArr[(d++)] = (byte)(i >> r);@b@ }@b@@b@ return dArr;@b@ }@b@@b@ public static final byte[] encodeToByte(byte[] sArr, boolean lineSep)@b@ {@b@ int sLen = (sArr != null) ? sArr.length : 0;@b@ if (sLen == 0)@b@ return new byte[0];@b@@b@ int eLen = sLen / 3 * 3;@b@ int cCnt = (sLen - 1) / 3 + 1 << 2;@b@ int dLen = cCnt + ((lineSep) ? (cCnt - 1) / 76 << 1 : 0);@b@ byte[] dArr = new byte[dLen];@b@@b@ int s = 0; int d = 0; for (int cc = 0; s < eLen; )@b@ {@b@ int i = (sArr[(s++)] & 0xFF) << 16 | (sArr[(s++)] & 0xFF) << 8 | sArr[(s++)] & 0xFF;@b@@b@ dArr[(d++)] = (byte)CA[(i >>> 18 & 0x3F)];@b@ dArr[(d++)] = (byte)CA[(i >>> 12 & 0x3F)];@b@ dArr[(d++)] = (byte)CA[(i >>> 6 & 0x3F)];@b@ dArr[(d++)] = (byte)CA[(i & 0x3F)];@b@@b@ if ((lineSep) && (++cc == 19) && (d < dLen - 2)) {@b@ dArr[(d++)] = 13;@b@ dArr[(d++)] = 10;@b@ cc = 0;@b@ }@b@@b@ }@b@@b@ int left = sLen - eLen;@b@ if (left > 0)@b@ {@b@ int i = (sArr[eLen] & 0xFF) << 10 | ((left == 2) ? (sArr[(sLen - 1)] & 0xFF) << 2 : 0);@b@@b@ dArr[(dLen - 4)] = (byte)CA[(i >> 12)];@b@ dArr[(dLen - 3)] = (byte)CA[(i >>> 6 & 0x3F)];@b@ dArr[(dLen - 2)] = ((left == 2) ? (byte)CA[(i & 0x3F)] : 61);@b@ dArr[(dLen - 1)] = 61;@b@ }@b@ return dArr;@b@ }@b@@b@ public static final byte[] decode(byte[] sArr)@b@ {@b@ int sLen = sArr.length;@b@@b@ int sepCnt = 0;@b@ for (int i = 0; i < sLen; ++i)@b@ if (IA[(sArr[i] & 0xFF)] < 0)@b@ ++sepCnt;@b@@b@@b@ if ((sLen - sepCnt) % 4 != 0)@b@ return null;@b@@b@ int pad = 0;@b@ int i = sLen;@b@ while (true) { do if ((i <= 1) || (IA[(sArr[(--i)] & 0xFF)] > 0)) break label89;@b@ while (sArr[i] != 61);@b@ ++pad;@b@ }@b@ label89: int len = ((sLen - sepCnt) * 6 >> 3) - pad;@b@@b@ byte[] dArr = new byte[len];@b@@b@ int s = 0; for (int d = 0; d < len; )@b@ {@b@ int i = 0;@b@ for (int j = 0; j < 4; ++j) {@b@ int c = IA[(sArr[(s++)] & 0xFF)];@b@ if (c >= 0)@b@ i |= c << 18 - j * 6;@b@ else@b@ --j;@b@@b@ }@b@@b@ dArr[(d++)] = (byte)(i >> 16);@b@ if (d < len) {@b@ dArr[(d++)] = (byte)(i >> 8);@b@ if (d < len)@b@ dArr[(d++)] = (byte)i;@b@ }@b@ }@b@@b@ return dArr;@b@ }@b@@b@ public static final byte[] decodeFast(byte[] sArr)@b@ {@b@ int i;@b@ int r;@b@ int sLen = sArr.length;@b@ if (sLen == 0)@b@ return new byte[0];@b@@b@ int sIx = 0; int eIx = sLen - 1;@b@@b@ while ((sIx < eIx) && (IA[(sArr[sIx] & 0xFF)] < 0)) {@b@ ++sIx;@b@ }@b@@b@ while ((eIx > 0) && (IA[(sArr[eIx] & 0xFF)] < 0)) {@b@ --eIx;@b@ }@b@@b@ int pad = (sArr[eIx] == 61) ? 1 : (sArr[(eIx - 1)] == 61) ? 2 : 0;@b@ int cCnt = eIx - sIx + 1;@b@ int sepCnt = (sLen > 76) ? ((sArr[76] == 13) ? cCnt / 78 : 0) << 1 : 0;@b@@b@ int len = ((cCnt - sepCnt) * 6 >> 3) - pad;@b@ byte[] dArr = new byte[len];@b@@b@ int d = 0;@b@ int cc = 0; for (int eLen = len / 3 * 3; d < eLen; )@b@ {@b@ int i = IA[sArr[(sIx++)]] << 18 | IA[sArr[(sIx++)]] << 12 | IA[sArr[(sIx++)]] << 6 | IA[sArr[(sIx++)]];@b@@b@ dArr[(d++)] = (byte)(i >> 16);@b@ dArr[(d++)] = (byte)(i >> 8);@b@ dArr[(d++)] = (byte)i;@b@@b@ if ((sepCnt > 0) && (++cc == 19)) {@b@ sIx += 2;@b@ cc = 0;@b@ }@b@ }@b@@b@ if (d < len)@b@ {@b@ i = 0;@b@ for (int j = 0; sIx <= eIx - pad; ++j)@b@ i |= IA[sArr[(sIx++)]] << 18 - j * 6;@b@@b@ for (r = 16; d < len; r -= 8)@b@ dArr[(d++)] = (byte)(i >> r);@b@ }@b@@b@ return dArr;@b@ }@b@@b@ public static final String encodeToString(byte[] sArr, boolean lineSep)@b@ {@b@ return new String(encodeToChar(sArr, lineSep));@b@ }@b@@b@ public static final byte[] decode(String str)@b@ {@b@ int sLen = (str != null) ? str.length() : 0;@b@ if (sLen == 0) {@b@ return new byte[0];@b@ }@b@@b@ int sepCnt = 0;@b@ for (int i = 0; i < sLen; ++i)@b@ if (IA[str.charAt(i)] < 0)@b@ ++sepCnt;@b@@b@@b@ if ((sLen - sepCnt) % 4 != 0) {@b@ return null;@b@ }@b@@b@ int pad = 0;@b@ int i = sLen;@b@ while (true) { do if ((i <= 1) || (IA[str.charAt(--i)] > 0)) break label105;@b@ while (str.charAt(i) != '=');@b@ ++pad;@b@ }@b@ label105: int len = ((sLen - sepCnt) * 6 >> 3) - pad;@b@@b@ byte[] dArr = new byte[len];@b@@b@ int s = 0; for (int d = 0; d < len; )@b@ {@b@ int i = 0;@b@ for (int j = 0; j < 4; ++j) {@b@ int c = IA[str.charAt(s++)];@b@ if (c >= 0)@b@ i |= c << 18 - j * 6;@b@ else@b@ --j;@b@ }@b@@b@ dArr[(d++)] = (byte)(i >> 16);@b@ if (d < len) {@b@ dArr[(d++)] = (byte)(i >> 8);@b@ if (d < len)@b@ dArr[(d++)] = (byte)i;@b@ }@b@ }@b@ return dArr;@b@ }@b@@b@ public static final byte[] decodeFast(String s)@b@ {@b@ int i;@b@ int r;@b@ int sLen = s.length();@b@ if (sLen == 0)@b@ return new byte[0];@b@@b@ int sIx = 0; int eIx = sLen - 1;@b@@b@ while ((sIx < eIx) && (IA[(s.charAt(sIx) & 0xFF)] < 0)) {@b@ ++sIx;@b@ }@b@@b@ while ((eIx > 0) && (IA[(s.charAt(eIx) & 0xFF)] < 0)) {@b@ --eIx;@b@ }@b@@b@ int pad = (s.charAt(eIx) == '=') ? 1 : (s.charAt(eIx - 1) == '=') ? 2 : 0;@b@ int cCnt = eIx - sIx + 1;@b@ int sepCnt = (sLen > 76) ? ((s.charAt(76) == '\r') ? cCnt / 78 : 0) << 1 : 0;@b@@b@ int len = ((cCnt - sepCnt) * 6 >> 3) - pad;@b@ byte[] dArr = new byte[len];@b@@b@ int d = 0;@b@ int cc = 0; for (int eLen = len / 3 * 3; d < eLen; )@b@ {@b@ int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6 | IA[s.charAt(sIx++)];@b@@b@ dArr[(d++)] = (byte)(i >> 16);@b@ dArr[(d++)] = (byte)(i >> 8);@b@ dArr[(d++)] = (byte)i;@b@@b@ if ((sepCnt > 0) && (++cc == 19)) {@b@ sIx += 2;@b@ cc = 0;@b@ }@b@ }@b@@b@ if (d < len)@b@ {@b@ i = 0;@b@ for (int j = 0; sIx <= eIx - pad; ++j)@b@ i |= IA[s.charAt(sIx++)] << 18 - j * 6;@b@@b@ for (r = 16; d < len; r -= 8)@b@ dArr[(d++)] = (byte)(i >> r);@b@ }@b@@b@ return dArr;@b@ }@b@@b@ static@b@ {@b@ Arrays.fill(IA, -1);@b@ int i = 0; for (int iS = CA.length; i < iS; ++i)@b@ IA[CA[i]] = i;@b@ IA[61] = 0;@b@ }@b@}