You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
7.8 KiB

2 years ago
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Text;
  5. namespace EasyBL
  6. {
  7. public class Base64Helper
  8. {
  9. /// <summary>
  10. /// </summary>
  11. /// <param name="str">todo: describe str parameter on Deserialize</param>
  12. /// <returns></returns>
  13. public static object Deserialize(string str)
  14. {
  15. return Base64Helper.DeserializeBase64(str);
  16. }
  17. /// <summary>
  18. /// </summary>
  19. /// <param name="obj"></param>
  20. /// <returns></returns>
  21. public static string Serialize(object obj)
  22. {
  23. return Base64Helper.SerializeBase64(obj);
  24. }
  25. public static string SerializeBase64(object o)
  26. {
  27. // Serialize to a base 64 string
  28. byte[] bytes;
  29. long length = 0;
  30. var ws = new MemoryStream();
  31. var sf = new BinaryFormatter();
  32. sf.Serialize(ws, o);
  33. length = ws.Length;
  34. bytes = ws.GetBuffer();
  35. var encodedData = bytes.Length + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
  36. return encodedData;
  37. }
  38. public static object DeserializeBase64(string s)
  39. {
  40. // We need to know the exact length of the string - Base64 can sometimes pad us by a byte
  41. // or two
  42. var p = s.IndexOf(':');
  43. var length = Convert.ToInt32(s.Substring(0, p));
  44. // Extract data from the base 64 string!
  45. var memorydata = Convert.FromBase64String(s.Substring(p + 1));
  46. var rs = new MemoryStream(memorydata, 0, length);
  47. var sf = new BinaryFormatter();
  48. var o = sf.Deserialize(rs);
  49. return o;
  50. }
  51. public static string ToBase64(string str)
  52. {
  53. return EncodingString(str);
  54. }
  55. /// <summary>
  56. /// 将字符串使用base64算法加密
  57. /// </summary>
  58. /// <param name="SourceString">待加密的字符串</param>
  59. /// <param name="Ens">Encoding 对象,如创建中文编码集对象: Encoding.GetEncoding("gb2312")</param>
  60. /// <returns>编码后的文本字符串</returns>
  61. public static string EncodingString(string SourceString, Encoding Ens)
  62. {
  63. return Convert.ToBase64String(Ens.GetBytes(SourceString));
  64. }
  65. /// <summary>
  66. /// 使用缺省的代码页将字符串使用base64算法加密
  67. /// </summary>
  68. /// <param name="SourceString">待加密的字符串</param>
  69. /// <returns>加密后的文本字符串</returns>
  70. public static string EncodingString(string SourceString)
  71. {
  72. return EncodingString(SourceString, Encoding.Default);
  73. }
  74. /// <summary>
  75. /// 从base64编码的字符串中还原字符串,支持中文
  76. /// </summary>
  77. /// <param name="Base64String">Base64加密后的字符串</param>
  78. /// <param name="Ens">Encoding对象,如创建中文编码集对象: Encoding.Default</param>
  79. /// <returns>还原后的文本字符串</returns>
  80. public static string DecodingString(string Base64String, Encoding Ens)
  81. {
  82. return Ens.GetString((Convert.FromBase64String(Base64String)));
  83. }
  84. /// <summary>
  85. ///使用缺省的代码页从Base64编码的字符串中还原字符串,支持中文
  86. /// </summary>
  87. /// <param name="Base64String">Base64加密后的字符串</param>
  88. /// <returns>还原后的文本字符串</returns>
  89. public static string DecodingString(string Base64String)
  90. {
  91. return DecodingString(Base64String, Encoding.Default);
  92. }
  93. /// <summary>
  94. /// 对一个文件进行Base64编码,并返回编码后的字符串
  95. /// </summary>
  96. /// <param name="strFileName">文件的路径和文件名</param>
  97. /// <returns>对文件进行Base64编码后的字符串</returns>
  98. public static string EncodingFileToString(string strFileName)
  99. {
  100. var fs = File.OpenRead(strFileName);
  101. using (var br = new BinaryReader(fs))
  102. {
  103. var Base64String = Convert.ToBase64String(br.ReadBytes((int)fs.Length));
  104. br.Close();
  105. fs.Close();
  106. return Base64String;
  107. }
  108. }
  109. /// <summary>
  110. /// 对一个文件进行Base64编码,并将编码后的内容写入到一个文件
  111. /// </summary>
  112. /// <param name="strSourceFileName">要编码的文件地址,支持任何类型的文件</param>
  113. /// <param name="strSaveFileName">要写入的文件路径</param>
  114. /// <returns>如果写入成功,则返回真</returns>
  115. public static bool EncodingFileToFile(string strSourceFileName, string strSaveFileName)
  116. {
  117. var strBase64 = EncodingFileToString(strSourceFileName);
  118. using (var fs = new StreamWriter(strSaveFileName))
  119. {
  120. fs.Write(strBase64);
  121. fs.Close();
  122. return true;
  123. }
  124. }
  125. /// <summary>
  126. /// 将Base64编码字符串解码并存储到一个文件中
  127. /// </summary>
  128. /// <param name="Base64String">经过Base64编码后的字符串</param>
  129. /// <param name="strSaveFileName">要输出的文件路径,如果文件存在,将被重写</param>
  130. /// <returns>如果操作成功,则返回True</returns>
  131. public static bool DecodingFileFromString(string Base64String, string strSaveFileName)
  132. {
  133. var fs = new FileStream(strSaveFileName, FileMode.Create);
  134. using (var bw = new BinaryWriter(fs))
  135. {
  136. bw.Write(Convert.FromBase64String(Base64String));
  137. //bw.Write(Convert.ToBase64String)
  138. bw.Close();
  139. fs.Close();
  140. return true;
  141. }
  142. }
  143. /// <summary>
  144. /// 将一个由Base64编码产生的文件解码并存储到一个文件
  145. /// </summary>
  146. /// <param name="strBase64FileName">以Base64编码格式存储的文件</param>
  147. /// <param name="strSaveFileName">要输出的文件路径,如果文件存在,将被重写</param>
  148. /// <returns>如果操作成功,则返回True</returns>
  149. public static bool DecodingFileFromFile(string strBase64FileName, string strSaveFileName)
  150. {
  151. using (var fs = new StreamReader(strBase64FileName, Encoding.ASCII))
  152. {
  153. var base64CharArray = new char[fs.BaseStream.Length];
  154. fs.Read(base64CharArray, 0, (int)fs.BaseStream.Length);
  155. var Base64String = new string(base64CharArray);
  156. fs.Close();
  157. return DecodingFileFromString(Base64String, strSaveFileName);
  158. }
  159. }
  160. /// <summary>
  161. /// 从网络地址一取得文件并转化为base64编码
  162. /// </summary>
  163. /// <param name="strURL">文件的URL地址,必须是绝对URL地址</param>
  164. /// <param name="objWebClient">System.Net.WebClient 对象</param>
  165. /// <returns>返回经过Base64编码的Web资源字符串</returns>
  166. public static string EncodingWebFile(string strURL, System.Net.WebClient objWebClient)
  167. {
  168. return Convert.ToBase64String(objWebClient.DownloadData(strURL));
  169. }
  170. /// <summary>
  171. /// 从网络地址一取得文件并转化为base64编码
  172. /// </summary>
  173. /// <param name="strURL">文件的URL地址,必须是绝对URL地址</param>
  174. /// <returns>返回经过Base64编码的Web资源字符串</returns>
  175. public static string EncodingWebFile(string strURL) => EncodingWebFile(strURL, new System.Net.WebClient());
  176. }
  177. }