Base64 encoding class source code (C #)

Source: Internet
Author: User
Tags file url
Using system; namespace shapbse64 {// <summary> // base64 encoding Algorithm Related Operations // by free Pentium (wgscd) /// </Summary> public class sbase64 {public sbase64 () {// todo: here, add the constructor logic //} // encrypt /// <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 (54936);} // <summary> // restores a string from a base64 encoded string, supports Chinese characters /// </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 byte value obtained from base64string is the character's internal code (ANSI character encoding) * generally, it is the formula to convert the data into Chinese characters using the internal code: * (char) (first byte binary value * 256 + second byte value) * Because the char or string in C # adopts unicode encoding, it cannot be used to calculate * ANSI Byte encoding and Unicode encoding incompatibility * according to the above formula.. Net Class Library provides the encoding class implementation from ANSI encoding 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 return ens from base64string. getstring (convert. frombase64string (base64string);} // <summary> // restores the string from a base64 encoded string, supports Chinese characters /// </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 (54936 ));} /// encode /// <summary> /// encode any type of file with base64 /// </Summary> /// <Param name = "FILENAME"> path and file name </param> /// <returns> base64-encoded string </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);/* 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 ;} /// <summary> /// Save the base64 encoded string as a file // </Summary> /// <Param name = "base64string"> after base64 encoding </param> /// <Param name = "FILENAME"> Save the file path and file name </param> /// <returns> whether the file is saved successfully </returns> 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 (convert. frombase64string (base64string); BW. close (); FS. close (); Return true ;} // summary /// <summary> /// obtain the file from network address 1 and convert it to base64 encoding /// </Summary> /// <Param name = "url"> File URL, an absolute URL </param> // <Param name = "objwebclient"> system. net. webClient object </param> /// <returns> </returns> Public static string encodingfilefromurl (string URL, system. net. webClient objwebclient) {return convert. tobase64string (objwebclient. downloaddata (URL ));} /// <summary> /// obtain the file from network address 1 and convert it to base64 encoding /// </Summary> /// <Param name = "url"> URL address, an absolute URL address </param> // <returns> converts the object to a base64 string </returns> Public static string encodingfilefromurl (string URL) {// system. net. webClient mywebclient = new system. net. webClient (); Return encodingfilefromurl (URL, new system. net. webClient ());}}}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.