Briefly describe the function of base64 encoding and C #'s support

Source: Internet
Author: User

Analyze problems

Unlike traditional encoding, base64 encoding is designed to confuse eight-byte data streams. In network transmission and email systems, base64 encoding is widely used. Base64 is not a confidential mechanism, but it does program plaintext into a difficult way to identify.

Note:

  The base64 algorithm is very simple. base64 separates all bits and reassembles them into bytes. The new byte only contains 6 bits, and then adds two zeros before each byte, to form a new byte array. For example, if a byte array contains three bytes, it will be allocated to four new bytes (3*8/6 = 4) During base64 encoding. Each byte is only filled with a minimum of six bytes, finally, set the height to 2 and 0. Describes the base64 algorithm.

   

   

In C #, the convert type is usually used for base64 encoding and decoding. It can convert 8-bit byte arrays and base64 encoded strings. The following code demonstrates its usage.

using System;using System.Text;namespace Test{    class Base64    {        static void Main()        {            string str = "abcde";            //Generate UTF8 byte array            byte[] bytes = Encoding.UTF8.GetBytes(str);            //Converted into Base64 string            string base64 = BytesToBase64(bytes);            Console.WriteLine(base64);            //Back to UTF8 byte array.            bytes = Base64ToBytes(base64);            //Back to string.            Console.WriteLine(Encoding.UTF8.GetString(bytes));            Console.Read();        }        //Converts 8-bit byte array into Base64 string        static string BytesToBase64(byte[] bytes)        {            try            {                //Conversion is not successful will throw an exception                return Convert.ToBase64String(bytes);            }            catch (ArgumentNullException)            {                return null;            }        }        //Converts Base64 string into 8-bit byte array        static byte[] Base64ToBytes(string base64)        {            try            {                //Conversion is not successful will throw an exception                return Convert.FromBase64String(base64);            }            catch (ArgumentNullException)            {                return null;            }            catch (FormatException)            {                return null;            }        }    }    }

As shown in the code above, the convert type provides a very simple interface to support conversion between base64 strings and 8-byte arrays, but note that, if the convert type fails to be converted, an exception is thrown directly. At any time, the reader must put the convert type operation statements in try and catch blocks.

Note:

The convert type is designed to convert content, which is totally different from type conversion. Convert type can also implement the type conversion function, but readers should use the is and as statements to improve efficiency.

The following is the execution result of the above Code.

  

Answer

Base64 encoding is a method for obfuscation of clear codes. The algorithm is to distribute the original 8-byte array to the new 6-Bit Array, then fill in 0 at the height of each byte to form a new 8-byte array. In. net, the convert type can be used to convert base64 strings and 8-byte arrays.

  

 

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.