解密和下载我在C#中使用AES加密的文件时出现问题

编程入门 行业动态 更新时间:2024-10-25 16:24:32
本文介绍了解密和下载我在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(); } }

我尝试了什么:

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

我尝试了什么:

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加密的文件时出现问题

本文发布于:2023-11-17 17:16:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我在   文件   AES

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!