Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
protected void Button1_Click(object sender, EventArgs e)
       {
           string filename = DropDownList1.SelectedItem.Value;
           string filePath = Path.Combine(Server.MapPath("~/cloudfiles"), filename);
           // key for decryption
           byte[] Key = Encoding.UTF8.GetBytes("asdf!@#$1234ASDF");

           //UnicodeEncoding ue = new UnicodeEncoding();
           FileStream fs = new FileStream(filePath, FileMode.Open);
           AesManaged rmCryp = new AesManaged();
           CryptoStream cs = new CryptoStream(fs, rmCryp.CreateDecryptor(Key, Key), CryptoStreamMode.Read);
           try
           {
               // Decrypt & Download Here
               Response.Clear();
               Response.ContentType = "application/octet-stream";
              // Response.AddHeader("Content-Disposition","attachment; filename=" + Path.GetFileName(filePath) + Path.GetExtension(filePath));
                Response.AddHeader("Content-Disposition", "attachment; filename=myfile" + Path.GetExtension(filePath));
               int data;
               while ((data = cs.ReadByte()) != -1)
               {
                   Response.OutputStream.WriteByte((byte)data);

                   Response.Flush();

               }
               cs.Close();
               fs.Close();
           }
           catch (Exception ex)
           {
               Response.Write(ex.Message);
           }
           finally
           {
               cs.Close();
               fs.Close();
           }
       }


What I have tried:

Hi all

For my research work im doing about a access control model and it uses AES symmetric key encryption for encrypting the file while uploading and  decryption while downloading.As im a newbie i have used a available algorithm and when im downloading file it is larger than original file and it has a content of the web page also.As an example if i encrypt text file when im downloading it embedded with web page content.can any one help me with that ? here is my code for file decryption.
Posted
Updated 6-Nov-17 0:18am
v2
Comments
F-ES Sitecore 6-Nov-17 7:20am    
After Response.Flush try doing a CompleteRequest too. Sample code can be found here

https://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce
Richard Deeming 6-Nov-17 10:34am    
Copying one byte at a time to the response seems like a great way to slow your application down for no reason. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900