The principle and use of Base64 transcoding and decoding (encryption and decryption) in Java

Source: Internet
Author: User
Tags base64

Base64 is one of the most common encoding methods for transmitting 8Bit bytes of code on the network , and you can view rfc2045~rfc2049, which has a detailed specification of MIME. BASE64 encoding can be used to pass longer identity information in an HTTP environment. For example, in the Java Persistence System hibernate, Base64 is used to encode a long unique identifier (typically 128-bit uuid) as a string that is used as a parameter in an HTTP form and an HTTP GET URL. In other applications, it is often necessary to encode binary data as appropriate in the form of URLs (including hidden form fields). At this time , the use of BASE64 encoding is not readable, that is, the encoded data will not be directly visible to the naked eye, it is a cryptographic role.

Base64 uses the a--z,a--z,0--9,+,/64 characters , the encoding principle is to convert 3 bytes to 4 bytes ((3 x 8) = 4 = (6 x 3)) read the first bytes, each read a byte, left 8 bits, and then shifted right four times, Each time 6 bits, so there are 4 bytes, so there is no guarantee that the resulting characters are visible characters, in order to achieve this goal, BASE64 developed a code table for uniform conversion. The size of the Code table is 2^6=64, which is also the origin of the Base64 name. When the remaining number of characters is less than 3 bytes, you should use 0 to populate, corresponding, the output character uses the ' = ' placeholder, so the encoded output after the end of the text may appear 1 to 2 ' = '. The decoding principle is to convert 4 bytes to 3 bytes. First read 4 6 bits (with or operations), move the left 6 bits at a time, and then move right 3 times, 8 bits at a time, so that it is restored.

BASE64 Coding Table

Value Encoding value Encoding value Encoding value Encoding
0 A + R i-Z
1 B S and J 52 0
2 C T-K 53 1
3 D U PNS L 54 2
4 E V M 55 3
5 F N 56 4
6 G X 57 5
7 H x P 58 6
8 I Z-Q 59 7
9 J 60 8
Ten K B (s) 61 9
L-C-T 62 +
63 M/D
N-E V
-O-F-w (pad) =
P. g x
Q, H, y

Base64 is a very common coding specification that converts binary sequences into human-readable ASCII character sequences that are commonly used when using text protocols such as HTTP and SMTP to transmit binary data . Base64 is not a cryptographic decryption algorithm for the security domain (such algorithms have DES, etc.), although we sometimes hear the use of Base64 to encrypt and decrypt the word, but here the encryption and decryption actually refers to the encoding (encode) and decoding (decode) process, The transformation is very simple and can only prevent the information from being directly identified.

BASE64 codec algorithm are very simple, there are many sources on the Internet, here is not introduced.

So how do you use this code in Java? In fact, the JDK inside the corresponding classes and methods, but the API is not visible, the Internet is said to write the JDK's internal personnel dedicated, Java can be used directly, but do not advocate for everyone to use, you can see this http://wenda.haosou.com/q/ 1378566517069409.

However, there is open source jar package for this, Javabase64-1.2.jar,. Once this jar is introduced, a few lines of code can be encoded and decoded:

Example code:

String type for BASE64 encoding

String encoded = Base64.encode ("Hello, world!");

String type for Base64 decoding

String decoded = base64.decode (encoded);

Specify the character encoding method

String encoded = Base64.encode ("Hello, world!", "UTF-8");

String decoded = Base64.decode (encoded, "UTF-8");

To encode a file:

If the file is small, it can be read directly into memory for encoding in the following ways

byte[] Source = ...; Load your data here

byte[] encoded = base64.encode (source);

Byte[] decoded = base64.decode (encoded);

If the large size is larger, it is recommended to use stream:

Code Sample BASE64 Encoding:

InputStream InputStream = new FileInputStream ("source.jpg");

OutputStream outputstream = new FileOutputStream ("encoded.b64");

Base64.encode (InputStream, OutputStream);

Outputstream.close ();

Inputstream.close ();

Code sample BASE64 Decoding:

InputStream InputStream = new FileInputStream ("encoded.b64");

OutputStream outputstream = new FileOutputStream ("decoded.jpg");

Base64.decode (InputStream, OutputStream);

Outputstream.close ();

Inputstream.close ();

Base64 encoding and decoding, base64 use in Java, Base64 encoding Introduction and principle, base64 encryption and decryption

The principle and use of Base64 transcoding and decoding (encryption and decryption) in Java

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.