About base64 encoding

Source: Internet
Author: User

Many friends may use the base64 encoding format, but the encoding is not equivalent to encryption (encoding is not encrypting). Encoding only formats the data, and encryption makes the data confidential.
When you select the encoding method, base64 encoding can convert data to common text format, such as X.509 standard format certificate (. CER) to export data in base64 encoding format, as shown below:
---- Begin certificate -----
Miidzjccas + gawibagiqdytp7qrsgfv0dwqdkef/+ zanbgkqhkig9w0baqifadbf
......
Od7lqj6u2hmfrq =
----- End certificate -----
The content between ---- begin certificate ----- and ----- end certificate ----- Is the certificate data. Such data is in base64 format.
As far as I know about the selection of encoding APIs, Sun provides a class library for base64 encoding and decoding. The package name is Sun. in addition, Apache provides an API for base64 encoding and decoding.
In the past, I used the APIS provided by Sun. The premise is that the running application runs under sun's JRE. If the JRE changes, for example, it is replaced by IBM's JRE, this API cannot be used.
Therefore, we recommend that you use Apache APIs, which are included in the Jakarta Commons project.

The classes in the sun API are base64encoder and base64decoder. For usage instructions, see the API provided by Sun (Google can help you). They inherit characterencoder and characterdecoder:

The API class provided by Apache is base64:

The following code respectively demonstrates the base64 encoding and decoding of strings (including English, Chinese, and Japanese:

<! --
/* Font Definitions */
@ Font-face
{Font-family: "Ms Ming Dynasty ";
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 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 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 Ming Dynasty ";
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,
I am a Chinese
,
Zookeeper has been released.
";



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 );


}

}

The program running result is as follows:

Sgvsbg8gv29ybgqs5oir5piv5lit5zu95lq6loaxpe1_s6uuobr + obsoobjoobp + obmq =

Sgvsbg8gv29ybgqs5oir5piv5lit5zu95lq6loaxpe1_s6uuobr + obsoobjoobp + obmq =

Hello
World,
I am a Chinese
,
Zookeeper has been released.

Hello
World,
I am a Chinese
,
Zookeeper has been released.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.