clsCustomEncryption.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.IO;
  8. namespace CommonObj.common
  9. {
  10. public sealed class CustomEncryption
  11. {
  12. //private static string key = "sfdjf48mdfdf3054";
  13. //private static string key = "sfdjf48mdfdf3054123";
  14. public CustomEncryption()
  15. {
  16. }
  17. /// <summary>
  18. /// Mã hóa dựa trên thuật toán mã hóa 1 chiều MD5,
  19. /// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
  20. /// </summary>
  21. /// <param name="plainText">Chuỗi cần mã hóa</param>
  22. /// <returns></returns>
  23. public static string EncryptUseMD5(string plainText)
  24. {
  25. string encrypted = null;
  26. MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
  27. byte[] pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(plainText));
  28. encrypted = Convert.ToBase64String(pwdhash);
  29. return encrypted;
  30. }
  31. /// <summary>
  32. /// Mã hóa dựa trên thuật toán mã hóa phức hợp, có thể giải mã sử dụng hàm Decrypt,
  33. /// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
  34. /// </summary>
  35. /// <param name="plainText">Chuỗi cần mã hóa</param>
  36. /// <returns></returns>
  37. public static string Encrypt(string clearText, string EncryptionKey)
  38. {
  39. //string EncryptionKey = "abc123";
  40. byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
  41. using (Aes encryptor = Aes.Create())
  42. {
  43. Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  44. encryptor.Key = pdb.GetBytes(32);
  45. encryptor.IV = pdb.GetBytes(16);
  46. using (MemoryStream ms = new MemoryStream())
  47. {
  48. using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
  49. {
  50. cs.Write(clearBytes, 0, clearBytes.Length);
  51. cs.Close();
  52. }
  53. clearText = Convert.ToBase64String(ms.ToArray());
  54. }
  55. }
  56. return clearText;
  57. }
  58. public static string Decrypt(string cipherText, string EncryptionKey)
  59. {
  60. //string EncryptionKey = "abc123";
  61. try
  62. {
  63. cipherText = cipherText.Replace(" ", "+");
  64. byte[] cipherBytes = Convert.FromBase64String(cipherText);
  65. using (Aes encryptor = Aes.Create())
  66. {
  67. Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
  68. encryptor.Key = pdb.GetBytes(32);
  69. encryptor.IV = pdb.GetBytes(16);
  70. using (MemoryStream ms = new MemoryStream())
  71. {
  72. using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
  73. {
  74. cs.Write(cipherBytes, 0, cipherBytes.Length);
  75. cs.Close();
  76. }
  77. cipherText = Encoding.Unicode.GetString(ms.ToArray());
  78. }
  79. }
  80. return cipherText;
  81. }
  82. catch (Exception ex)
  83. {
  84. return "";
  85. }
  86. }
  87. }
  88. }