Encrypt.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace WebAPIBase.Utils
  6. {
  7. public partial class StringHelper
  8. {
  9. /// <summary>
  10. /// 中英文加密
  11. /// </summary>
  12. /// <param name="instr">要加密的字体串</param>
  13. /// <returns></returns>
  14. public static string EncryptMD5(string instr)
  15. {
  16. string result;
  17. try
  18. {
  19. byte[] toByte = Encoding.Default.GetBytes(instr);
  20. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  21. toByte = md5.ComputeHash(toByte);
  22. result = BitConverter.ToString(toByte).Replace("-", "");
  23. }
  24. catch
  25. {
  26. result = string.Empty;
  27. }
  28. return result;
  29. }
  30. /// <summary>
  31. /// 指定字节流编码计算MD5哈希值,可解决不同系统中文编码差异的问题。
  32. /// </summary>
  33. /// <param name="source">要进行哈希的字符串</param>
  34. /// <param name="bytesEncoding">获取字符串字节流的编码,如果是中文,不同系统之间务必请使用相同编码</param>
  35. /// <returns>32位大写MD5哈希值</returns>
  36. public static string ComputeMD5(string source, Encoding bytesEncoding)
  37. {
  38. byte[] sourceBytes = bytesEncoding.GetBytes(source);
  39. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  40. byte[] hashedBytes = md5.ComputeHash(sourceBytes);
  41. StringBuilder buffer = new StringBuilder(hashedBytes.Length);
  42. foreach (byte item in hashedBytes)
  43. {
  44. buffer.AppendFormat("{0:X2}", item);
  45. }
  46. return buffer.ToString();
  47. }
  48. #region 加密解密,可以设置密钥
  49. /// <summary>
  50. /// 可以设置密钥的加密方式
  51. /// </summary>
  52. /// <param name="sourceData">原文</param>
  53. /// <param name="key">密钥,8位数字,字符串方式</param>
  54. /// <returns></returns>
  55. public static string Encrypt(string sourceData, string key)
  56. {
  57. #region 检查密钥是否符合规定
  58. if (key.Length > 8)
  59. {
  60. key = key.Substring(0, 8);
  61. }
  62. #endregion
  63. Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  64. Byte[] keys = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, key.Length));
  65. try
  66. {
  67. //convert data to byte array
  68. Byte[] sourceDataBytes = System.Text.ASCIIEncoding.UTF8.GetBytes(sourceData);
  69. //get target memory stream
  70. MemoryStream tempStream = new MemoryStream();
  71. //get encryptor and encryption stream
  72. DESCryptoServiceProvider encryptor = new DESCryptoServiceProvider();
  73. CryptoStream encryptionStream = new CryptoStream(tempStream, encryptor.CreateEncryptor(keys, iv), CryptoStreamMode.Write);
  74. //encrypt data
  75. encryptionStream.Write(sourceDataBytes, 0, sourceDataBytes.Length);
  76. encryptionStream.FlushFinalBlock();
  77. //put data into byte array
  78. Byte[] encryptedDataBytes = tempStream.GetBuffer();
  79. //convert encrypted data into string
  80. return System.Convert.ToBase64String(encryptedDataBytes, 0, (int)tempStream.Length);
  81. }
  82. catch (Exception ex)
  83. {
  84. throw (ex);
  85. }
  86. }
  87. /// <summary>
  88. /// 根据知道的密钥解密
  89. /// </summary>
  90. /// <param name="sourceData">密文</param>
  91. /// <param name="key">密钥,8位数字,字符串方式</param>
  92. /// <returns></returns>
  93. public static string Decrypt(string sourceData, string key)
  94. {
  95. #region 检查密钥是否符合规定
  96. if (key.Length > 8)
  97. {
  98. key = key.Substring(0, 8);
  99. }
  100. #endregion
  101. Byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  102. Byte[] keys = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, key.Length));
  103. try
  104. {
  105. Byte[] encryptedDataBytes = System.Convert.FromBase64String(sourceData);
  106. using (MemoryStream tempStream = new MemoryStream(encryptedDataBytes, 0, encryptedDataBytes.Length))
  107. {
  108. using (DESCryptoServiceProvider decryptor = new DESCryptoServiceProvider())
  109. {
  110. using (
  111. CryptoStream decryptionStream = new CryptoStream(tempStream,
  112. decryptor.CreateDecryptor(keys, iv), CryptoStreamMode.Read))
  113. {
  114. using (StreamReader allDataReader = new StreamReader(decryptionStream))
  115. {
  116. return allDataReader.ReadToEnd();
  117. }
  118. }
  119. }
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. throw;
  125. }
  126. }
  127. #endregion
  128. [Obsolete("方法名写错了,请用ASP16MD5", false)]
  129. public static string ASP18MD5(string instr)
  130. {
  131. return EncryptMD5(instr).Substring(8, 16).ToLower();
  132. }
  133. /// <summary>
  134. /// 16位md5加密,返回小写
  135. /// </summary>
  136. /// <param name="instr">要加密的字体串</param>
  137. /// <returns></returns>
  138. public static string ASP16MD5(string instr)
  139. {
  140. return EncryptMD5(instr).Substring(8, 16).ToLower();
  141. }
  142. }
  143. }