DES加密算法加密大文件问题???

li3807 2010-02-09 02:39:48
使用DES加密算法加密大文件,加解密都写完了,但解密文件成功后,发现文件被损坏了,请问这是什么问题,是不是我有什么特别的地方需要处理的,请指教。
...全文
505 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fonlan 2010-02-21
  • 打赏
  • 举报
回复
不是所有文件都是你所用缓存长度的整数倍的,你的代码没有处理好最后那部分小于你的缓存的文件末尾
li3807 2010-02-09
  • 打赏
  • 举报
回复
算法应该没问题,在加密小于缓存的文件时就没有问题。
mwp 2010-02-09
  • 打赏
  • 举报
回复
我觉得是你算法有问题,你先加解密一段小文本试试
li3807 2010-02-09
  • 打赏
  • 举报
回复
fsRead = new FileStream(filePath, FileMode.Open, FileAccess.Read);
fsWrite = new FileStream(filePath + Properties.Resources.Ext, FileMode.OpenOrCreate, FileAccess.Write);

//写入版本信息
fsWrite.WriteByte(ver);

//写入Key和IV
fsWrite.Write(key, 0, key.Length);
fsWrite.Write(iv, 0, iv.Length);

//写入加密后文件信息
fileVal = new byte[buffLenght];
cStream = new CryptoStream(fsWrite, desc.CreateEncryptor(key, iv), CryptoStreamMode.Write);

while ((readCount = fsRead.Read(fileVal, 0, fileVal.Length)) != 0)
{
buff = new byte[readCount];
Array.Copy(fileVal, buff, readCount);
cStream.Write(buff, 0, buff.Length);
}

cStream.FlushFinalBlock();
return true;
这是我的代码,我在使用加密流写内容前,先写入了一些其它不用加密的信息
limii 2010-02-09
  • 打赏
  • 举报
回复
up回复内容太短了回复内容太短了!
xiaofeiVSmugua 2010-02-09
  • 打赏
  • 举报
回复
我加密解密都ok没问题的。是不是你哪里写错了哦
xiaofeiVSmugua 2010-02-09
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//自定义密匙
private string filePathA;//储存文件路径
private string filePathB;//储存文件复制后的路径
/// <summary>
/// 文件加密
/// </summary>
/// <param name="inFile">文件储存路径</param>
/// <param name="outFile">储存文件复制的路径</param>
/// <param name="encryptKey"></param>
/// <returns></returns>
public bool EncryptDES(string inFile, string outFile, string encryptKey)
{
byte[] rgb = Keys;
try
{
byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
FileStream inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
FileStream outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
outFs.SetLength(0);//帮助读写的变量
byte[] byteIn = new byte[100];//放临时读入的流
long readLen = 0;//读入流的长度
long totalLen = inFs.Length;//读入流的总长度
int everylen=0;//每次读入流的长度
DES des = new DESCryptoServiceProvider();//将inFile加密后放到outFile
CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgb, rgbKeys), CryptoStreamMode.Write);
while (readLen < totalLen)
{
everylen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everylen);
readLen = readLen + everylen;
}
encStream.Close();
inFs.Close();
outFs.Close();
return true;//加密成功
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
return false;//加密失败
}
}


public bool DecryptDES(string inFile, string outFile, string encryptKey)
{
byte[] rgb = Keys;
try
{
byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
FileStream inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
FileStream outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
outFs.SetLength(0);//帮助读写的变量
byte[] byteIn = new byte[100];//放临时读入的流
long readLen = 0;//读入流的长度
long totalLen = inFs.Length;//读入流的总长度
int everylen=0;//每次读入流的长度
DES des = new DESCryptoServiceProvider();//将inFile加密后放到outFile
CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgb, rgbKeys), CryptoStreamMode.Write);
while (readLen < totalLen)
{
everylen = inFs.Read(byteIn, 0, 100);
encStream.Write(byteIn, 0, everylen);
readLen = readLen + everylen;
}
encStream.Close();
inFs.Close();
outFs.Close();
return true;//加密成功
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
return false;//加密失败
}
}
/// <summary>
/// 拷贝文件
/// </summary>
public void copyFile()
{
filePathA = this.fei.PostedFile.FileName;//获取文件全部路径
string fileName = this.fei.FileName;
string path = System.IO.Path.GetDirectoryName(filePathA);
filePathB = path + "\\1" + fileName;//重新设置文件名
File.Copy(filePathA, filePathB);
}

protected void btnOK_Click(object sender, EventArgs e)
{
copyFile();
if (EncryptDES(filePathB, filePathA, "mingrisoft"))
{
RegisterStartupScript("false", "<script>alert('加密成功!\\n');</script>");
}
else
{
RegisterStartupScript("false", "<script>alert('失败成功!\\n');</script>");
}
File.Delete(filePathB);
}
protected void btnCancel_Click(object sender, EventArgs e)
{
copyFile();
if (DecryptDES(filePathB, filePathA, "mingrisoft"))
{
RegisterStartupScript("false", "<script>alert('加密成功!\\n');</script>");
}
else
{
RegisterStartupScript("false", "<script>alert('失败成功!\\n');</script>");
}
File.Delete(filePathB);
}
}
这是我写的也许对你有用。
li3807 2010-02-09
  • 打赏
  • 举报
回复
最后发现一个问题,我的缓存是409600字节,如果加密小于这个字节的文件就没有问题,如果大于这个缓存的话就会出现文件错误,所以猜想是由于分区块加密所造成的,该如何解决呢???
li3807 2010-02-09
  • 打赏
  • 举报
回复
我使用二进制查看二个文件的不同,发现存在一些地址的值是不同的,这会是什么原因造成的?
li3807 2010-02-09
  • 打赏
  • 举报
回复
加解密过程不报错,但对已解密文件进行使用时就会出现文件已损坏的报错,如对解密后的Zip文件进行解压就会出错。
lerit 2010-02-09
  • 打赏
  • 举报
回复
好像没有特别处理吧,你多大的时候开始报错

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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