A very practical base64 class (reproduced)

xiaoxiao2021-03-06  59

Recently, I learned C # network programming, I put some related good texts found on the Internet, attached to here, in order to learn, share.

------------------------------------------

Using system;

namespace SClassLibrary.SEncoding {///

/// For base64 encoding algorithm of related operations ///: Yin Shuguang /// public class SBase64 {public SBase64 () {//// TODO: Add constructor logic here //} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------ /// // / Use Base64 algorithm encryption /// /// String /// System.Text. Encoding object, such as creating Chinese codes set objects: system.text.Encoding.Getencoding (54936) /// Extracted text string Public Static String EncodingForstring (String SourceString, System). Text.Encoding ENS) {Return Convert.TOBASE64String (ENS.GETBYTES (SOURCESTRING);

///

/// Use Base64 algorithm to encrypt /// /// String /// plus code The text string public static string encodingforstring (String SourceString) {Return EncodingForstring (SourceString, System.Text.Encoding.Getencoding (54936));}

///

/// from the Base64 encoded string, support Chinese /// //// Base64 encrypted string /// system.text.Encoding object, such as creating Chinese codes set objects: system.text.encoding.getencoding (54936) /// Restore text characters String public static string decodingforstring (String Base64String, System.Text.Encoding ENS) {/ **************************************** ************************** * * From the Base64String, the byte value is the character's code (ANSI Character Coding) * Generally, the UE Code Converts to Chinese Characters is a Formula: * (CHAR) (Bi-input value * 256 second byte value) * CHAR or STRING in C # due to With Unicode encoding, you cannot calculate * ANSI's byte and Unicode encoding is not compatible with the above formula. If you use the .NET class library to implement the conversion from the ANSI code to Unicode code * * GetEncoding method relies on Basic platforms 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 table below to get encodings supported by systems with corresponding code pages. ** 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 /// /// base64-encrypted string /// text string after reduction public static string DecodingForString (string base64String) {return DecodingForString (base64String System.text.Encoding.Getencoding (54936));}

/ / -------------------------------------------------------------------------------------------- -------------------------------------- ///

/// for any type The file is performed based on the path and file name /// of the file. Base64 encoded string public static string EncodingForFile (string fileName) {System.IO.FileStream fs = System.IO.File.OpenRead (fileName); System.IO.BinaryReader br = new System.IO.BinaryReader (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.length);

Br.close (); fs.close (); return base64string;} ///

// / / /} 编 编 编 编 编 编 编 编 编 编 为 为 文件 为 为Base64String "> String /// Save the file and file name /// Save the file to success public static bool SaveDecodingToFile (string base64String, string fileName) {System.IO.FileStream fs = new System.IO.FileStream (fileName, System.IO.FileMode.Create); System.IO.BinaryWriter bw = new System.IO.BinaryWriter (fs); BW.WRITE (); bw.close (); fs.close (); return true;} // ---------------- -------------------------------------------------- ------------ // /// From the network address, the file is acquired and converted to Base64 encoding /// /// File URL address, an absolute URL address /// System.Net.WebClient object /// public static string eNCodingFireFromurl (String Url, System.Net.WebClient ObjwebClient) {Return Convert.TOBASE64STRING (ObjwebClient.download Data (URL));}

///

/// A file from the network address and converts to the base64 encoded /// /// file URL address, an absolute URL address /// the base64 string file conversion public static string EncodingFileFromUrl (string url) {// System.Net.WebClient myWebClient = new System.Net.WebClient (); return EncodingFileFromUrl ( URL, New System.Net.WebClient ());}}}

转载请注明原文地址:https://www.9cbs.com/read-112670.html

New Post(0)