Introduction to base64 encoding and its use in Java

Source: Internet
Author: User
Introduction to base64 encoding and its use in Java

Base64 is a frequently used encoding method. It was first generated to send email attachments. Today we have fixed a base64 decoding error. Let's take a look at it.

Principle

Base64 encoding converts three 8-bit bytes (3*8 = 24) to four 6-bit bytes (4*6 = 24), and then adds two zeros before the Six-bit encoding, the format is 8 bits per byte.

For example, the ASCII code of the string "123", "1" is 49, "2" is 50, and "3" is 51, as follows:

00110001 00110010 00110011

Divide the preceding 24 bytes into four groups, with each group of 6 bytes, for example:

00110001 00110010 00110011

Then add two zeros to each group, as shown below:

000011000001 0011000010 0000110011

In decimal format:

Then we can find the corresponding encoding according to the following base64 encoding table:

Table 1: the Base 64 alphabet value encoding 0 A 17 R 34 I 51 Z 1 B 18 S 35 J 52 0 2 C 19 t 36 K 53 1 3 D 20 u 37 L 54 2 4 E 21 V 38 m 55 3 5 F 22 W 39 n 56 4 6g 23x40 o 57 5 7 H 24 y 41 P 58 6 8 I 25 Z 42 Q 59 7 9 J 26 A 43 R 60 8 10 k 27 B 44 s 61 9 11 l 28 C 45 t 62 + 12 m 29 D 46 U 63/13 N 30 E 47 v 14 O 31 F 48 W (PAD) = 15 p 32G 49x16 Q 33 H 50 y

 

Therefore, after base64, It is mtiz.

If the original character is not an integer multiple of 3, 0 is used to fill in the corresponding position for less than 6 digits, and then two zeros are added before the six digits, for example, encode the string "1" as follows:

00110001

A group of six digits. The remaining parts must be zero, as shown in the following code:

001100010000

Then add two zeros to each group, as shown below:

0000110000010000

In decimal format:

12, 16

Corresponding to m and Q respectively. The remaining two digits are null and replaced by =. The encoding result is:

MQ =

Therefore, in many cases, after base64 encoding, it may end with =.

Note: According to rfc822, a carriage return line must be added for every 76 characters.

The principle of base64 decoding is the opposite. I will not repeat it again.

Implementation in Java

Java already has the encapsulated base64 implementation, which is very simple.CodeRight.

1. Encoding

  1. /**
  2. * Description: Encoding
  3. * @ Version1.0 Jan 2, 2009 11:48:48 by Cui hongbao
  4. * @ Param pbytes
  5. * @ Return
  6. */
  7. Public StaticString base64encode (Byte[] Pbytes ){
  8. Base64encoder base64 =NewBase64encoder ();
  9. ReturnBase64.encode (pbytes );
  10. }
  11. /**
  12. * Description: Encoding
  13. * @ Version1.0 Jan 2, 2009 11:49:02 by Cui hongbao
  14. * @ Param Str
  15. * @ Return
  16. */
  17. Public StaticString base64encode (string Str ){
  18. Try{
  19. ReturnBase64encode (Str. getbytes ("UTF-8"));
  20. }Catch(Unsupportedencodingexception e ){
  21. E. printstacktrace ();
  22. }
  23. Return Null;
  24. }

2. Decoding

  1. /**
  2. * Description: Decode
  3. * @ Version1.0 Jan 2, 2009 11:49:08 by Cui hongbao
  4. * @ Param Pcontent
  5. * @ Return
  6. * @ Throws ioexception
  7. */
  8. Public Static Byte[] Base64decode (string Pcontent)ThrowsIoexception {
  9. Base64decoder base64 =NewBase64decoder ();
  10. ReturnBase64.decodebuffer (Pcontent );
  11. }

References

Http://www.faqs.org/rfcs/rfc3548.html

Http://blog.csdn.net/zzyy00/archive/2006/09/06/1186959.aspx

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.