Beispiel #1
0
        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;
            }
        }