Using System; using System. text; using System. IO; using System. security; using System. security. cryptography; namespace Tgnet. base. security {/** // <summary> /// operations related to the Base64 encoding algorithm /// </summary> /// <example> the following example shows encode a string in Base64 format, and return the encoded string: // <code> // public string ToBase64 (string str) {// return Security. base64.EncodingString (str ); ///} // </code> /// </example> public class Base64 {/** // <summary> // use the base64 algorithm to encode the string encryption /// </summary> /// <param name = "SourceString"> string to be encrypted </param> /// <param name = "Ens"> System. text. encoding object, such as creating a Chinese sequence set object: // System. text. encoding. getEncoding ("gb2312") </param> // <returns> encoded text string </returns> public static string EncodingString (string SourceString, System. text. encoding Ens) {return Convert. toBase64String (Ens. getBytes (SourceString ));} /** // <summary> // use the default code page to encrypt the string using base64. // </summary> /// <param name = "SourceString"> string to be encrypted </param> // <returns> the encrypted text string </returns> public static string EncodingString (string SourceString) {return EncodingString (SourceString, System. text. encoding. default);}/** // <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, such as creating a Chinese sequence set object: // System. text. encoding. default </param> // <returns> restored text string </returns> public static string DecodingString (string Base64String, System. text. encoding Ens) {return Ens. getString (Convert. fromBase64String (Base64String);}/** // <summary> // use the default code page to restore a 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 DecodingString (string Base64String) {return DecodingString (Base64String, System. text. encoding. default);}/** // <summary> // encode an object using Base64, the encoded string /// </summary> /// <param name = "strFileName"> file path and file name </param> /// <returns> base64 encoded string </returns> public static string EncodingFileToString (string strFileName) {System. IO. fileStream fs = System. IO. file. openRead (strFileName); System. IO. binaryReader br = new System. IO. binaryReader (fs); string Base64String = Convert. toBase64String (br. readBytes (int) fs. length); br. close (); fs. close (); return Base64String;}/** // <summary> // encode an object by Base64, write the encoded content to a file // </summary> /// <param name = "strSourceFileName"> the address of the file to be encoded, supports all types of files </param> /// <param name = "strSaveFileName"> path of the file to be written </param> /// <returns> If the write is successful, returns the true </returns> public static bool EncodingFileToFile (string strSourceFileName, string strSaveFileName) {string strBase64 = EncodingFileToString (strSourceFileName); System. IO. streamWriter fs = new System. IO. streamWriter (strSaveFileName); fs. write (strBase64); fs. close (); return true ;} /** // <summary> // decodes and stores the Base64 encoded string in a file. /// </summary> /// <param name = "Base64String "> Base64 encoded string </param> // <param name =" strSaveFileName "> path of the file to be output, if the file exists, it will be overwritten </param> // <returns> If the operation is successful, True is returned </returns> public static bool DecodingFileFromString (string Base64String, string strSaveFileName) {System. IO. fileStream fs = new System. IO. fileStream (strSaveFileName, System. IO. fileMode. create); System. IO. binaryWriter bw = new System. IO. binaryWriter (fs); bw. write (Convert. fromBase64String (Base64String); // bw. write (Convert. toBase64String) bw. close (); fs. close (); return true ;} /** // <summary> // decodes and stores a Base64 encoded file to a file. /// </summary> // <param name = "strBase64FileName"> path of the file to be output in Base64 encoding format </param> // <param name = "strSaveFileName">, if the file exists, it will be overwritten </param> // <returns> If the operation is successful, True is returned </returns> public static bool DecodingFileFromFile (string strBase64FileName, string strSaveFileName) {System. IO. streamReader fs = new System. IO. streamReader (strBase64FileName, System. text. encoding. ASCII); char [] base64CharArray = new char [fs. baseStream. length]; fs. read (base64CharArray, 0, (int) fs. baseStream. length); string Base64String = new string (base64CharArray); fs. close (); return DecodingFileFromString (Base64String, strSaveFileName );} /** // <summary> // obtain the file from network address 1 and convert it to base64 encoding // </summary> /// <param name = "strURL "> File URL, the URL must be an absolute URL </param> // <param name = "objWebClient"> System. net. webClient object </param> /// <returns> returns a Base64 encoded Web Resource string </returns> public static string EncodingWebFile (string strURL, System. net. webClient objWebClient) {return Convert. toBase64String (objWebClient. downloadData (strURL ));} /** // <summary> // obtain the file from network address 1 and convert it to base64 encoding // </summary> /// <param name = "strURL "> File URL, must be an absolute URL address </param> /// <returns> returns a Base64 encoded Web Resource string </returns> public static string EncodingWebFile (string strURL) {return EncodingWebFile (strURL, new System. net. webClient ());}}}