转自: 中国开源社区 最快的 Base64 编码/解码 算法
需要的jar包 commons-lang-2.5.jar
import java.util.Arrays; import java.io.UnsupportedEncodingException; import org.apache.commons.lang.time.StopWatch; public class Base64 { private static final char[] CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" .toCharArray(); private static final int[] INV = new int[256]; static { Arrays.fill(INV, -1); for (int i = 0, iS = CHARS.length; i < iS; i++) { INV[CHARS[i]] = i; } INV['='] = 0; } /** * * Returns Base64 characters, a clone of used array. */ public static char[] getAlphabet() { return CHARS.clone(); } // ---------------------------------------------------------------- char[] public static char[] encodeToChar(String s) { try { return encodeToChar(s.getBytes("UTF-8"), false); } catch (UnsupportedEncodingException ignore) { return null; } } public static char[] encodeToChar(byte[] arr) { return encodeToChar(arr, false); } /** * * Encodes a raw byte array into a BASE64 <code>char[]</code>. */ public static char[] encodeToChar(byte[] arr, boolean lineSeparator) { int len = arr != null ? arr.length : 0; if (len == 0) { return new char[0]; } int evenlen = (len / 3) * 3; int cnt = ((len - 1) / 3 + 1) << 2; int destLen = cnt + (lineSeparator ? (cnt - 1) / 76 << 1 : 0); char[] dest = new char[destLen]; 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++] = CHARS[(i >>> 18) & 0x3f]; dest[d++] = CHARS[(i >>> 12) & 0x3f]; dest[d++] = CHARS[(i >>> 6) & 0x3f]; dest[d++] = CHARS[i & 0x3f]; if (lineSeparator && (++cc == 19) && (d < (destLen - 2))) { dest[d++] = '\r'; dest[d++] = '\n'; cc = 0; } } int left = len - evenlen; // 0 - 2. if (left > 0) { int i = ((arr[evenlen] & 0xff) << 10) | (left == 2 ? ((arr[len - 1] & 0xff) << 2) : 0); dest[destLen - 4] = CHARS[i >> 12]; dest[destLen - 3] = CHARS[(i >>> 6) & 0x3f]; dest[destLen - 2] = left == 2 ? CHARS[i & 0x3f] : '='; dest[destLen - 1] = '='; } return dest; } /** * * Decodes a BASE64 encoded char array. */ public byte[] decode(char[] 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]; int d = 0; for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6 | INV[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 |= INV[arr[sndx++]] << (18 - j * 6); } for (int r = 16; d < len; r -= 8) { dest[d++] = (byte) (i >> r); } } return dest; } // ---------------------------------------------------------------- byte public static byte[] encodeToByte(String s) { try { return encodeToByte(s.getBytes("UTF-8"), false); } catch (UnsupportedEncodingException ignore) { return null; } } public static byte[] encodeToByte(byte[] arr) { return encodeToByte(arr, false); } /** * * Encodes a raw byte array into a BASE64 <code>byte[]</code>. */ public static byte[] encodeToByte(byte[] arr, boolean lineSep) { 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 + (lineSep ? (cnt - 1) / 76 << 1 : 0); byte[] dest = new byte[destlen]; 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++] = (byte) CHARS[(i >>> 18) & 0x3f]; dest[d++] = (byte) CHARS[(i >>> 12) & 0x3f]; dest[d++] = (byte) CHARS[(i >>> 6) & 0x3f]; dest[d++] = (byte) CHARS[i & 0x3f]; if (lineSep && ++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] = (byte) CHARS[i >> 12]; dest[destlen - 3] = (byte) CHARS[(i >>> 6) & 0x3f]; dest[destlen - 2] = left == 2 ? (byte) CHARS[i & 0x3f] : (byte) '='; dest[destlen - 1] = '='; } return dest; } /** * * Decodes a BASE64 encoded byte array. */ public static 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]; int d = 0; for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { int i = INV[arr[sndx++]] << 18 | INV[arr[sndx++]] << 12 | INV[arr[sndx++]] << 6 | INV[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 |= INV[arr[sndx++]] << (18 - j * 6); } for (int r = 16; d < len; r -= 8) { dest[d++] = (byte) (i >> r); } } return dest; } // ---------------------------------------------------------------- string public static String encodeToString(String s) { try { return new String(encodeToChar(s.getBytes("UTF-8"), false)); } catch (UnsupportedEncodingException ignore) { return null; } } public static String decodeToString(String s) { try { return new String(decode(s), "UTF-8"); } catch (UnsupportedEncodingException ignore) { return null; } } public static String encodeToString(byte[] arr) { return new String(encodeToChar(arr, false)); } /** * * Encodes a raw byte array into a BASE64 <code>String</code>. */ public static String encodeToString(byte[] arr, boolean lineSep) { return new String(encodeToChar(arr, lineSep)); } /** * * Decodes a BASE64 encoded string. */ public static byte[] decode(String s) { int length = s.length(); if (length == 0) { return new byte[0]; } int sndx = 0, endx = length - 1; int pad = s.charAt(endx) == '=' ? (s.charAt(endx - 1) == '=' ? 2 : 1) : 0; int cnt = endx - sndx + 1; int sepCnt = length > 76 ? (s.charAt(76) == '\r' ? cnt / 78 : 0) << 1 : 0; int len = ((cnt - sepCnt) * 6 >> 3) - pad; byte[] dest = new byte[len]; int d = 0; for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) { int i = INV[s.charAt(sndx++)] << 18 | INV[s.charAt(sndx++)] << 12 | INV[s.charAt(sndx++)] << 6 | INV[s.charAt(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 |= INV[s.charAt(sndx++)] << (18 - j * 6); } for (int r = 16; d < len; r -= 8) { dest[d++] = (byte) (i >> r); } } return dest; } public static void main(String[] args) { StopWatch sw = new StopWatch(); sw.start(); String str = "!@#¥%……&*()——+brown fox jumps 你好,我是谁,Hello,我dogThe quick brown fox jumps over the lazy dog"; for (int i = 0; i < 100000; i++) { //Base64.encodeToByte(str); Base64.encodeToString(str); } //System.out.println(Base64.encodeToString(str)); sw.stop(); System.out.println(sw.getTime() + "毫秒"); } }
相关推荐
2) 修复 BASE64编码解码,个别情况下不会选择AVX2模式的问题(C/C++的bool类型是单字节,易的逻辑型是4字节,易的逻辑型为真时 转为字节集可能是{0,0,1,0},导致单字节判断 时灵时不灵)。1.4更新(2019.6.5)。 1) 添加...
实现BASE64编码和解码程序, 在类中实现如下函数并运行测试正确。 BASE64编码算法请在网上查询。 public String encode(byte[] data) { } public byte[] decode(String b) { }
基于JavaScript的轻量化BASE64编码及解码算法.pdf
用c语言实现的base64,包括加密算法和解密算法。
android 开发中有时会需要用到base64加解密...google在android.util下提供了一个Base64工具类,可以很方便的用它encode和decode,里面提供了一些经典的常用算法。 此程序为改方法完整Java代码,可以直接运行或者调用。
实现base64编解码算法,欢迎大家提出改进意见和建议
base编码解码实现算法及实现源码优化算法应用于网络
这是一个在计算机网络通信中用到的一个Base64编码的一个编码解码算法
包括大小字母转换、时间戳转换、ASCII和BCD互相转换、BCD到无符号长整型数互相转换、基本异或操作、CRC算法、MD5、SHA1、SHA224、SHA256、SHA384、SHA512、DES/3DES、MAC算法、RSA算法、SM算法:SM2/SM3/SM4、中文到...
4.界面上左边两个Base64编解码按钮是对应上面编辑框里的内容进行Base64编解码的; 说明: 1.Base64编码后文件大小将增大为原有大小的4/3倍左右; 2.Huffman编码后压缩比不定,视文件内字符频率而定,对压缩后的文件...
Base64编码解码函数的VB实现。 无窗体,模块文件
Base58编码解码C语言实现。 Base58是区块链中使用的一种编码方法。 文件列表: base58.h, base58.cpp, test.cpp。 包含测试工程: VC++6.0, VC++2008, VC++2013, CentOS7-x64+gcc-4.8.5.20150623+cmake。 提供此源码...
2) 修复 BASE64编码解码,个别情况下不会选择AVX2模式的问题(C/C++的bool类型是单字节,易的逻辑型是4字节,易的逻辑型为真时 转为字节集可能是{0,0,1,0},导致单字节判断 时灵时不灵)。 1.4更新(2019.6.5) 1) 添加 ...
从标准输入中读取已经进行base64编码的内容,解码输出。 格式:base64 -d -i 从标准输入中读取已经进行base64编码的内容,解码输出。加上-i参数,忽略非字母表字符,比如换行符。 man base64 写道 -i, --...
软件包内部有一个有效的base62算法的c ++版本。 货源在包装内。 请注意,从2.0.0版开始,该算法与之前使用的算法不同。 如果您使用base62 <2> = 2.0.0对其进行解码。 2.0.1版本有一个新的咀嚼算法,我们有一个...
如果您的处理器支持 AVX2、SSSE3、SSE4.1、SSE4.2 或 AVX 指令,该库将选择一个优化的编解码器,让它一次编码/解码 12 或 24 个字节,相比之下,速度提高了四倍或更多到“普通”字节编解码器。 NEON 支持在编译时...
3des加密解密 base64编码解码的源代码 完全与标准算法一致,与C# java 结果一致
MD5Class: 完整的MD5码生成类和Base64编码解码函数. Tmqlgn.cfg: 保存QQ登录命令行(经过加密处理) 声明: 程序中的MD5算法和BASE64编码算法均来自互联网,经适当的改编封装而成,未经原作者许可, 请勿用于商业目的. ...
JAVA Base64算法 对字符进行64位编码,解码