| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Security.Cryptography;
- using System.Text;
- using System.IO;
- namespace CommonObj.common
- {
- public sealed class CustomEncryption
- {
- //private static string key = "sfdjf48mdfdf3054";
- //private static string key = "sfdjf48mdfdf3054123";
- public CustomEncryption()
- {
- }
- /// <summary>
- /// Mã hóa dựa trên thuật toán mã hóa 1 chiều MD5,
- /// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
- /// </summary>
- /// <param name="plainText">Chuỗi cần mã hóa</param>
- /// <returns></returns>
- public static string EncryptUseMD5(string plainText)
- {
- string encrypted = null;
- MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
- byte[] pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(plainText));
- encrypted = Convert.ToBase64String(pwdhash);
- return encrypted;
- }
- /// <summary>
- /// 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,
- /// Sau khi mã hóa, sử dụng thuật toán encode Base64 để lưu dưới dạng Text
- /// </summary>
- /// <param name="plainText">Chuỗi cần mã hóa</param>
- /// <returns></returns>
- public static string Encrypt(string clearText, string EncryptionKey)
- {
- //string EncryptionKey = "abc123";
- byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
- using (Aes encryptor = Aes.Create())
- {
- Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
- encryptor.Key = pdb.GetBytes(32);
- encryptor.IV = pdb.GetBytes(16);
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cs.Write(clearBytes, 0, clearBytes.Length);
- cs.Close();
- }
- clearText = Convert.ToBase64String(ms.ToArray());
- }
- }
- return clearText;
- }
- public static string Decrypt(string cipherText, string EncryptionKey)
- {
- //string EncryptionKey = "abc123";
- try
- {
- cipherText = cipherText.Replace(" ", "+");
- byte[] cipherBytes = Convert.FromBase64String(cipherText);
- using (Aes encryptor = Aes.Create())
- {
- Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
- encryptor.Key = pdb.GetBytes(32);
- encryptor.IV = pdb.GetBytes(16);
- using (MemoryStream ms = new MemoryStream())
- {
- using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
- {
- cs.Write(cipherBytes, 0, cipherBytes.Length);
- cs.Close();
- }
- cipherText = Encoding.Unicode.GetString(ms.ToArray());
- }
- }
- return cipherText;
- }
- catch (Exception ex)
- {
- return "";
- }
- }
- }
- }
|