protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { Panel1.Visible = true; if (e.CommandName == "Decrypt") { int rowIndex = Convert.ToInt32(e.CommandArgument); string encrypted_text, decrypted_text, key; key = TextBox1.Text.ToString(); if (key == "") { Response.Write("<script>window.alert('Please enter a key!!');</script>"); return; } EncDecServiceReference.EncDecServiceClient ed_service = new EncDecServiceReference.EncDecServiceClient(); GridViewRow row = GridView1.Rows[rowIndex]; file_name = row.Cells[2].Text; string user = System.Web.HttpContext.Current.User.Identity.Name; SqlConnection conn = new SqlConnection(); conn.ConnectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; try { using (conn) { string query = "select TextToByte from UserFiles where UserName=@username and FilePath=@file_name"; SqlCommand cmd = new SqlCommand(query, conn); cmd.Parameters.AddWithValue("@username", user); cmd.Parameters.AddWithValue("@file_name", file_name); conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { encrypted_text = reader.GetValue(0).ToString(); decrypted_text = ed_service.DecryptData(key, encrypted_text); dec_data = decrypted_text.ToString(); } conn.Close(); } } catch (SqlException ex) { Response.Write(ex); } } }
protected void EncryptData(object sender, EventArgs e) { if (FileUpload1.HasFile == false) { Label2.Text = "please select a file"; Label2.ForeColor = System.Drawing.Color.Red; return; } string key; if (TextBox1.Text.Length != 32 || isAlphanumeric(TextBox1.Text.ToString()) == false) { Label1.Text = "key must be of 32 char long AlphaNumberic value"; Label1.ForeColor = System.Drawing.Color.Red; return; } else { key = TextBox1.Text.ToString(); } SqlConnection conn = new SqlConnection(); conn.ConnectionString = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString; try { using (conn) { string query = "insert into UserFiles(UserName, FilePath, TextToByte) values (@username, @filepath, @texttobyte)"; SqlCommand cmd = new SqlCommand(query, conn); EncDecServiceReference.EncDecServiceClient service = new EncDecServiceReference.EncDecServiceClient(); string file_content = null; using (StreamReader reader = new StreamReader(FileUpload1.PostedFile.InputStream)) { file_content = reader.ReadToEnd(); } string file_path = FileUpload1.FileName; string encrypted_text = service.EncryptData(key, file_content); cmd.Parameters.AddWithValue("@username", System.Web.HttpContext.Current.User.Identity.Name); cmd.Parameters.AddWithValue("@filepath", file_path); cmd.Parameters.AddWithValue("@texttobyte", encrypted_text); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); Label3.Text = "<h3> Your File has been successfully added to the database !!! </h3>"; Label3.ForeColor = System.Drawing.Color.Green; } } catch (SqlException err) { Label3.Text = "You cannot have multiple files with same name!!! "; Label3.ForeColor = System.Drawing.Color.Red; //Label3.Text += err.Message; } catch (System.ServiceModel.ProtocolException ex) { Label3.Text = "File Format not supported!"; Label3.ForeColor = System.Drawing.Color.Red; } }