Dear Experts,
I am working on encryption and decryption of string. I find the class on the internet but that class is not working. Please find the code below. Please let me know how to call this class on Button_Click Event. I already tried this link http://www.aspsnippets.com/Articles/AES-Encryption-Decryption-Cryptography-Tutorial-with-example-in-ASPNet-using-C-and-VBNet.aspx.
Thanks
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
/// <summary>
/// Summary description for CryptorEngine
/// </summary>
//public class CryptorEngine
//{
// public CryptorEngine()
// {
// public static string Encrypt(string toEncrypt, bool useHashing)
// {
// byte[] keyArray;
// byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
// System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// // Get the key from config file
// string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
// //System.Windows.Forms.MessageBox.Show(key);
// if (useHashing)
// {
// MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
// keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
// hashmd5.Clear();
// }
// else
// keyArray = UTF8Encoding.UTF8.GetBytes(key);
// TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
// tdes.Key = keyArray;
// tdes.Mode = CipherMode.ECB;
// tdes.Padding = PaddingMode.PKCS7;
// ICryptoTransform cTransform = tdes.CreateEncryptor();
// byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
// tdes.Clear();
// return Convert.ToBase64String(resultArray, 0, resultArray.Length);
// }
// /// <summary>
// /// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
// /// </summary>
// /// <param name="cipherString">encrypted string</param>
// /// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
// /// <returns></returns>
// public static string Decrypt(string cipherString, bool useHashing)
// {
// byte[] keyArray;
// byte[] toEncryptArray = Convert.FromBase64String(cipherString);
// System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// //Get your key from config file to open the lock!
// string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
// if (useHashing)
// {
// MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
// keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
// hashmd5.Clear();
// }
// else
// keyArray = UTF8Encoding.UTF8.GetBytes(key);
// TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
// tdes.Key = keyArray;
// tdes.Mode = CipherMode.ECB;
// tdes.Padding = PaddingMode.PKCS7;
// ICryptoTransform cTransform = tdes.CreateDecryptor();
// byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
// tdes.Clear();
// return UTF8Encoding.UTF8.GetString(resultArray);
// }
// }
namespace EnCryptDecrypt
{
public class CryptorEngine
{
/// <summary>
/// Encrypt a string using dual encryption method. Return a encrypted cipher Text
/// </summary>
/// <param name="toEncrypt">string to be encrypted</param>
/// <param name="useHashing">use hashing? send to for extra secirity</param>
/// <returns></returns>
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// Get the key from config file
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
//System.Windows.Forms.MessageBox.Show(key);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
/// </summary>
/// <param name="cipherString">encrypted string</param>
/// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
/// <returns></returns>
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
}