SecurityUtil.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Text;
  3. using System.Security.Cryptography;
  4. namespace WMS.Util
  5. {
  6. /// <summary>
  7. /// MD5加密类
  8. /// </summary>
  9. public class SecurityUtil
  10. {
  11. #region MD5加密
  12. /// <summary>
  13. /// 给一个字符串进行MD5加密
  14. /// </summary>
  15. /// <param name="strText">待加密字符串</param>
  16. /// <returns>加密后的字符串</returns>
  17. public static string MD5Encrypt(string strText)
  18. {
  19. MD5 md5 = new MD5CryptoServiceProvider();
  20. byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
  21. return System.Text.Encoding.Default.GetString(result);
  22. }
  23. /// <summary>
  24. /// MD5加密
  25. /// </summary>
  26. /// <param name="str">加密字符</param>
  27. /// <param name="code">加密位数16/32</param>
  28. /// <returns></returns>
  29. public static string MD5(string str, int code)
  30. {
  31. MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
  32. byte[] bytes = Encoding.UTF7.GetBytes(str);
  33. bytes = crypto.ComputeHash(bytes);
  34. StringBuilder sb = new StringBuilder();
  35. foreach (byte num in bytes)
  36. {
  37. sb.AppendFormat("{0:x2}", num);
  38. }
  39. if (code == 16)
  40. {
  41. return sb.ToString().Substring(8, 16);
  42. }
  43. else
  44. {
  45. return sb.ToString();
  46. }
  47. }
  48. #endregion
  49. #region DES加密/解密
  50. /// <summary>
  51. /// DES加密数据
  52. /// </summary>
  53. /// <param name="Text"></param>
  54. /// <param name="sKey"></param>
  55. /// <returns></returns>
  56. public static string DesEncrypt(string Text, string sKey)
  57. {
  58. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  59. byte[] inputByteArray;
  60. inputByteArray = Encoding.Default.GetBytes(Text);
  61. des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  62. des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  63. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  64. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  65. cs.Write(inputByteArray, 0, inputByteArray.Length);
  66. cs.FlushFinalBlock();
  67. StringBuilder ret = new StringBuilder();
  68. foreach (byte b in ms.ToArray())
  69. {
  70. ret.AppendFormat("{0:X2}", b);
  71. }
  72. return ret.ToString();
  73. }
  74. /// <summary>
  75. /// DES解密数据
  76. /// </summary>
  77. /// <param name="Text"></param>
  78. /// <param name="sKey"></param>
  79. /// <returns></returns>
  80. public static string DesDecrypt(string Text, string sKey)
  81. {
  82. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  83. int len;
  84. len = Text.Length / 2;
  85. byte[] inputByteArray = new byte[len];
  86. int x, i;
  87. for (x = 0; x < len; x++)
  88. {
  89. i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
  90. inputByteArray[x] = (byte)i;
  91. }
  92. des.Key = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  93. des.IV = ASCIIEncoding.ASCII.GetBytes(MD5Encrypt(sKey).Substring(0, 8));
  94. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  95. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  96. cs.Write(inputByteArray, 0, inputByteArray.Length);
  97. cs.FlushFinalBlock();
  98. return Encoding.Default.GetString(ms.ToArray());
  99. }
  100. #endregion
  101. #region Base64 编码
  102. /// <summary>
  103.         /// Base64 编码
  104.         /// </summary>
  105.         /// <param name="encode">编码方式</param>
  106.         /// <param name="source">要编码的字符串</param>
  107.         /// <returns>返回编码后的字符串</returns>
  108. public static string EncodeBase64(Encoding encode, string source)
  109. {
  110. string result = "";
  111. byte[] bytes = encode.GetBytes(source);
  112. try
  113. {
  114. result = Convert.ToBase64String(bytes);
  115. }
  116. catch
  117. {
  118. result = source;
  119. }
  120. return result;
  121. }
  122. /// <summary>
  123. /// Base64 解码
  124. /// </summary>
  125. /// <param name="encode">解码方式</param>
  126. /// <param name="source">要解码的字符串</param>
  127. /// <returns>返回解码后的字符串</returns>
  128. public static string DecodeBase64(Encoding encode, string source)
  129. {
  130. string result = "";
  131. byte[] bytes = Convert.FromBase64String(source);
  132. try
  133. {
  134. result = encode.GetString(bytes);
  135. }
  136. catch
  137. {
  138. result = source;
  139. }
  140. return result;
  141. }
  142. #endregion
  143. #region AES加密/解密
  144. //AES加密
  145. public static string AesEncrypt(string value, string key, string iv = "")
  146. {
  147. if (string.IsNullOrEmpty(value)) return string.Empty;
  148. if (key == null) throw new Exception("未将对象引用设置到对象的实例。");
  149. if (key.Length < 16) throw new Exception("指定的密钥长度不能少于16位。");
  150. if (key.Length > 32) throw new Exception("指定的密钥长度不能多于32位。");
  151. if (key.Length != 16 && key.Length != 24 && key.Length != 32) throw new Exception("指定的密钥长度不明确。");
  152. if (!string.IsNullOrEmpty(iv))
  153. {
  154. if (iv.Length < 16) throw new Exception("指定的向量长度不能少于16位。");
  155. }
  156. var _keyByte = Encoding.UTF8.GetBytes(key);
  157. var _valueByte = Encoding.UTF8.GetBytes(value);
  158. using (var aes = new RijndaelManaged())
  159. {
  160. aes.IV = !string.IsNullOrEmpty(iv) ? Encoding.UTF8.GetBytes(iv) : Encoding.UTF8.GetBytes(key.Substring(0, 16));
  161. aes.Key = _keyByte;
  162. aes.Mode = CipherMode.CBC;
  163. aes.Padding = PaddingMode.PKCS7;
  164. var cryptoTransform = aes.CreateEncryptor();
  165. var resultArray = cryptoTransform.TransformFinalBlock(_valueByte, 0, _valueByte.Length);
  166. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  167. }
  168. }
  169. //AES解密
  170. public static string AesDecrypt(string value, string key, string iv = "")
  171. {
  172. if (string.IsNullOrEmpty(value)) return string.Empty;
  173. if (key == null) throw new Exception("未将对象引用设置到对象的实例。");
  174. if (key.Length < 16) throw new Exception("指定的密钥长度不能少于16位。");
  175. if (key.Length > 32) throw new Exception("指定的密钥长度不能多于32位。");
  176. if (key.Length != 16 && key.Length != 24 && key.Length != 32) throw new Exception("指定的密钥长度不明确。");
  177. if (!string.IsNullOrEmpty(iv))
  178. {
  179. if (iv.Length < 16) throw new Exception("指定的向量长度不能少于16位。");
  180. }
  181. var _keyByte = Encoding.UTF8.GetBytes(key);
  182. var _valueByte = Convert.FromBase64String(value);
  183. using (var aes = new RijndaelManaged())
  184. {
  185. aes.IV = !string.IsNullOrEmpty(iv) ? Encoding.UTF8.GetBytes(iv) : Encoding.UTF8.GetBytes(key.Substring(0, 16));
  186. aes.Key = _keyByte;
  187. aes.Mode = CipherMode.CBC;
  188. aes.Padding = PaddingMode.PKCS7;
  189. var cryptoTransform = aes.CreateDecryptor();
  190. var resultArray = cryptoTransform.TransformFinalBlock(_valueByte, 0, _valueByte.Length);
  191. return Encoding.UTF8.GetString(resultArray);
  192. }
  193. }
  194. #endregion
  195. }
  196. }