ASP.NET 上传文件加密问题

qjmay860909 2010-11-22 03:48:49
用对称加密对将要上传得文件进行加密,在下载时先对文件进行解密,然后下载
备注:如下为加密算法
/// <summary>
/// 加密指定的文件,如果成功返回True,否则false
/// </summary>
/// <param name="filePath">要加密的文件路径</param>
/// <param name="outPath">加密后的文件输出路径</param>
public void EncryptFile(string filePath, string outPath)
{
bool isExist = File.Exists(filePath);
if (isExist)//如果存在
{
byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
//得到要加密文件的字节流
FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fin, this.EncodingMode);
string dataStr = reader.ReadToEnd();
byte[] toEncrypt = this.EncodingMode.GetBytes(dataStr);
fin.Close();

FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
ICryptoTransform encryptor = des.CreateEncryptor(keyb, ivb);
CryptoStream csEncrypt = new CryptoStream(fout, encryptor, CryptoStreamMode.Write);
try
{
//加密得到的文件字节流
csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
csEncrypt.FlushFinalBlock();
}
catch (Exception err)
{
throw new ApplicationException(err.Message);
}
finally
{
try
{
fout.Close();
csEncrypt.Close();
}
catch
{
;
}
}
}
else
{
throw new FileNotFoundException("没有找到指定的文件");
}
}

解密算法如下
 /// <summary>
/// 解密指定的文件
/// </summary>
/// <param name="filePath">要解密的文件路径</param>
/// <param name="outPath">解密后的文件输出路径</param>
public void DecryptFile(string filePath, string outPath)
{
bool isExist = File.Exists(filePath);
if (isExist)//如果存在
{
byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
FileInfo file = new FileInfo(filePath);
byte[] deCrypted = new byte[file.Length];
//得到要解密文件的字节流
FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
//解密文件
try
{
ICryptoTransform decryptor = des.CreateDecryptor(keyb, ivb);
CryptoStream csDecrypt = new CryptoStream(fin, decryptor, CryptoStreamMode.Read);
csDecrypt.Read(deCrypted, 0, deCrypted.Length);
}
catch (Exception err)
{
throw new ApplicationException(err.Message);
}
finally
{
try
{
fin.Close();
}
catch { ;}
}
FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
fout.Write(deCrypted, 0, deCrypted.Length);
fout.Close();
}
else
{
throw new FileNotFoundException("指定的解密文件没有找到");
}
}

为什么我加密.txt文本文件上传下载没有错误,但是图片文件,压缩包或者office文件就会提示文件被破坏,求解!!
...全文
368 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyfy123 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 truecoffeefox 的回复:]
一般是数组的问题,或者编码
这个是正常的,环境win7_chs,nf4,vs2010
public void DecryptFile(string filePath, string savePath, string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (str……
[/Quote]

为什么??你在解密的时候使用的还是
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
呢?
这个不是加密么?
truecoffeefox 2010-11-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wangsunjun 的回复:]

引用 6 楼 truecoffeefox 的回复:
引用 3 楼 wangsunjun 的回复:

Byte[]数组错位了,加密算法不支持图片文件

des加密咋能不支持图片加密呢,加密对象是文件,和啥类型无关吧


加密是可以加密,但解密过来之后,就不是原来的样子了
[/Quote]

那和不能加密没有区别啊,des不是md5,不能保证文件完整性那就说明加密出现异常
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 truecoffeefox 的回复:]
引用 3 楼 wangsunjun 的回复:

Byte[]数组错位了,加密算法不支持图片文件

des加密咋能不支持图片加密呢,加密对象是文件,和啥类型无关吧
[/Quote]

加密是可以加密,但解密过来之后,就不是原来的样子了
truecoffeefox 2010-11-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangsunjun 的回复:]

Byte[]数组错位了,加密算法不支持图片文件
[/Quote]
des加密咋能不支持图片加密呢,加密对象是文件,和啥类型无关吧
truecoffeefox 2010-11-22
  • 打赏
  • 举报
回复
一般是数组的问题,或者编码
这个是正常的,环境win7_chs,nf4,vs2010
public void DecryptFile(string filePath, string savePath, string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (string.IsNullOrEmpty(keyStr))
{
keyStr = this.key;
}
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
byte[] hb = new SHA1Managed().ComputeHash(keyByteArray);
this.sKey = new byte[8];
this.sIV = new byte[8];
for (int i = 0; i < 8; i++)
{
this.sKey[i] = hb[i];
}
for (int i = 8; i < 0x10; i++)
{
this.sIV[i - 8] = hb[i];
}
des.Key = this.sKey;
des.IV = this.sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
}
public void EncryptFile(string filePath, string savePath, string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (string.IsNullOrEmpty(keyStr))
{
keyStr = this.key;
}
FileStream fs = File.OpenRead(filePath);
byte[] inputByteArray = new byte[fs.Length];
fs.Read(inputByteArray, 0, (int)fs.Length);
fs.Close();
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
byte[] hb = new SHA1Managed().ComputeHash(keyByteArray);
this.sKey = new byte[8];
this.sIV = new byte[8];
for (int i = 0; i < 8; i++)
{
this.sKey[i] = hb[i];
}
for (int i = 8; i < 0x10; i++)
{
this.sIV[i - 8] = hb[i];
}
des.Key = this.sKey;
des.IV = this.sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs = File.OpenWrite(savePath);
foreach (byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
}
  • 打赏
  • 举报
回复
Byte[]数组错位了,加密算法不支持图片文件
gongsun 2010-11-22
  • 打赏
  • 举报
回复
这个方法可能只对 文本文件有效果。
shen_wei 2010-11-22
  • 打赏
  • 举报
回复
不清楚。。。支持下 大头菜!!!

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧