<summary>
Related operations on Base64 coding algorithms
by Free Pentium (WGSCD)
</summary>
public class SBase64
{
Public SBase64 ()
{
//
TODO: Add constructor logic here
//
}
//--------------------------------------------------------------------------------
<summary>
Encrypt a string using the Base64 algorithm
</summary>
<param name= "sourcestring" > Strings to be encrypted </param>
<param name= "Ens" >system.text.encoding object, such as creating a Chinese encoding Set object: System.Text.Encoding.GetEncoding (54936) </param >
<returns> text string after the overweight </returns>
public static string Encodingforstring (String sourcestring,system.text.encoding ens)
{
Return convert.tobase64string (Ens. GetBytes (sourcestring));
}
<summary>
Encrypt a string using the Base64 algorithm
</summary>
<param name= "sourcestring" > Strings to be encrypted </param>
<returns> text string after the overweight </returns>
public static string Encodingforstring (String sourcestring)
{
Return encodingforstring (sourcestring,system.text.encoding.getencoding (54936));
}
<summary>
To restore a string from a base64 encoded string, support Chinese
</summary>
<param name= "base64string" >base64 encrypted string </param>
<param name= "Ens" >system.text.encoding object, such as creating a Chinese encoding Set object: System.Text.Encoding.GetEncoding (54936) </param >
<returns> text string after restore </returns>
public static string Decodingforstring (String base64string,system.text.encoding ens)
{
/**
* ***********************************************************
*
* Internal code (ANSI character encoding) of bytes obtained from base64string
* Generally, the conversion of the machine code to Chinese characters is the formula:
* (char) (the first byte of the binary value *256+ the second byte value)
* and char or string in C # because of Unicode encoding, it is not possible to follow the formula above
* ANSI Byte and Unicode encoding incompatible
* Using the encoding class provided by the. NET class library to convert from ANSI to Unicode code
*
* The GetEncoding method relies on most of the code pages supported by the underlying platform. However, system support is provided for the following conditions: The default encoding, the encoding specified in the locale of the computer executing this method, Little-endian Unicode (utf-16le), Big-endian Unicode (UTF-16BE), and Windows Operating system (windows-1252); Utf-7;utf-8;ascii and GB18030 (Simplified Chinese).
*
* Specify one of the names listed in the following table to obtain a system-supported encoding 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 American version of Windows 98 may not support the Japanese Shift-jis code page (code page 932). In this case, the GetEncoding method will raise the NotSupportedException when executing the following C # code:
*
* Encoding enc = encoding.getencoding ("Shift-jis");
*
* **************************************************************/
Get the original character from the Base64string
Return ENS. GetString (convert.frombase64string (base64string));
}
<summary>
To restore a string from a base64 encoded string, support Chinese
</summary>
<param name= "base64string" >base64 encrypted string </param>
<returns> text string after restore </returns>
public static string Decodingforstring (String base64string)
{
Return decodingforstring (base64string,system.text.encoding.getencoding (54936));
}
<summary>
Base64 any type of file
</summary>
<param name= "filename" > File path and filename </param>
<returns> string after base64 encoding of files </returns>
public static string Encodingforfile (String fileName)
{
System.IO.FileStream Fs=system.io.file.openread (fileName);
System.IO.BinaryReader br= New System.IO.BinaryReader (FS);
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.