Golang implementation of BASE64 encryption

Source: Internet
Author: User
Tags key string
This is a creation in Article, where the information may have evolved or changed.

Base64 encryption is an encryption method that we often see, such as the verification process of ESMTP and the Internet transmission of binary files.


Base64 encryption method is a relatively simple one, the encoding process is the original text in accordance with every 6bit (remember, is "bit") for a set of the original to replace, because 6bit can represent the range of values in 0~63 (2 of the 6-square, A total of 64), so you can map the 6bit value with a character descriptor of length 64.


This mapping table is usually A-Z, A-Z, 0-9 Plus + and/two symbols, or "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/".


However, before the encryption, we have to solve a problem, that is, a character is usually a byte (byte), Byte is 8bit, but the time of encryption is in accordance with every 6bit units, then how to encrypt it?


In fact, Base64 encoding is done in a group of three bytes, so that three bytes is 24bit, then 24bit÷6bit = 4 (characters), so that after Base64 encryption, every 3 characters will be encrypted to 4 characters. In other words, if normal, the original text after being Base64 encrypted is 3/4 longer than the original.


Of course, this "normal" situation, then if the length of the original text is not a multiple of 3 this abnormal situation should do?


If the length of the original text is not more than 3 integers, then the "=" number is used to populate it. For example, if the last 1 characters, not enough 3, then at the end of the cipher add two "=", if the last remaining 1 characters, at the end of the cipher add a "=".

Again, is to fill the problem of the bit, such as if the last 1 characters left, then the 1 characters only 8bit, so that the first 6bit encoding after only 2bit, encryption on the way to continue. So to fill the insufficient bits with a zero, here is an example to understand:

Now to encode the "ABCDE" base64, then only the "de" is left after the first three characters are encrypted, and the 2 binary representation of the two characters is

"01100100 01100101", a total of 16 bits, if you want to Base64 encryption, you must refill two 0 for "01100100 01100101".

This will be a 6 integer multiple, can be successfully completed encryption. Don't forget to add a "=" when you're done encrypting it.




Here is the implementation code, if there is an error, please leave a message:

Func EncodeBase64 (Plain string) (cipher string) {Const key string = " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"var (length int = len (plain) Loop_time int = Le ngth/3left int = length% 3bytes_catnate int32index int) for I: = 0; i < loop_time; i++ {bytes_catnate = Int32 (Plain[index]) <<16 + int32 (Plain[index+1]) <<8 + int32 (Plain[index+2]) Index + = 3cipher + = string (key[(bytes_catnate>>18) &0x3f]) cipher + = string (key[(bytes_catnate>>12) &0x3f] ) Cipher + = string (key[(bytes_catnate>>6) &0x3f]) cipher + = string (key[bytes_catnate&0x3f])}if left = = 1 { Bytes_catnate = Int32 (Plain[index]) << 4cipher + = string (key[(bytes_catnate>>6) &0x3f]) cipher + = string (key[bytes_catnate&0x3f]) cipher + = "="}if left = = 2 {bytes_catnate = (int32 (Plain[index]) <<8 + int32 (plain[ INDEX+1])) << 2cipher + = string (key[(bytes_catnate>>12) &0x3f]) cipher + = string (key[(bytes_catnate >>6) &0x3f]) cipher + = string (key[bytes_catnate&0x3f]) cipher + = "="}return cipher} 




if reproduced please specify the source: http://blog.csdn.net/gophers/article/details/22793357

Related Article

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.