关于Base64编码

很多朋友可能都会使用到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:

关于Base64编码_第1张图片

Apache提供的API的类为Base64:

关于Base64编码_第2张图片

下面的代码分别展示了将字符串(包含英文,中文,日文)进行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, 我是中国人 , それは本です。

你可能感兴趣的:(apache,String,api,Microsoft,sun,encoding)