Coding and decoding method of Base64 in C #

Source: Internet
Author: User

Original: Base64 encoding in C #, decoding method

1, base64 to string

String strpath = "Ahr0cdovlziwmy44ms4yos40njo1ntu3l1
9iywlkds9yaw5ncy9tawrplziwmda3mzgwlte2lm1pza== ";
byte[] Bpath = convert.frombase64string (strpath);
strpath = System.Text.ASCIIEncoding.Default.GetString (Bpath);

2. String to Base64
System.Text.Encoding encode = System.Text.Encoding.ASCII;
byte[] Bytedata = encode. GetBytes ("test");
String strpath = Convert.tobase64string (bytedata,0,bytedata. Length);

Base64 Working principle

Base64 is one of the most commonly used encoding methods in MIME messages. Its main idea is to encode the input string or data into only {' A '-' Z ', ' a '-' z ', ' 0 '-' 9 ', ' + ', '/'} these 64 printable characters of the string, so called "Base64".

BASE64 encodes the input data stream by taking 6 bits at a time, using the value of this 6 bit (0-63) as an index to look up the table and output the corresponding characters. In this way, each 3 bytes will be encoded as 4 characters (3x8→4x6), and less than 4 characters are populated with ' = '.





Coding
public string EncodeBase64 (string code_type,string code)
{
string encode = "";
byte[] bytes = encoding.getencoding (code_type). GetBytes (code);
Try
{
encode = convert.tobase64string (bytes);
}
Catch
{
encode = code;
}
return encode;
}
Decoding
public string DecodeBase64 (string code_type,string code)
{
String decode = "";
byte[] bytes = convert.frombase64string (code);
Try
{
decode = encoding.getencoding (Code_type). GetString (bytes);
}
Catch
{
decode = code;
}
return decode;
}

Coding and decoding method of Base64 in C #

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.