Base64 encoding and decoding

Source: Internet
Author: User
Tags uppercase letter
This test today provides the base64 encoding rules. You need to write a program to implement base64 decoding. (For strings that contain Chinese or other multi-byte characters, pay attention to their encoding. For example, for Chinese characters, use utf8 instead of gb2312, which is described at the end ).
Base64 encoding rules:
In the field of cryptography, many encryption ideas are very creative, and base64 is one of them. Although the rules are simple, they are worth your consideration. Many materials can be found online for details and Applications of base64 encoding.
Base64 encoding is used to interpret the binary string corresponding to the original text. We know that a byte is 8 bits, and a binary string is interpreted as a group of 6 bits, that is, base64 encoding. Since the six bits have a total of 2 ^ 6 = 64 different binary strings, the 64 different binary strings are mapped to 64 ASCII characters. In this way, base64 encoding converts any binary input stream into an ASCII string. The 64 ASCII characters to be mapped are: 'A'-'Z', 'a'-'Z', '0'-'9', '+ ', '/'. Assume that the data before encoding is X bytes, And the ASCII string after encoding is y bytes. It is obvious that 3 * x = 4 * Y.
For example, we want to encode uppercase letters 'A' in base64. uppercase letters A are 0x41, and the binary format is 01000001, which are interpreted in a group of 6 bits, the first six bits are 010000, and there are still two B01, not six, so add four zeros to it and make six. Therefore, the value of 010000 000 is 16, in the ing table, 'A' is 0, and 'B' is 1 ......, 16th IS 'Q', so 010000 010000 is mapped to an ASCII string, which is QQ. Because a is a byte, only two bytes are needed to make up three. Therefore, you need to fill in two equal byte values ', in this way, the base64 encoding of uppercase letter A is QQ =. Similarly, if we encode "AB", the result is: qui =.

An online test site for base64 encoding and decoding is:
Http://www.21softs.com/b64endecode.htm


Here are some of the problems I encountered when testing my program:
I thought about how if there are Chinese characters in the program, so I used "test" to encode these two Chinese characters on the website. The result is: 5rWL6K + V, I used my program to decode the result. The decoded result is not the "test" Chinese character.
However, I use my own base64 encoding program to encode any string and then use my own decoding program to decode the encoded result, the decoded result is exactly the same as that before encoding! This indicates that the program logic is correct. The reason may be that the character encoding is different.
"Test" two Chinese characters are encoded in my program. The result is: suLK1A =, one Chinese character is two bytes. Therefore, "test" is four bytes. After encoding, there should be two characters = filling, logical. However, on the online test website, the base64 encoding of the "test" is: 5rWL6K + V, without any padding characters. This indicates that the word "test" on the online test website is not four bytes, according to the Formula 3 * x = 4 * y, it should be exactly 6 bytes, one Chinese character three bytes. This reminds me of UTF-8 encoding.
Therefore, convert the word "test" from GB2312 to UTF8. The binary values of "test" under UTF8 are 0xe6, 0xb5, 0x8b, 0xe8, 0xaf, 0x95, use your own program to encode these six bytes in base64 format. The result is: 5rWL6K + V.
 

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.