前几天无意中看到Java中有Base64编码,不解的我去百科了一下,了解了Base64的基本使用和实现原理,于是在空暇时自己手动写了一个,这个类可以完成对字母数字的编码和解码工作,但是对于中文,还没有仔细研究其编码的实现过程。至于什么是Base64,用它来干什么,请移步到:http://zh.wikipedia.org/zh-cn/Base64
下面贴出这个工具类的源代码,供朋友们参考,本人新手写得不好,请用力拍砖:
/************************Base64Util.java*****************************/
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
package util;
import java.util.HashMap;
import java.util.Map;
/** * This class is a tool class to convert Base64 string and
* normal string to each other. <br><br>
* More information about base64, Please refer to:
* http://zh.wikipedia.org/zh-cn/Base64
* @author user
*/
public class Base64Util {
private static final Map<Integer, Character> base64CharMap = new HashMap<>();
private static final String base64CharString =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
static {
// initialize base64 map
for ( int i= 0 ;i<base64CharString.length();i++){
char c = base64CharString.charAt(i);
base64CharMap.put( new Integer(i), new Character(c));
} } /**
* This method is used to encode a normal string to base64 string * @param origin * The String to be encoded * @return * The String after encoded. */ public static String encode (String origin) {
if (origin == null ) {
return null ;
} if (origin.length() == 0 ) {
return "" ;
} int length = origin.length();
String binaryString = "" ;
// to binary String
for ( int i= 0 ;i<length;i++) {
int ascii = origin.charAt(i);
String binaryCharString = Integer.toBinaryString(ascii); while (binaryCharString.length() < 8 ) {
binaryCharString = "0" + binaryCharString;
} binaryString += binaryCharString; } // to base64 index
int beginIndex = 0 ;
int endIndex = beginIndex + 6 ;
String base64BinaryString = "" ;
String charString = "" ;
while ((base64BinaryString =
binaryString.substring(beginIndex, endIndex)).length() > 0 ) {
//if length is less than 6, add "0".
while (base64BinaryString.length() < 6 ) {
base64BinaryString += "0" ;
} int index = Integer.parseInt(base64BinaryString, 2 );
char base64Char = base64CharMap.get(index);
charString = charString + base64Char; beginIndex += 6 ;
endIndex += 6 ;
if (endIndex >= binaryString.length()) {
endIndex = binaryString.length(); } if (endIndex < beginIndex) {
break ;
} } if (length % 3 == 2 ) {
charString += "=" ;
} if (length % 3 == 1 ) {
charString += "==" ;
} return charString;
} /**
* This method is used to decode from base64 string to a normal string. * @param encodedString * The string to be decoded. * @return * The string after decoded. */ public static String decode(String encodedString) {
if (encodedString == null ) {
return null ;
} if (encodedString.length() == 0 ) {
return "" ;
} //get origin base64 String
String origin = encodedString.substring( 0 ,encodedString.indexOf( "=" ));
String equals = encodedString.substring(encodedString.indexOf( "=" ));
String binaryString = "" ;
//convert base64 string to binary string
for ( int i= 0 ;i<origin.length();i++) {
char c = origin.charAt(i);
int ascii = base64CharString.indexOf(c);
String binaryCharString = Integer.toBinaryString(ascii); while (binaryCharString.length() < 6 ) {
binaryCharString = "0" + binaryCharString;
} binaryString += binaryCharString; } // the encoded string has 1 "=", means that the binary string has append 2 "0"
if (equals.length() == 1 ) {
binaryString = binaryString.substring( 0 ,binaryString.length()- 2 );
} // the encoded string has 2 "=", means that the binary string has append 4 "0"
if (equals.length() == 2 ) {
binaryString = binaryString.substring( 0 ,binaryString.length()- 4 );
} // convert to String
String charString = "" ;
String resultString = "" ;
int beginIndex = 0 ;
int endIndex = beginIndex + 8 ;
while ((charString =
binaryString.substring(beginIndex, endIndex)).length() == 8 ) {
int ascii = Integer.parseInt(charString, 2 );
resultString += ( char )ascii;
beginIndex += 8 ;
endIndex += 8 ;
if (endIndex > binaryString.length()) {
break ;
} } return resultString;
} // Test methods here.
public static void main(String[] args) {
String originString = "Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure." ;
String base64String = "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz" +
"IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg" +
"dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu" +
"dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo" +
"ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=" ;
System.out.println( "===============encode=================" );
String result = encode(originString); System.out.println(result); System.out.println(result.equals(base64String)); System.out.println( "===============decode=================" );
String decodedString = decode(base64String); System.out.println(decodedString); System.out.println(originString.equals(decodedString)); System.out.println(decode(encode( "abcdefg" )));
} } |