[Golang] Base64 encryption and decryption

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.

First explain the following what is Base64 (from Baidu Encyclopedia):

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.

There are base64 libraries in the Golang that can be implemented, and I try to implement the cryptographic decryption function.

At the same time I think they can also be used for the transmission of chat messages.

Func Decode (Raw []byte) []byte {var buf bytes. bufferdecoded: = Make ([]byte, 215) buf. Write (RAW) Decoder: = base64. Newdecoder (base64. Stdencoding, &BUF) decoder. Read (decoded) return Decoded}func Encode (Raw []byte) []byte {var encoded bytes. Bufferencoder: = base64. Newencoder (base64. Stdencoding, &encoded) encoder. Write (RAW) encoder. Close () return encoded. Bytes ()}

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.