My application is MVC 5 using EF 6.2. I am decrypting certain columns while generating a list, it works but slow. Is there a better way to improve the performance of this approach?
var mylist = await _db.vw_LearnerCourse.AsNoTracking().ToListAsync();
var grid1 = mylist.Select(c => new
{
FirstName = Encryption.Decrypt5(c.FirstName),
LastName = Encryption.Decrypt5(c.LastName)
}).ToList();
public static string Decrypt5(string cipherText)
{
if (string.IsNullOrWhiteSpace(cipherText)) return null;
if (!string.IsNullOrWhiteSpace(cipherText))
{
string strOut;
try
{
var arrOffsets = new ArrayList();
arrOffsets.Insert(0, 73);
arrOffsets.Insert(1, 56);
arrOffsets.Insert(2, 31);
arrOffsets.Insert(3, 58);
arrOffsets.Insert(4, 77);
arrOffsets.Insert(5, 75);
strOut = "";
int intCounter;
for (intCounter = 0;
intCounter <= cipherText.Length - 1;
intCounter += 2)
{
var strSub = cipherText.Substring(intCounter, 1);
var strSub1 = cipherText.Substring(intCounter + 1, 1);
var intVal = int.Parse(strSub,
NumberStyles.HexNumber) * 16 + int.Parse(strSub1,
NumberStyles.HexNumber);
var intMod = intCounter / 2 % arrOffsets.Count;
var intNewVal = intVal -
Convert.ToInt32(arrOffsets[intMod]) + 256;
intNewVal = intNewVal % 256;
var strDecimal = ((char)intNewVal).ToString();
strOut = strOut + strDecimal;
}
}
catch (Exception err)
{
throw new Exception(err.Message);
}
var encryptionKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
cipherText = strOut;
cipherText = cipherText.Replace(" ", "+");
var cipherBytes = Convert.FromBase64String(cipherText);
using (var encryptor = Aes.Create())
{
var pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[]
{
xxxxxxx
});
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor.CreateDecryptor(),
CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
return null;
}