加密与解密篇1之Base64
Base64是一种基于64个可打印字符来表示二进制数据的表示方法。由于2的6次方等于64,所以每6个位元为一个单元,对应某个可打印字符。三个字节有24个位元,对应于4个Base64单元,即3个字节需要用4个可打印字符来表示。它可用来作为电子邮件的传输编码。在Base64中的可打印字符包括字母A-Z、a-z、数字0-9 ,这样共有62个字符,此外两个可打印符号在不同的系统中而不同。一些如uuencode的其他编码方法,和之后binhex的版本使用不同的64字符集来代表6个二进制数字,但是它们不叫Base64。
【摘自维基百科】
为了保证所输出的编码位可读字符,Base64制定了一个编码表,以便进行统一转换。编码表的大小为2^6=64,这也是Base64名称的由来。
Base64编码表
码值 |
字符 |
|
码值 |
字符 |
|
码值 |
字符 |
|
码值 |
字符 |
0 |
A |
16 |
Q |
32 |
g |
48 |
w |
|||
1 |
B |
17 |
R |
33 |
h |
49 |
x |
|||
2 |
C |
18 |
S |
34 |
i |
50 |
y |
|||
3 |
D |
19 |
T |
35 |
j |
51 |
z |
|||
4 |
E |
20 |
U |
36 |
k |
52 |
0 |
|||
5 |
F |
21 |
V |
37 |
l |
53 |
1 |
|||
6 |
G |
22 |
W |
38 |
m |
54 |
2 |
|||
7 |
H |
23 |
X |
39 |
n |
55 |
3 |
|||
8 |
I |
24 |
Y |
40 |
o |
56 |
4 |
|||
9 |
J |
25 |
Z |
41 |
p |
57 |
5 |
|||
10 |
K |
26 |
a |
42 |
q |
58 |
6 |
|||
11 |
L |
27 |
b |
43 |
r |
59 |
7 |
|||
12 |
M |
28 |
c |
44 |
s |
60 |
8 |
|||
13 |
N |
29 |
d |
45 |
t |
61 |
9 |
|||
14 |
O |
30 |
e |
46 |
u |
62 |
+ |
|||
15 |
P |
31 |
f |
47 |
v |
63 |
/ |
Java代码中Base64的使用示例:
package cn.com.base64; import java.io.IOException; public class Test { /** * 编码 * @param bstr * @return String */ public static String encode(byte[] bstr){ return new sun.misc.BASE64Encoder().encode(bstr); } /** * 解码 * @param str * @return string */ public static byte[] decode(String str){ byte[] bt = null; try { sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder(); bt = decoder.decodeBuffer( str ); } catch (IOException e) { e.printStackTrace(); } return bt; } /** * @param args */ public static void main(String[] args) { /* Test te = new Test(); String aa = "hello world"; System.out.println("加密前:"+aa); aa = te.encode(aa.getBytes()); System.out.println("经过BASE64加密后:"+aa); String str = aa; String str2 = new String(te.decode(str)); System.out.println("经过BASE64解密后:"+str2); */ } }
说明:第一次在Eclipse中引入BASE64Encoder /BASE64Decoder类,无法引入,解决办法请参考:http://blog.****.net/a0501bqzhxy/article/details/6441526
下面是摘自高人的Base64源码:
package cn.com.base; public class Base64 { private static final String base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "+/"; private static final int splitLinesAt = 76; public static byte[] zeroPad(int length, byte[] bytes) { byte[] padded = new byte[length]; // initialized to zero by JVM System.arraycopy(bytes, 0, padded, 0, bytes.length); return padded; } public static String encode(String string) { String encoded = ""; byte[] stringArray; try { stringArray = string.getBytes("UTF-8"); // use appropriate encoding string! } catch (Exception ignored) { stringArray = string.getBytes(); // use locale default rather than croak } // determine how many padding bytes to add to the output int paddingCount = (3 - (stringArray.length % 3)) % 3; // add any necessary padding to the input stringArray = zeroPad(stringArray.length + paddingCount, stringArray); // process 3 bytes at a time, churning out 4 output bytes // worry about CRLF insertions later for (int i = 0; i < stringArray.length; i += 3) { int j = ((stringArray[i] & 0xff) << 16) + ((stringArray[i + 1] & 0xff) << 8) + (stringArray[i + 2] & 0xff); encoded = encoded + base64code.charAt((j >> 18) & 0x3f) + base64code.charAt((j >> 12) & 0x3f) + base64code.charAt((j >> 6) & 0x3f) + base64code.charAt(j & 0x3f); } // replace encoded padding nulls with "=" return splitLines(encoded.substring(0, encoded.length() - paddingCount) + "==".substring(0, paddingCount)); } public static String splitLines(String string) { String lines = ""; for (int i = 0; i < string.length(); i += splitLinesAt) { lines += string.substring(i, Math.min(string.length(), i + splitLinesAt)); lines += "\r\n"; } return lines; } public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.err.println("encoding \"" + args[i] + "\""); System.out.println(encode(args[i])); } } }
其中核心算法一系列的左移与右移运算,实现是理解不了!这个必须得有高人指点。
参考资料:
http://snowolf.iteye.com/blog/379860
http://my.eoe.cn/indexer/archive/1055.html
http://www.iteye.com/topic/605714
http://java.chinaitlab.com/Special/javajm/Index.html
http://www.wikihow.com/Encode-a-String-to-Base64-With-Java