0

I want to store data from textbox into database but it should be encrypted and after that displaying those data by some searching key i used a method for encrypting and its work fine the main problem is displaying data decrypted

 string encClass = AESencDec.Decrypt(txt_class.Text);
 SqlConnection connection = new SqlConnection(ConnectionString);
        SqlCommand command = new SqlCommand("select * from nuclear where Class='" + encClass + "'", connection);
        connection.Open();
        command.CommandType = CommandType.Text;
        command.ExecuteNonQuery();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter();                             
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        connection.Close();

it actually reads data from database and display but i want to know how can i show it decrypted ???

this is Decrypt class that I used for this project

 public static string Decrypt(string text)

    {
        string hash = "f0xle@rn";
        byte[] plaintextbytes = Convert.FromBase64String(text);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
            using (TripleDESCryptoServiceProvider triples = new TripleDESCryptoServiceProvider() { Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
            {
                ICryptoTransform transform = triples.CreateDecryptor();
                byte[] results = transform.TransformFinalBlock(plaintextbytes, 0, plaintextbytes.Length);
                return UTF8Encoding.UTF8.GetString(results);
            }
        }

    }

and this is encryption function

 public static string Encrypt(string text)
    {
         string hash = "f0xle@rn";
         byte[] plaintextbytes = UTF8Encoding.UTF8.GetBytes(text);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] keys = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
                using (TripleDESCryptoServiceProvider triples = new TripleDESCryptoServiceProvider() {Key = keys, Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 })
            {
                ICryptoTransform transform = triples.CreateEncryptor();
                byte[] results = transform.TransformFinalBlock(plaintextbytes, 0, plaintextbytes.Length);
                return Convert.ToBase64String(results);
            }
        }
    }
Emad mohseny
  • 13
  • 1
  • 6

1 Answers1

0

Here is an example that shows updating the datatable using test data. You can update it to use whatever the column names in your database are:

    int rowCount = 5;
    DataTable table = new DataTable();
    table.Columns.Add(new DataColumn("id", typeof(String)));
    table.Columns.Add(new DataColumn("encrypted", typeof(String)));
    table.Columns.Add(new DataColumn("decrypted", typeof(String)));

    //Write test encrypted data to data table
    for(int i = 0; i < rowCount; i++) 
    {
        string clearText = "test" + i.ToString();
        string cipherText = Encrypt(clearText);
        table.Rows.Add(new object[] {i.ToString(), cipherText, ""});
    }

    //Decrypt each item, and assign result to decrypted column
    foreach (DataRow row in table.Rows) 
    {
        row["decrypted"] = Decrypt(row["encrypted"].ToString());
    }

So your data adapter populates a datatable. You can just iterate the rows and decrypt before applying it to the data source:

da.Fill(dt);

foreach(DataRow row in dt.Rows)
{
    row["name of encrypted column"] = Decrypt(row["name of encrypted column"].ToString());
}

dataGridView1.DataSource = dt;
Ehz
  • 2,027
  • 1
  • 12
  • 11
  • I update the database by sqlcommand . it wasn't really useful code , I just wanna to display all I have in database (decrypted) , do you have any idea ? – Emad mohseny May 16 '17 at 19:53
  • I used your method but I got problem with invalid length base-64 array or string , do you know what is the problem ? – Emad mohseny May 17 '17 at 18:59
  • A base 64 string should have a length that is a multiple of 4. There are equals sign padding at the end to keep the expected length. It is possible that part of your process is stripping out the padding? http://stackoverflow.com/questions/2925729/invalid-length-for-a-base-64-char-array – Ehz May 17 '17 at 21:06
  • I couldn't find useful code , would you help me more ,thank you. – Emad mohseny May 18 '17 at 08:38