ASP.NET - How to Decrypt & loop through each and every row in GridView RowDataBound in asp.net c#

Asked By Venkatesh Desai on 13-Mar-17 07:00 AM
Hi All,

How can I all loop throw the values in the gridview and decrypt them all and display it in the gridview decrypted.

my code goes like this

public string Decrypt(string sEncryptText)
{
            if (sEncryptText.Length == 0)
                return (sEncryptText);
            return (DecryptString(sEncryptText, sKey));
}

protected static string DecryptString(string InputText, string Password)
{
            RijndaelManaged RijndaelCipher = new RijndaelManaged();
            byte[] EncryptedData = Convert.FromBase64String(InputText.Replace(" ", "+"));
            byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
            ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(16), SecretKey.GetBytes(16));
            MemoryStream memoryStream = new MemoryStream(EncryptedData);
            CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
            byte[] PlainText = new byte[EncryptedData.Length];
            int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
            memoryStream.Close();
            cryptoStream.Close();
            string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
            return DecryptedData;
}

protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)         { foreach (TableCell cell in e.Row.Cells)
                {
Label lblEMPNAME = (Label)cell.FindControl("lblEMPNAME");
                string EName = lblEMPNAME.Text.ToString();
                lblEMPNAME.Text = Decrypt(EName);
}
}
}

For the first row, 

In Ename it is showing the value "ffipl1B4CN8ICBenKzFHrjm42C0NtOQbBVaevprGrSU="
In lblEMPNAME.Text it is showing the value "xyz" For the second row, In Ename it is showing the value "xyz" When it came to the next line i.e., lblEMPNAME.Text = objED.Decrypt(EName); it is getting the below error Invalid length for a Base-64 char array or string.