解密和下载我在C#中使用AES加密的文件时出现问题 [英] Problem while decrypt and downloading files that I encrypt using AES in C#

查看:164
本文介绍了解密和下载我在C#中使用AES加密的文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





我尝试了什么: < br $>




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.

推荐答案

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));
响应。 AddHeader(Content-Disposition,attachment; filename = myfile+ Path.GetExtension(filePath));
int data;
while((data = cs.ReadByte())!= -1 )
{
Response.OutputStream.WriteByte((字节)的数据);

Response.Flush();

}
cs.Close();
fs.Close();
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
最后
{
cs.Close();
fs.Close();
}
}
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(); } }





我尝试了什么: < br $>




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.


这篇关于解密和下载我在C#中使用AES加密的文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆