base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,要求把每三个8Bit的字节转换为四个6Bit的字节。
BASE64和其他相似的编码算法通常用于转换二进制数据为文本数据,其目的是为了简化存储或传输。更具体地说,BASE64算法主要用于转换二进 制数据为ASCII字符串格式。Java语言提供了一个非常好的BASE64算法的实现,即Apache Commons Codec工具包。本文将简要地讲述怎样使用BASE64以及它是怎样工作的。
下面我们用BASE64对字符串进行编码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import
java.io.UnsupportedEncodingException;
import
org.apache.commons.codec.binary.Base64;
public
class
Base64Test {
public
static
void
main(String[] args){
String str =
"Hello World"
;
try
{
byte
[] encodeBase64 = Base64.encodeBase64(str.getBytes(
"UTF-8"
));
System.out.println(
"RESULT: "
+
new
String(encodeBase64));
}
catch
(UnsupportedEncodingException e){
e.printStackTrace();
}
}
}
|
输出结果为:
- RESULT: SGVsbG8gV29ybGQ=
上面输出的字符串是“Hello world”字符串的8位二进制值被连接在一起,然后以6位分组。随后每个组都被转换成一个单独的数字并映射到Base64的索引。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
binary dec Base64
010010
18
S
000110
6
G
010101
21
V
101100
44
s
011011
27
b
000110
6
G
111100
60
8
100000
32
g
010101
29
d
110110
54
2
111101
61
9
110010
50
y
011011
27
b
000110
6
G
010000
16
Q
|
注意:字符串最后加上了“=”,其意思表示字符串编码的结束。
简单模拟Base64的加解密过程:
Base64的加密跟解密 ,其实很简单,代码如下:
public class base64Utrl {
/**
* 将原始数据编码为base64编码
*/
static public char[] encode(byte[] data) {
char[] out = new char[((data.length + 2) / 3) * 4];
for (int i = 0, index = 0; i < data.length; i += 3, index += 4) {
boolean quad = false;
boolean trip = false;
int val = (0xFF & (int) data[i]);
val <<= 8;
if ((i + 1) < data.length) {
val |= (0xFF & (int) data[i + 1]);
trip = true;
}
val <<= 8;
if ((i + 2) < data.length) {
val |= (0xFF & (int) data[i + 2]);
quad = true;
}
out[index + 3] = alphabet[(quad ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 2] = alphabet[(trip ? (val & 0x3F) : 64)];
val >>= 6;
out[index + 1] = alphabet[val & 0x3F];
val >>= 6;
out[index + 0] = alphabet[val & 0x3F];
}
return out;
}
/**
* 将base64编码的数据解码成原始数据
*/
static public byte[] decode(char[] data) {
int len = ((data.length + 3) / 4) * 3;
if (data.length > 0 && data[data.length - 1] == '=')
--len;
if (data.length > 1 && data[data.length - 2] == '=')
--len;
byte[] out = new byte[len];
int shift = 0;
int accum = 0;
int index = 0;
for (int ix = 0; ix < data.length; ix++) {
int value = codes[data[ix] & 0xFF];
if (value >= 0) {
accum <<= 6;
shift += 6;
accum |= value;
if (shift >= 8) {
shift -= 8;
out[index++] = (byte) ((accum >> shift) & 0xff);
}
}
}
if (index != out.length)
throw new Error("miscalculated data length!");
return out;
}
static private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();
static private byte[] codes = new byte[256];
static {
for (int i = 0; i < 256; i++)
codes[i] = -1;
for (int i = 'A'; i <= 'Z'; i++)
codes[i] = (byte) (i - 'A');
for (int i = 'a'; i <= 'z'; i++)
codes[i] = (byte) (26 + i - 'a');
for (int i = '0'; i <= '9'; i++)
codes[i] = (byte) (52 + i - '0');
codes['+'] = 62;
codes['/'] = 63;
}
public static void main(String[] args) throws Exception {
// 加密成base64
String strSrc = "http://www.jbsql.com,1";
System.out.println("加密前:"+strSrc);
String strOut = new String(base64Utrl.encode(strSrc.getBytes("GB18030")));
System.out.println("加密后:"+strOut);
String strOut2 = new String(base64Utrl.decode("aHR0cDovL3d3dy5qYnNxbC5jb20sMQ==".toCharArray()), "GB18030");
System.out.println("解密后:"+strOut2);
}
}