a legacy application that I am working on, returns a huge dataset on a grid (dont ask why) and also has a feature to export it to excel.
I am encrypting(using C# methods : code below) some of that data in databse and decrypting it while the dataset is read in grid on that page. Earlier the report was generated in around 15 seconds but now it just times out and keeps on going..
It seems to be because of decryption happening on all those records, is there a better way to do that?
#region Encryption-Decryption Methods
public static string Encrypt(string TextFromForm)
{
byte[] encryptedBytes = null;
byte[] BytesToBeEncrypted = null;
if (TextFromForm != null)
{
BytesToBeEncrypted = Encoding.Unicode.GetBytes(TextFromForm);
}
string EncryptionKeyPassword = "********";
// The salt bytes must be at least 8 bytes/ setting salt is mandatory for AES
byte[] saltBytes = Encoding.ASCII.GetBytes("********");
try
{
using (MemoryStream ms = new MemoryStream())
{
using (AesManaged AES = new AesManaged())
{
AES.Padding = PaddingMode.PKCS7;
AES.KeySize = 256;
AES.BlockSize = 128; //half of key size : can be changed if needed
// we can pass the key as a parameter to the method.
var key = new Rfc2898DeriveBytes(EncryptionKeyPassword, saltBytes); // generates the key with the password and salt
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC; // Cipher block Chaining
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(BytesToBeEncrypted, 0, BytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return System.Text.Encoding.GetEncoding(1252).GetString(encryptedBytes); // returning string from encrypted bytes
}
catch (Exception ex)
{
string encryptionmessage = ex.Message;
}
return System.Text.Encoding.GetEncoding(1252).GetString(encryptedBytes);
}
public static string Decrypt(string EncryptedText)
{
byte[] decryptedBytes = null;
string EncryptionKeyPassword = "*********";
byte[] BytesToBeDecrypted = null;
if (String.IsNullOrEmpty(EncryptedText))
{
EncryptedText = "";
}
BytesToBeDecrypted = System.Text.Encoding.GetEncoding(1252).GetBytes(EncryptedText); // decoding bytes from encrypted text
// The salt bytes must be at least 8 bytes. setting salt is mandatory in AES
byte[] saltBytes = Encoding.ASCII.GetBytes("*********");
try
{
using (MemoryStream ms = new MemoryStream())
{
using (AesManaged AES = new AesManaged())
{
AES.Padding = PaddingMode.PKCS7;
AES.KeySize = 256;
AES.BlockSize = 128;
// we can pass the key as a parameter to the method instead of generating.
var key = new Rfc2898DeriveBytes(EncryptionKeyPassword, saltBytes); // generates the key with the password and salt
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(BytesToBeDecrypted, 0, BytesToBeDecrypted.Length);
cs.Close();
}
decryptedBytes = ms.ToArray();
}
}
string TextFromDB = Encoding.Unicode.GetString(decryptedBytes);
return TextFromDB;
}
catch (Exception ex)
{
string decryptionmessage = ex.Message;
}
return string.Empty;
}
#endregion
Thanks All, Actually I had been generating the Key and IV in the method of Encryption and Decryption everytime they were called, so it was taking a lot of time to execute. By generating the key at session_start, I was able to use it everywhere and reduce encryption and decryption time.