using System; namespace SClassLibrary.SEncoding {/// /// related operations related to the base64 encoding algorithm OF ///: Yin Shuguang /// summary> public class SBase64 {public SBase64 () {// / / TODO: Add constructor logic here //} // ----------------------------------- ------------------------------------------- /// /// Use Base64 algorithm to encrypt /// summary> /// String param> /// System Name = "ENS"> System .Text.Encoding object, such as creating Chinese codes set objects: system.text.encoding.getencoding (54936) param> /// After encoding text string returns> public static string encodingforstring (String SourceString) System.Text.Encoding ENS) {Return Convert.TOBASE64STRING (SourceString));} /// /// Use Base64 algorithm encryption /// summary> /// < param name = "sourceString"> be encrypted string param> /// coded text string after returns> public static string EncodingForString (string sourceString) {return EncodingForString (sourceString, System.Text. Encoding.getencoding (54936));} /// /// from the Base64 encoded string, support Chinese /// summary> /// Base64 encrypted string param> /// system.text.Encoding object, such as creating Chinese encoding Set object: system.text.Encoding.Getencoding (54936) param> /// Restored text strings returns> public static string decodingforstring (String Base64String, System.Text.Encoding ENS) {/ ** * ***************************************************************** ************ * * The byte value obtained from the base64String is the machine code (ANSI character encoding) * General, the internal code converted to Chinese characters: * (charr ) (Binary value of the first byte * 256
The second byte value) * CHAR or STRING in C # is not compatible with the formula of * ANSI and Unicode encoding in accordance with the formula of the above formula. NET. NET class library. Class implementation from ANSI encoding to Unicode code * * GetEncoding method relies on the foundation platform to support most code pages. However, the system support is provided for the following case: the default code, that is, the encoding specified in the area setting of the computer that performs this method; Little-Endian Unicode (UTF-16LE); BIG-Endian Unicode (UTF-16BE); Windows operating system (Windows-1252); UTF-7; UTF-8; ASCII and GB18030 (Simplified Chinese). * * Specifies one of the names listed in the following table to obtain encoding supported by the system supported by the corresponding code page. * * Code page name * 1200 "UTF-16LE", "UTF-16", "UCS-2", "Unicode" or "ISO-10646-UCS-2" * 1201 "UTF-16BE" or "Unicodeffe" * 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 US version of Windows 98 may not support Japanese Shift-JIS code page (code page 932).
In this case, the GetEncoding method will trigger notsupportedException: * * Encoding enc = encoding.getencoding ("shift-jis"); * * * * * * * * * * ************* *************************************************************************** / / / Get the original character returnif (Base64String)) from Base64String;} /// /// From the Base64 encoded string, it supports Chinese /// summary> /// base64-encrypted string param> /// text string after reduction returns> public static string DecodingForString (string base64String) {return DecodingForString (base64String System.text.Encoding.Getencoding (54936));} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- - /// /// Take Base64 encoding //// summary> /// file path and file name param> /////// string of the file base64 encoding returns> public static string EncodingForFile (string fileName) {System.IO.FileStream fs = System.IO.File.OpenRead (fileName); System.IO.BinaryReader br = New system.io.binaryReade R (fs); /*system.byte [] b = new system.byte [fs.length]; fs.read (b, 0, convert.toint32 (fs.length)); * / string base64string = convert.tobase64string (Br.Readbytes ((int) fs.lend "; br.close (); fs.close (); return base64string;} /// // / / / saver is saved as file / // summary> /// String param> /// saved file and file name param> /// Save if the file is successful returns>