/// <summary> /// A method to Encrypt/decrypt a specified Triple DES encrypted file using the /// key and iv provided. /// สำหรับ Encrypt ข้อมูล อื่นๆ ที่ไม่ใช้ Password สามารถถอดรหัสได้ /// </summary> private string EncryptData(string plainMessage, string password) { if (Varidation.IsEmpty(plainMessage)) { return(""); } try { password = GetEncoding(privateKey, password); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.IV = new byte[8]; PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]); des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); MemoryStream ms = new MemoryStream(plainMessage.Length * 2); CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); byte[] plainBytes = Encoding.UTF8.GetBytes(plainMessage); encStream.Write(plainBytes, 0, plainBytes.Length); encStream.FlushFinalBlock(); byte[] encryptedBytes = new byte[ms.Length]; ms.Position = 0; ms.Read(encryptedBytes, 0, (int)ms.Length); encStream.Close(); return(Convert.ToBase64String(encryptedBytes)); } catch (Exception e) { throw new Exception(e.Message); } }
/// <summary> /// A method to Encrypt/decrypt a specified Triple DES encrypted file using the /// key and iv provided. /// สำหรับ Decrypt ข้อมูล /// </summary> private string DecryptData(string encryptedBase64, string password) { if (Varidation.IsEmpty(encryptedBase64)) { return(""); } try { password = GetEncoding(privateKey, password); TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.IV = new byte[8]; PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]); des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]); byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64); MemoryStream ms = new MemoryStream(encryptedBase64.Length); CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); decStream.Write(encryptedBytes, 0, encryptedBytes.Length); decStream.FlushFinalBlock(); byte[] plainBytes = new byte[ms.Length]; ms.Position = 0; ms.Read(plainBytes, 0, (int)ms.Length); decStream.Close(); return(Encoding.UTF8.GetString(plainBytes)); } catch (Exception e) { //throw new NotImplementedException(); //return Redirect(res.Pageviews.PvNotFound); //throw new Exception("Can't convert because data type of column " + columnDestination + " is not String!"); throw new Exception(e.Message); } }
/// <summary> /// ส่งข้อมูลที่ทำการแปลงข้อมูลให้อยู่ใน รูปของการ Encode แล้ว /// </summary> string Authenticate(string userPwd) { if (Varidation.IsEmpty(userPwd)) { return(""); } // Should we create a hash for the password? if (userPwd.Length > 0) { userPwd = HashData(userPwd); } return(userPwd); }