/** // <Summary>
/// Use base64 to encrypt the string
/// </Summary>
/// <Param name = "sourcestring"> string to be encrypted </param>
/// <Param name = "ens"> system. Text. Encoding object, for example, create a Chinese sequence set object: system. Text. encoding. getencoding (54936) </param>
/// <Returns> encoded text string </returns>
Public static string encodingforstring (string sourcestring, system. Text. Encoding ENS)
{
Return convert. tobase64string (ENS. getbytes (sourcestring ));
}
/** // <Summary>
/// Use base64 to encrypt the string
/// </Summary>
/// <Param name = "sourcestring"> string to be encrypted </param>
/// <Returns> encoded text string </returns>
Public static string encodingforstring (string sourcestring)
{
Return encodingforstring (sourcestring, system. Text. encoding. getencoding (65001 ));
}
/** // <Summary>
/// Restore the string from a base64 encoded string. Chinese characters are supported.
/// </Summary>
/// <Param name = "base64string"> base64-encrypted string </param>
/// <Param name = "ens"> system. Text. Encoding object, for example, create a Chinese sequence set object: system. Text. encoding. getencoding (54936) </param>
/// <Returns> restored text string </returns>
Public static string decodingforstring (string base64string, system. Text. Encoding ENS)
{
/**//**
**************************************** ********************
*
* The Bytes obtained from base64string are characters in the internal machine code (ANSI character encoding)
* Generally, the formula for converting an inner code to a Chinese character is:
* (Char) (first byte binary value * 256 + second byte value)
* Because the char or string in C # adopts unicode encoding, it cannot be calculated according to the above formula.
* ANSI Byte encoding is incompatible with Unicode encoding.
* Therefore, use the encoding class provided by the. NET class library to convert ANSI code to Unicode code.
*
* The getencoding method relies on the basic platform to support most code pages. However, system support is provided for: default encoding, that is, the encoding specified in the locale settings of the computer on which this method is executed; little-Endian Unicode (UTF-16LE ); big-Endian Unicode (UTF-16BE); Windows operating system (Windows-1252); UTF-7; UTF-8; ASCII and gb18030 (Simplified Chinese ).
*
* Specify a name listed in the following table to obtain the encoding supported by the system with the corresponding code page.
*
* Code Page name
* 1200 UTF-16LE, UTF-16, ucs-2, Unicode, or ISO-10646-UCS-2"
* 1201 "UTF-16BE" or "unicodefffe"
* 1252 "Windows-1252"
* 65000 utf-7, csunicode11utf7, unicode-1-1-utf-7, unicode-2-0-utf-7, x-unicode-1-1-utf-7 or x-unicode-2-0-utf-7"
* 65001 "UTF-8", "unicode-1-1-utf-8", "unicode-2-0-utf-8", "x-unicode-1-1-utf-8" or "x-unicode-2-0-utf-8"
* 20127 "US-ASCII", "us", "ASCII", "ANSI_X3.4-1968", "ANSI_X3.4-1986", "cp367", "csascii", "ibm367", "iso-ir-6 ", "ISO646-US" or "iso_646.irv: 1991"
* 54936 "gb18030"
*
* Some platforms may not support specific code pages. For example, the Japanese shift-JIS code page (code page 932) may not be supported in Windows 98 in the US ). In this case, the getencoding method will trigger notsupportedexception when executing the following C # code:
*
* Encoding ENC = encoding. getencoding ("shift-JIS ");
*
**************************************** ***********************/
// Obtain the original character from base64string
Return ens. getstring (convert. frombase64string (base64string ));
}
/** // <Summary>
/// Restore the string from a base64 encoded string. Chinese characters are supported.
/// </Summary>
/// <Param name = "base64string"> base64-encrypted string </param>
/// <Returns> restored text string </returns>
Public static string decodingforstring (string base64string)
{
Return decodingforstring (base64string, system. Text. encoding. getencoding (65001 ));
}
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