ExtendedUtils.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. namespace WWPipeLine.Commons
  6. {
  7. /// <summary>
  8. /// 扩展静态类
  9. /// </summary>
  10. public static class ExtendedUtils
  11. {
  12. private static string encryptKey = Guid.NewGuid().ToString("").Substring(0, 4);
  13. private static string encryptValue = "sxzy";
  14. public static string Encrypt(string str, string keyStr = "")
  15. {
  16. try
  17. {
  18. DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
  19. if (string.IsNullOrEmpty(keyStr))
  20. {
  21. keyStr = encryptKey;
  22. }
  23. byte[] key = Encoding.Unicode.GetBytes(keyStr);
  24. byte[] data = Encoding.Unicode.GetBytes(str);
  25. MemoryStream MStream = new MemoryStream();
  26. CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
  27. CStream.Write(data, 0, data.Length);
  28. CStream.FlushFinalBlock();
  29. return Convert.ToBase64String(MStream.ToArray()) + keyStr;
  30. }
  31. catch (Exception ex)
  32. {
  33. Commons.LogHelper.Error(ex);
  34. }
  35. return string.Empty;
  36. }
  37. //字符串解密
  38. public static string Decrypt(string str, string keyStr = "")
  39. {
  40. DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
  41. try
  42. {
  43. if (string.IsNullOrEmpty(keyStr))
  44. {
  45. keyStr = encryptKey;
  46. }
  47. byte[] key = Encoding.Unicode.GetBytes(keyStr);
  48. byte[] data = Convert.FromBase64String(str);
  49. MemoryStream MStream = new MemoryStream();
  50. CryptoStream CStram = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
  51. CStram.Write(data, 0, data.Length);
  52. CStram.FlushFinalBlock();
  53. return Encoding.Unicode.GetString(MStream.ToArray());
  54. }
  55. catch (Exception ex)
  56. {
  57. Commons.LogHelper.Error(ex);
  58. }
  59. return string.Empty;
  60. }
  61. public static string Encrypts(string str)
  62. {
  63. try
  64. {
  65. DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
  66. byte[] key = Encoding.Unicode.GetBytes(encryptValue);
  67. byte[] data = Encoding.Unicode.GetBytes(str);
  68. MemoryStream MStream = new MemoryStream();
  69. CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
  70. CStream.Write(data, 0, data.Length);
  71. CStream.FlushFinalBlock();
  72. return Convert.ToBase64String(MStream.ToArray());
  73. }
  74. catch (Exception ex)
  75. {
  76. Commons.LogHelper.Error(ex);
  77. }
  78. return string.Empty;
  79. }
  80. //字符串解密
  81. public static string Decrypts(string str)
  82. {
  83. DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
  84. try
  85. {
  86. byte[] key = Encoding.Unicode.GetBytes(encryptValue);
  87. byte[] data = Convert.FromBase64String(str);
  88. MemoryStream MStream = new MemoryStream();
  89. CryptoStream CStram = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
  90. CStram.Write(data, 0, data.Length);
  91. CStram.FlushFinalBlock();
  92. return Encoding.Unicode.GetString(MStream.ToArray());
  93. }
  94. catch (Exception ex)
  95. {
  96. Commons.LogHelper.Error(ex);
  97. }
  98. return string.Empty;
  99. }
  100. }
  101. }