. NET implement string base64 encoding

Source: Internet
Author: User

usingSystem;usingSystem.Text;usingSystem.IO;usingsystem.security;usingSystem.Security.Cryptography; namespaceTgnet.Base.Security {/**//// <summary>     ///related operations for BASE64 encoding algorithms/// </summary>     /// <example>The following example Base64 encodes a string and returns the encoded string:/// <code>     ///Public string ToBase64 (String str) {///return Security.Base64.EncodingString (str); ///        }     /// </code>     /// </example>      Public classBase64 {/**//// <summary>         ///encrypt strings using the base64 algorithm/// </summary>         /// <param name= "sourcestring" >string to encrypt</param>         /// <param name= "Ens" >System.Text.Encoding objects, such as creating a Chinese encoding set object:///System.Text.Encoding.GetEncoding ("gb2312")</param>         /// <returns>encoded text string</returns>          Public Static stringEncodingstring (stringsourcestring,system.text.encoding Ens) {             returnconvert.tobase64string (Ens.getbytes (sourcestring)); }           /**//// <summary>         ///Use the default code page to encrypt strings using the base64 algorithm/// </summary>         /// <param name= "sourcestring" >string to encrypt</param>         /// <returns>the encrypted text string</returns>          Public Static stringEncodingstring (stringsourcestring) {             returnencodingstring (Sourcestring,system.text.encoding.default); }           /**//// <summary>         ///restores a string from a base64 encoded string, supports Chinese/// </summary>         /// <param name= "base64string" >Base64 the encrypted string</param>         /// <param name= "Ens" >System.Text.Encoding objects, such as creating a Chinese encoding set object:///System.Text.Encoding.Default</param>         /// <returns>the restored text string</returns>          Public Static stringDecodingstring (stringbase64string,system.text.encoding Ens) {             returnens.getstring ((convert.frombase64string (base64string))); }           /**//// <summary>         ///restores a string from a Base64 encoded string using the default code page, which supports Chinese/// </summary>         /// <param name= "base64string" >Base64 the encrypted string</param>         /// <returns>the restored text string</returns>          Public Static stringDecodingstring (stringbase64string) {              returndecodingstring (Base64string,system.text.encoding.default); }           /**//// <summary>         ///Base64 encodes a file and returns the encoded string/// </summary>         /// <param name= "strFileName" >path and filename of the file</param>         /// <returns>BASE64 encoded string for a file</returns>          Public Static stringEncodingfiletostring (stringstrFileName) {System.IO.FileStream FS=System.IO.File.OpenRead (strFileName); System.IO.BinaryReader BR=NewSystem.IO.BinaryReader (FS); stringBase64string=convert.tobase64string (Br. Readbytes ((int) fs.                       Length)); Br.             Close (); Fs.             Close (); returnbase64string; }             /**//// <summary>         ///Base64 encodes a file and writes the encoded content to a file/// </summary>         /// <param name= "Strsourcefilename" >file address to encode, support for any type of file</param>         /// <param name= "Strsavefilename" >the file path to write to</param>         /// <returns>If the write succeeds, returns the true</returns>          Public Static BOOLEncodingfiletofile (stringStrsourcefilename,stringstrsavefilename) {             stringstrbase64=encodingfiletostring (strsourcefilename); System.IO.StreamWriter FS=NewSystem.IO.StreamWriter (strsavefilename); Fs.             Write (STRBASE64); Fs.             Close (); return true; }           /**//// <summary>         ///decodes and stores Base64 encoded strings into a file/// </summary>         /// <param name= "base64string" >string after BASE64 encoding</param>         /// <param name= "Strsavefilename" >the file path to output, if the file exists, will be rewritten</param>         /// <returns>returns True if the operation is successful</returns>          Public Static BOOLDecodingfilefromstring (stringBase64string,stringstrsavefilename) {System.IO.FileStream FS=NewSystem.IO.FileStream (strsavefilename,system.io.filemode.create); System.IO.BinaryWriter BW=NewSystem.IO.BinaryWriter (FS); Bw.             Write (convert.frombase64string (base64string)); //bw. Write (convert.tobase64string)bw.             Close (); Fs.             Close (); return true; }           /**//// <summary>         ///decodes and stores a file generated by the BASE64 encoding into a file/// </summary>         /// <param name= "Strbase64filename" >files stored in BASE64 encoded format</param>         /// <param name= "Strsavefilename" >the file path to output, if the file exists, will be rewritten</param>         /// <returns>returns True if the operation is successful</returns>          Public Static BOOLDecodingfilefromfile (stringStrbase64filename,stringstrsavefilename) {System.IO.StreamReader FS=NewSystem.IO.StreamReader (STRBASE64FILENAME,SYSTEM.TEXT.ENCODING.ASCII); Char[] base64chararray=New Char[FS.             Basestream.length]; Fs. Read (Base64chararray,0,(int) fs.             Basestream.length); stringbase64string=New string(Base64chararray); Fs.             Close (); returndecodingfilefromstring (base64string,strsavefilename); }           /**//// <summary>         ///get files from network address one and convert to Base64 encoding/// </summary>         /// <param name= "strURL" >the URL address of the file, which must be an absolute URL address</param>         /// <param name= "Objwebclient" >System.Net.WebClient Object</param>         /// <returns>returns a BASE64 encoded Web resource string</returns>          Public Static stringEncodingwebfile (stringstrurl,system.net.webclient objwebclient) {             returnconvert.tobase64string (Objwebclient.downloaddata (strURL)); }           /**//// <summary>         ///get files from network address one and convert to Base64 encoding/// </summary>         /// <param name= "strURL" >the URL address of the file, which must be an absolute URL address</param>         /// <returns>returns a BASE64 encoded Web resource string</returns>          Public Static stringEncodingwebfile (stringstrURL) {             returnEncodingwebfile (strURL,NewSystem.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.