SoftSecurity.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. namespace HslCommunication.BasicFramework
  8. {
  9. /**********************************************************************************
  10. *
  11. * Create Date:2017-05-03 16:52:48
  12. * Author By Richard.Hu
  13. *
  14. *
  15. **********************************************************************************/
  16. /// <summary>
  17. /// 字符串加密解密相关的自定义类
  18. /// </summary>
  19. public static class SoftSecurity
  20. {
  21. /// <summary>
  22. /// 加密数据,采用对称加密的方式
  23. /// </summary>
  24. /// <param name="pToEncrypt">待加密的数据</param>
  25. /// <returns>加密后的数据</returns>
  26. internal static string MD5Encrypt(string pToEncrypt)
  27. {
  28. return MD5Encrypt(pToEncrypt, "zxcvBNMM");
  29. }
  30. /// <summary>
  31. /// 加密数据,采用对称加密的方式
  32. /// </summary>
  33. /// <param name="pToEncrypt">待加密的数据</param>
  34. /// <param name="Password">密钥,长度为8,英文或数字</param>
  35. /// <returns>加密后的数据</returns>
  36. public static string MD5Encrypt(string pToEncrypt, string Password)
  37. {
  38. string aisdhaisdhwdb = Password;
  39. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  40. byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
  41. des.Key = Encoding.ASCII.GetBytes(aisdhaisdhwdb);
  42. des.IV = Encoding.ASCII.GetBytes(aisdhaisdhwdb);
  43. MemoryStream ms = new MemoryStream();
  44. CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
  45. cs.Write(inputByteArray, 0, inputByteArray.Length);
  46. cs.FlushFinalBlock();
  47. StringBuilder ret = new StringBuilder();
  48. foreach (byte b in ms.ToArray())
  49. {
  50. ret.AppendFormat("{0:X2}", b);
  51. }
  52. ret.ToString();
  53. return ret.ToString();
  54. }
  55. /// <summary>
  56. /// 解密过程,使用的是对称的加密
  57. /// </summary>
  58. /// <param name="pToDecrypt">等待解密的字符</param>
  59. /// <returns>返回原密码,如果解密失败,返回‘解密失败’</returns>
  60. internal static string MD5Decrypt(string pToDecrypt)
  61. {
  62. return MD5Decrypt(pToDecrypt, "zxcvBNMM");
  63. }
  64. /// <summary>
  65. /// 解密过程,使用的是对称的加密
  66. /// </summary>
  67. /// <param name="pToDecrypt">等待解密的字符</param>
  68. /// <param name="password">密钥,长度为8,英文或数字</param>
  69. /// <returns>返回原密码,如果解密失败,返回‘解密失败’</returns>
  70. public static string MD5Decrypt(string pToDecrypt, string password)
  71. {
  72. if (pToDecrypt == "") return pToDecrypt;
  73. string zxcawrafdgegasd = password;
  74. DESCryptoServiceProvider des = new DESCryptoServiceProvider();
  75. byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
  76. for (int x = 0; x < pToDecrypt.Length / 2; x++)
  77. {
  78. int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
  79. inputByteArray[x] = (byte)i;
  80. }
  81. des.Key = Encoding.ASCII.GetBytes(zxcawrafdgegasd);
  82. des.IV = Encoding.ASCII.GetBytes(zxcawrafdgegasd);
  83. MemoryStream ms = new MemoryStream();
  84. CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
  85. cs.Write(inputByteArray, 0, inputByteArray.Length);
  86. cs.FlushFinalBlock();
  87. cs.Dispose();
  88. return Encoding.Default.GetString(ms.ToArray());
  89. }
  90. }
  91. }