clsCustomEncryptionbk.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. namespace CommonObj.common
  8. {
  9. public sealed class clsCustomEncryptionbk
  10. {
  11. //private static string key = "sfdjf48mdfdf3054";
  12. //private static string key = "sfdjf48mdfdf3054123";
  13. public clsCustomEncryptionbk()
  14. {
  15. }
  16. /// <summary>
  17. /// Mã hóa dựa trên thuật toán mã hóa 1 chiều MD5,
  18. /// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
  19. /// </summary>
  20. /// <param name="plainText">Chuỗi cần mã hóa</param>
  21. /// <returns></returns>
  22. public static string EncryptUseMD5(string plainText)
  23. {
  24. string encrypted = null;
  25. MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
  26. byte[] pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(plainText));
  27. encrypted = Convert.ToBase64String(pwdhash);
  28. return encrypted;
  29. }
  30. /// <summary>
  31. /// 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,
  32. /// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
  33. /// </summary>
  34. /// <param name="plainText">Chuỗi cần mã hóa</param>
  35. /// <returns></returns>
  36. public static string Encrypt(string plainText,string key)
  37. {
  38. string encrypted = null;
  39. try
  40. {
  41. byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
  42. byte[] pwdhash = null;
  43. MD5CryptoServiceProvider hashmd5;
  44. //generate an MD5 hash from the password.
  45. //a hash is a one way encryption meaning once you generate
  46. //the hash, you cant derive the password back from it.
  47. hashmd5 = new MD5CryptoServiceProvider();
  48. pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
  49. hashmd5 = null;
  50. // Create a new TripleDES service provider
  51. TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
  52. tdesProvider.Key = pwdhash;
  53. tdesProvider.Mode = CipherMode.ECB;
  54. encrypted = Convert.ToBase64String(
  55. tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
  56. }
  57. catch (Exception e)
  58. {
  59. string str = e.Message;
  60. throw;
  61. }
  62. return encrypted;
  63. }
  64. /// <summary>
  65. /// Thực hiện giải mã một xâu ký tự
  66. /// </summary>
  67. /// <param name="encryptedString">Chuỗi đã mã hóa</param>
  68. /// <returns></returns>
  69. public static string Decrypt(string encryptedString,string key)
  70. {
  71. string decyprted = null;
  72. byte[] inputBytes = null;
  73. try
  74. {
  75. inputBytes = Convert.FromBase64String(encryptedString);
  76. byte[] pwdhash = null;
  77. MD5CryptoServiceProvider hashmd5;
  78. //generate an MD5 hash from the password.
  79. //a hash is a one way encryption meaning once you generate
  80. //the hash, you cant derive the password back from it.
  81. hashmd5 = new MD5CryptoServiceProvider();
  82. pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
  83. hashmd5 = null;
  84. // Create a new TripleDES service provider
  85. TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
  86. tdesProvider.Key = pwdhash;
  87. tdesProvider.Mode = CipherMode.ECB;
  88. decyprted = ASCIIEncoding.ASCII.GetString(
  89. tdesProvider.CreateDecryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
  90. }
  91. catch
  92. {
  93. decyprted = "";
  94. }
  95. return decyprted;
  96. }
  97. }
  98. }