Base64CryptionHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. using System;
  2. using System.Collections;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace Vinno.vCloud.Common.FIS.Helper
  6. {
  7. /// <summary>
  8. /// Base64 加密、解密
  9. /// <para>主要方法如下:</para>
  10. /// <para>01. Encrypt(string text) //加密字符串</para>
  11. /// <para>02. Decrypt(string text) //解密字符串</para>
  12. /// 6Bit数字【0~63】映射Base64字符表如下,补位我们使用=等号代替,按(64,'=')处理
  13. /// </summary>
  14. internal class Base64CryptionHelper
  15. {
  16. #region 加密字符串
  17. /// <summary>
  18. /// Base64加密,整体加密
  19. /// </summary>
  20. /// <param name="text">待加密的字符串</param>
  21. /// <returns>加密后字符串</returns>
  22. private static string Encrypt(string text)
  23. {
  24. //如果字符串为空,则返回
  25. if (string.IsNullOrEmpty(text))
  26. {
  27. return "";
  28. }
  29. try
  30. {
  31. char[] Base64Code = new char[]{'0','1','2','3','4','5','6','7',
  32. '8','9','+','/','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
  33. 'o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
  34. 'U','V','W','X','Y','Z','='};
  35. byte empty = 0;
  36. ArrayList byteMessage = new ArrayList(Encoding.UTF8.GetBytes(text));
  37. StringBuilder outmessage;
  38. int messageLen = byteMessage.Count;
  39. int page = messageLen / 3;
  40. int use = 0;
  41. if ((use = messageLen % 3) > 0)
  42. {
  43. for (int i = 0; i < 3 - use; i++)
  44. byteMessage.Add(empty);
  45. page++;
  46. }
  47. outmessage = new StringBuilder(page * 4);
  48. for (int i = 0; i < page; i++)
  49. {
  50. byte[] instr = new byte[3];
  51. instr[0] = (byte)byteMessage[i * 3];
  52. instr[1] = (byte)byteMessage[i * 3 + 1];
  53. instr[2] = (byte)byteMessage[i * 3 + 2];
  54. int[] outstr = new int[4];
  55. outstr[0] = instr[0] >> 2;
  56. outstr[1] = (instr[0] & 0x03) << 4 ^ instr[1] >> 4;
  57. if (!instr[1].Equals(empty))
  58. outstr[2] = (instr[1] & 0x0f) << 2 ^ instr[2] >> 6;
  59. else
  60. outstr[2] = 64;
  61. if (!instr[2].Equals(empty))
  62. outstr[3] = instr[2] & 0x3f;
  63. else
  64. outstr[3] = 64;
  65. outmessage.Append(Base64Code[outstr[0]]);
  66. outmessage.Append(Base64Code[outstr[1]]);
  67. outmessage.Append(Base64Code[outstr[2]]);
  68. outmessage.Append(Base64Code[outstr[3]]);
  69. }
  70. return outmessage.ToString();
  71. }
  72. catch (Exception ex)
  73. {
  74. throw ex;
  75. }
  76. }
  77. /// <summary>
  78. /// Base64加密,单独加密
  79. /// </summary>
  80. /// <param name="text">待加密的字符串</param>
  81. /// <returns>加密后字符串</returns>
  82. public static string EncryptIndependent(string text)
  83. {
  84. //如果字符串为空,则返回
  85. if (string.IsNullOrEmpty(text))
  86. {
  87. return "";
  88. }
  89. try
  90. {
  91. if (text.Length == 1)
  92. {
  93. return Encrypt(text);
  94. }
  95. else
  96. {
  97. StringBuilder outmessage = new StringBuilder();
  98. for (int i = 0; i < text.Length; i++)
  99. {
  100. outmessage.Append(Encrypt(text[i].ToString()));
  101. }
  102. return outmessage.ToString();
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. throw ex;
  108. }
  109. }
  110. #endregion 加密字符串
  111. #region 解密 字符串
  112. /// <summary>
  113. /// Base64解密,整体解密
  114. /// </summary>
  115. /// <param name="text">待解密的字符串</param>
  116. /// <returns>解密后的字符串</returns>
  117. private static string Decrypt(string text)
  118. {
  119. //如果字符串为空,则返回
  120. if (string.IsNullOrEmpty(text))
  121. {
  122. return "";
  123. }
  124. //将空格替换为加号
  125. text = text.Replace(" ", "+");
  126. try
  127. {
  128. if (text.Length % 4 != 0)
  129. {
  130. throw new Exception("包含不正确的BASE64编码");
  131. }
  132. if (!Regex.IsMatch(text, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase))
  133. {
  134. throw new Exception("包含不正确的BASE64编码");
  135. }
  136. string Base64Code = "0123456789+/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=";
  137. int page = text.Length / 4;
  138. ArrayList outMessage = new ArrayList(page * 3);
  139. char[] message = text.ToCharArray();
  140. for (int i = 0; i < page; i++)
  141. {
  142. byte[] instr = new byte[4];
  143. instr[0] = (byte)Base64Code.IndexOf(message[i * 4]);
  144. instr[1] = (byte)Base64Code.IndexOf(message[i * 4 + 1]);
  145. instr[2] = (byte)Base64Code.IndexOf(message[i * 4 + 2]);
  146. instr[3] = (byte)Base64Code.IndexOf(message[i * 4 + 3]);
  147. byte[] outstr = new byte[3];
  148. outstr[0] = (byte)(instr[0] << 2 ^ (instr[1] & 0x30) >> 4);
  149. if (instr[2] != 64)
  150. {
  151. outstr[1] = (byte)(instr[1] << 4 ^ (instr[2] & 0x3c) >> 2);
  152. }
  153. else
  154. {
  155. outstr[2] = 0;
  156. }
  157. if (instr[3] != 64)
  158. {
  159. outstr[2] = (byte)(instr[2] << 6 ^ instr[3]);
  160. }
  161. else
  162. {
  163. outstr[2] = 0;
  164. }
  165. outMessage.Add(outstr[0]);
  166. if (outstr[1] != 0)
  167. outMessage.Add(outstr[1]);
  168. if (outstr[2] != 0)
  169. outMessage.Add(outstr[2]);
  170. }
  171. byte[] outbyte = (byte[])outMessage.ToArray(Type.GetType("System.Byte"));
  172. return Encoding.UTF8.GetString(outbyte);
  173. }
  174. catch (Exception ex)
  175. {
  176. throw ex;
  177. }
  178. }
  179. /// <summary>
  180. /// Base64解密,单独解密
  181. /// </summary>
  182. /// <param name="text">待解密的字符串</param>
  183. /// <returns>解密后的字符串</returns>
  184. public static string DecryptIndependent(string text)
  185. {
  186. //如果字符串为空,则返回
  187. if (string.IsNullOrEmpty(text))
  188. {
  189. return "";
  190. }
  191. //将空格替换为加号
  192. text = text.Replace(" ", "+");
  193. try
  194. {
  195. if (text.Length % 4 != 0)
  196. {
  197. throw new Exception("包含不正确的BASE64编码");
  198. }
  199. if (!Regex.IsMatch(text, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase))
  200. {
  201. throw new Exception("包含不正确的BASE64编码");
  202. }
  203. if (text.Length == 4)
  204. {
  205. return Decrypt(text);
  206. }
  207. else
  208. {
  209. StringBuilder outMessage = new StringBuilder();
  210. for (int i = 0; i < text.Length; i = i + 4)
  211. {
  212. outMessage.Append(Decrypt(text.Substring(i, 4)));
  213. }
  214. return outMessage.ToString();
  215. }
  216. }
  217. catch (Exception ex)
  218. {
  219. throw ex;
  220. }
  221. }
  222. #endregion 解密 字符串
  223. }
  224. }