com.jcohy.security.base64.Base64 Maven / Gradle / Ivy
The newest version!
package com.jcohy.security.base64;
import java.nio.charset.Charset;
/**
* Base64工具类,提供Base64的编码和解码方案
*
* @author Looly
*/
public class Base64 {
/**
* 标准编码表
*/
private final byte[] STANDARD_ENCODE_TABLE = {'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', '+', '/'};
/**
* URL安全的编码表,将 + 和 / 替换为 - 和 _
*/
private final byte[] URL_SAFE_ENCODE_TABLE = {'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', '-', '_'};
/**
* Base64解码表
*/
private final byte[] DECODE_TABLE = {-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, 62, -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, 63, -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};
// -------------------------------------------------------------------- encode
/**
* 编码为Base64,非URL安全的
*
* @param arr 被编码的数组
* @param lineSep 在76个char之后是CRLF还是EOF
* @return 编码后的bytes
*/
public byte[] encode(byte[] arr, boolean lineSep) {
return encode(arr, lineSep, false);
}
/**
* 编码为Base64
* 如果isMultiLine为true
,则每76个字符一个换行符,否则在一行显示
*
* @param arr 被编码的数组
* @param isMultiLine 在76个char之后是CRLF还是EOF
* @param isUrlSafe 是否使用URL安全字符,一般为false
* @return 编码后的bytes
*/
public byte[] encode(byte[] arr, boolean isMultiLine, boolean isUrlSafe) {
int len = arr != null ? arr.length : 0;
if (len == 0) {
return new byte[0];
}
int evenlen = (len / 3) * 3;
int cnt = ((len - 1) / 3 + 1) << 2;
int destlen = cnt + (isMultiLine ? (cnt - 1) / 76 << 1 : 0);
byte[] dest = new byte[destlen];
byte[] encodeTable = isUrlSafe ? URL_SAFE_ENCODE_TABLE : STANDARD_ENCODE_TABLE;
for (int s = 0, d = 0, cc = 0; s < evenlen; ) {
int i = (arr[s++] & 0xff) << 16 | (arr[s++] & 0xff) << 8 | (arr[s++] & 0xff);
dest[d++] = encodeTable[(i >>> 18) & 0x3f];
dest[d++] = encodeTable[(i >>> 12) & 0x3f];
dest[d++] = encodeTable[(i >>> 6) & 0x3f];
dest[d++] = encodeTable[i & 0x3f];
if (isMultiLine && ++cc == 19 && d < destlen - 2) {
dest[d++] = '\r';
dest[d++] = '\n';
cc = 0;
}
}
int left = len - evenlen;
if (left > 0) {
int i = ((arr[evenlen] & 0xff) << 10) | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0);
dest[destlen - 4] = encodeTable[i >> 12];
dest[destlen - 3] = encodeTable[(i >>> 6) & 0x3f];
dest[destlen - 2] = left == 2 ? encodeTable[i & 0x3f] : (byte) '=';
dest[destlen - 1] = '=';
}
return dest;
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @return 被加密后的字符串
*/
public String encode(String source) {
return encode(source, Charset.defaultCharset());
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public String encode(String source, String charset) {
return encode(getPrivateBytes(source, charset), charset);
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public String encode(String source, Charset charset) {
return encode(getPrivateBytes(source, charset.name()), charset);
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @return 被加密后的字符串
*/
public String encode(byte[] source) {
return encode(source, Charset.defaultCharset());
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public String encode(byte[] source, String charset) {
return getPrivateString(encode(source, false), Charset.forName(charset));
}
/**
* base64编码
*
* @param source 被编码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public String encode(byte[] source, Charset charset) {
return getPrivateString(encode(source, false), charset);
}
// -------------------------------------------------------------------- decode
/**
* base64解码
*
* @param source 被解码的base64字符串
* @return 被加密后的字符串
*/
public String decodeStr(String source) {
return decodeStr(source, Charset.defaultCharset());
}
/**
* base64解码
*
* @param source 被解码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public String decodeStr(String source, String charset) {
return getPrivateString(decode(source, charset), Charset.forName(charset));
}
/**
* base64解码
*
* @param source 被解码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public String decodeStr(String source, Charset charset) {
return getPrivateString(decode(source, charset), charset);
}
/**
* base64解码
*
* @param source 被解码的base64字符串
* @return 被加密后的字符串
*/
public byte[] decode(String source) {
return decode(source, Charset.defaultCharset());
}
/**
* base64解码
*
* @param source 被解码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public byte[] decode(String source, String charset) {
return decode(getPrivateBytes(source, charset));
}
/**
* base64解码
*
* @param source 被解码的base64字符串
* @param charset 字符集
* @return 被加密后的字符串
*/
public byte[] decode(String source, Charset charset) {
return decode(getPrivateBytes(source, charset.name()));
}
/**
* 解码Base64,同时解码URL安全和非安全的base64编码
*
* @param arr byte数组
* @return 解码后的byte数组
*/
public byte[] decode(byte[] arr) {
int length = arr.length;
if (length == 0) {
return new byte[0];
}
int sndx = 0, endx = length - 1;
int pad = arr[endx] == '=' ? (arr[endx - 1] == '=' ? 2 : 1) : 0;
int cnt = endx - sndx + 1;
int sepCnt = length > 76 ? (arr[76] == '\r' ? cnt / 78 : 0) << 1 : 0;
int len = ((cnt - sepCnt) * 6 >> 3) - pad;
byte[] dest = new byte[len];
// byte[] decodeTable = isUrlSafe ? URL_SAFE_DECODE_TABLE : STANDARD_DECODE_TABLE;
byte[] decodeTable = DECODE_TABLE;
int d = 0;
for (int cc = 0, eLen = (len / 3) * 3; d < eLen; ) {
int i = decodeTable[arr[sndx++]] << 18 | decodeTable[arr[sndx++]] << 12 | decodeTable[arr[sndx++]] << 6 | decodeTable[arr[sndx++]];
dest[d++] = (byte) (i >> 16);
dest[d++] = (byte) (i >> 8);
dest[d++] = (byte) i;
if (sepCnt > 0 && ++cc == 19) {
sndx += 2;
cc = 0;
}
}
if (d < len) {
int i = 0;
for (int j = 0; sndx <= endx - pad; j++) {
i |= decodeTable[arr[sndx++]] << (18 - j * 6);
}
for (int r = 16; d < len; r -= 8) {
dest[d++] = (byte) (i >> r);
}
}
return dest;
}
private byte[] getPrivateBytes(String data, String charsetString) {
if (data == null) {
return null;
}
Charset charset = charsetString == null || charsetString.length() == 0 || charsetString.trim().length() == 0 ? Charset.defaultCharset() : Charset.forName(charsetString);
return data.getBytes(charset);
}
private static String getPrivateString(byte[] bytes, Charset charset) {
if (bytes == null) {
return null;
}
if (null == charset) {
return new String(bytes);
}
return new String(bytes, charset);
}
}