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 #