很多朋友可能都會使用到BASE64這種編碼格式,但是編碼不等於加密(encoding is not encrypting),編碼只是將資料進行格式化,而加密使資料具有保密性。
在編碼方式的選擇時,BASE64編碼可以將資料轉換為普通文字格式設定,例如X.509標準格式的認證(.cer)就可以匯出為BASE64編碼格式的資料,如下所示:
----BEGIN CERTIFICATE-----
MIIDZjCCAs+gAwIBAgIQDYtP7qrSGFv0dWqdKeF/+zANBgkqhkiG9w0BAQIFADBf
......
oD7lqj6u2HMfrQ==
-----END CERTIFICATE-----
其中在----BEGIN CERTIFICATE-----和-----END CERTIFICATE-----之間的內容就是認證資料。這樣的資料就是BASE64格式的資料。
關於編碼API的選擇,據我所知,Sun提供了進行BASE64編碼和解碼的類庫,package名為sun.misc,另外Apache也提供了可以實現BASE64編碼和解碼的API。
以前筆者是使用Sun提供的API,這個前提是啟動並執行應用程式運行在Sun的JRE下面,如果JRE變了,例如換成了IBM的JRE,那麼這個API就不可以使用了。
所以推薦使用Apache的API,包含在Jakarta Commons項目中。
其中Sun API中包含的類名為BASE64Encoder和BASE64Decoder,使用方法參見Sun提供的API(Google可以協助你),他們繼承CharacterEncoder和CharacterDecoder:
Apache提供的API的類為Base64:
下面的代碼分別展示了將字串(包含英文,中文,日文)進行Base64編碼和解碼的過程:
<!--
/* Font Definitions */
@font-face
{font-family:"MS 明朝";
panose-1:2 2 6 9 4 2 5 8 3 4;
mso-font-alt:"MS Mincho";
mso-font-charset:128;
mso-generic-font-family:roman;
mso-font-pitch:fixed;
mso-font-signature:-1610612033 1757936891 16 0 131231 0;}
@font-face
{font-family:SimSun;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:宋體;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"Microsoft Sans Serif";
panose-1:2 11 6 4 2 2 2 2 2 4;
mso-font-charset:0;
mso-generic-font-family:swiss;
mso-font-pitch:variable;
mso-font-signature:1627421663 -2147483648 8 0 66047 0;}
@font-face
{font-family:"Palatino Linotype";
panose-1:2 4 5 2 5 5 5 3 3 4;
mso-font-charset:0;
mso-generic-font-family:roman;
mso-font-pitch:variable;
mso-font-signature:-536870009 1073741843 0 0 415 0;}
@font-face
{font-family:"/@MS 明朝";
panose-1:2 2 6 9 4 2 5 8 3 4;
mso-font-charset:128;
mso-generic-font-family:roman;
mso-font-pitch:fixed;
mso-font-signature:-1610612033 1757936891 16 0 131231 0;}
@font-face
{font-family:"/@SimSun";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0mm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:SimSun;
mso-font-kerning:1.0pt;
mso-fareast-language:ZH-CN;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
-->
package org.ly;
import java.io.IOException;
import
org.apache.commons.codec.DecoderException;
import
org.apache.commons.codec.EncoderException;
import
org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class Base64Encode {
public
static void main(String[] args) throws EncoderException,
DecoderException, IOException {
String sample =
"Hello World,
我是中國人
,
それは本です。
";
Base64
base64 = new Base64();
byte[]
bytes = base64.encode(sample.getBytes("UTF-8"));
String
encode1 = new String(bytes, "UTF-8");
System.out.println(encode1);
BASE64Encoder
base = new BASE64Encoder();
String
encode2 = base.encode(sample.getBytes("UTF-8"));
System.out.println(encode2);
bytes
= base64.decode(encode1.getBytes("UTF-8"));
String
decode1 = new String(bytes, "UTF-8");
System.out.println(decode1);
byte[]
bytes2 = new BASE64Decoder().decodeBuffer(encode2);
String
decode2 = new String(bytes2, "UTF-8");
System.out.println(decode2);
}
}
程式運行結果如下:
SGVsbG8gV29ybGQs5oiR5piv5Lit5Zu95Lq6LOaXpeacrOS6uuOBr+OBsOOBjOOBp+OBmQ==
SGVsbG8gV29ybGQs5oiR5piv5Lit5Zu95Lq6LOaXpeacrOS6uuOBr+OBsOOBjOOBp+OBmQ==
Hello
World,
我是中國人
,
それは本です。
Hello
World,
我是中國人
,
それは本です。