Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Encrypt function:
- public static string EncryptData(string Message)
- {
- byte[] Results;
- System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
- MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
- byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
- TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
- TDESAlgorithm.Key = TDESKey;
- TDESAlgorithm.Mode = CipherMode.ECB;
- TDESAlgorithm.Padding = PaddingMode.PKCS7;
- byte[] DataToEncrypt = UTF8.GetBytes(Message);
- try
- {
- ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
- Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
- }
- finally
- {
- TDESAlgorithm.Clear();
- HashProvider.Clear();
- }
- return Convert.ToBase64String(Results);
- }
- Decrypt function:
- public static string DecryptString(string Message)
- {
- byte[] Results;
- System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
- MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
- byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(passphrase));
- TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
- TDESAlgorithm.Key = TDESKey;
- TDESAlgorithm.Mode = CipherMode.ECB;
- TDESAlgorithm.Padding = PaddingMode.PKCS7;
- byte[] DataToDecrypt = Convert.FromBase64String(Message);
- try
- {
- ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
- Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
- }
- finally
- {
- TDESAlgorithm.Clear();
- HashProvider.Clear();
- }
- return UTF8.GetString(Results);
- }
- Note: read more in http://www.codeproject.com/Tips/306620/Encryption-Decryption-Function-in-Net-using-MD-Cry
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement