`
- 浏览:
51264 次
- 性别:
- 来自:
北京
-
public class Base64 {
private static final char[] S_BASE64CHAR = {
'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', '+', '/'
};
private static final char S_BASE64PAD = '=';
private static final byte[] S_DECODETABLE = new byte[128];
static {
for (int i = 0; i < S_DECODETABLE.length; i ++)
S_DECODETABLE[i] = Byte.MAX_VALUE; // 127
for (int i = 0; i < S_BASE64CHAR.length; i ++) // 0 to 63
S_DECODETABLE[S_BASE64CHAR[i]] = (byte)i;
}
private static int decode0(char[] ibuf, byte[] obuf, int wp) {
int outlen = 3;
if (ibuf[3] == S_BASE64PAD) outlen = 2;
if (ibuf[2] == S_BASE64PAD) outlen = 1;
int b0 = S_DECODETABLE[ibuf[0]];
int b1 = S_DECODETABLE[ibuf[1]];
int b2 = S_DECODETABLE[ibuf[2]];
int b3 = S_DECODETABLE[ibuf[3]];
switch (outlen) {
case 1:
obuf[wp] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
return 1;
case 2:
obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
obuf[wp] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
return 2;
case 3:
obuf[wp++] = (byte)(b0 << 2 & 0xfc | b1 >> 4 & 0x3);
obuf[wp++] = (byte)(b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
obuf[wp] = (byte)(b2 << 6 & 0xc0 | b3 & 0x3f);
return 3;
default:
throw new RuntimeException("Internal Errror");
}
}
/** *//**
* Decode the base64 data.
* @param data The base64 encoded data to be decoded
* @param off The offset within the encoded data at which to start decoding
* @param len The length of data to decode
* @return The decoded data
*/
public static byte[] decode(char[] data, int off, int len) {
char[] ibuf = new char[4];
int ibufcount = 0;
byte[] obuf = new byte[len/4*3+3];
int obufcount = 0;
for (int i = off; i < off+len; i ++) {
char ch = data[i];
if (ch == S_BASE64PAD
|| ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
ibuf[ibufcount++] = ch;
if (ibufcount == ibuf.length) {
ibufcount = 0;
obufcount += decode0(ibuf, obuf, obufcount);
}
}
}
if (obufcount == obuf.length)
return obuf;
byte[] ret = new byte[obufcount];
System.arraycopy(obuf, 0, ret, 0, obufcount);
return ret;
}
/** *//**
* Decode the base64 data.
* @param data The base64 encoded data to be decoded
* @return The decoded data
*/
public static byte[] decode(String data) {
char[] ibuf = new char[4];
int ibufcount = 0;
byte[] obuf = new byte[data.length()/4*3+3];
int obufcount = 0;
for (int i = 0; i < data.length(); i ++) {
char ch = data.charAt(i);
if (ch == S_BASE64PAD
|| ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
ibuf[ibufcount++] = ch;
if (ibufcount == ibuf.length) {
ibufcount = 0;
obufcount += decode0(ibuf, obuf, obufcount);
}
}
}
if (obufcount == obuf.length)
return obuf;
byte[] ret = new byte[obufcount];
System.arraycopy(obuf, 0, ret, 0, obufcount);
return ret;
}
/** *//**
* Decode the base64 data.
* @param
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
JAVA Base64算法 对字符进行64位编码,解码
Base64算法完整源码与调用方法,Base64算法完整源码与调用方法,
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,大家可以查看RFC2045~RFC2049,上面有MIME的详细规范。Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,...
c++实现base64算法加密解密,不需要修改,直接运行即可
Base64不是什么新奇的算法了,不过如果你没从事过页面开发(或者说动态页面开发,尤其是邮箱服务),你都不怎么了解过,只是听起来很熟悉。 对于黑客来说,Base64与MD5算法有着同样的位置,因为电子邮箱(e-mail)...
Base64算法完整源码与调用方法 Complete source code and a method is called Base64 algorithm
C#编写的base64算法包含utf-8和普通编码的base64算法应用程序可以查看网页代码,可以下载一切网页前台代码
NULL 博文链接:https://314649444.iteye.com/blog/1889750
base64算法实现,有使用sun.misc.BASE64Decoder工具类的,也有自己写的算法实现
BASE64算法基于C++控制台实现
BASE64算法基于C++MFC实现
Base64算法完整源码与调用方法
C++Review 实现与数据库连接并用到Base64算法和随机数算法
BASE64算法基于C++之MAC与Linux平台实现
Base64算法实现(Delphi源码).doc
Base64算法完整源码与调用方法...................................
java加密解密研究3、Base64算法