Member 12173614 Ответов: 0

Проблема при расшифровки и загрузки файлов, шифровать с помощью AES на C#


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();
           }
       }


Что я уже пробовал:

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.

F-ES Sitecore

После Ответа.Flush попробуйте сделать CompleteRequest тоже. Пример кода можно найти здесь

https://stackoverflow.com/questions/20988445/how-to-avoid-response-end-thread-was-being-aborted-exception-during-the-exce

Richard Deeming

Копирование одного байта за раз в ответ кажется отличным способом замедлить работу вашего приложения без всякой причины. :)

0 Ответов