c#通过DES加密算法加密大文件的方法
本文实例讲述了c#通过DES加密算法加密大文件的方法。分享给大家供大家参考。具体实现方法如下:
usingSystem.Collections;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingSystem.Text;
usingSystem.IO;
usingSystem.Security.Cryptography;
publicpartialclassDefault2:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
privatestaticbyte[]Keys={0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF};//自定义密匙
privatestringfilePathA;//储存文件路径
privatestringfilePathB;//储存文件复制后的路径
///<summary>
///文件加密
///</summary>
///<paramname="inFile">文件储存路径</param>
///<paramname="outFile">储存文件复制的路径</param>
///<paramname="encryptKey"></param>
///<returns></returns>
publicboolEncryptDES(stringinFile,stringoutFile,stringencryptKey)
{
byte[]rgb=Keys;
try
{
byte[]rgbKeys=Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
FileStreaminFs=newFileStream(inFile,FileMode.Open,FileAccess.Read);//读入流
FileStreamoutFs=newFileStream(outFile,FileMode.OpenOrCreate,FileAccess.Write);//等待写入流
outFs.SetLength(0);//帮助读写的变量
byte[]byteIn=newbyte[100];//放临时读入的流
longreadLen=0;//读入流的长度
longtotalLen=inFs.Length;//读入流的总长度
inteverylen=0;//每次读入流的长度
DESdes=newDESCryptoServiceProvider();//将inFile加密后放到outFile
CryptoStreamencStream=newCryptoStream(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();
returntrue;//加密成功
}
catch(Exceptionex)
{
Response.Write(ex.Message.ToString());
returnfalse;//加密失败
}
}
publicboolDecryptDES(stringinFile,stringoutFile,stringencryptKey)
{
byte[]rgb=Keys;
try
{
byte[]rgbKeys=Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
FileStreaminFs=newFileStream(inFile,FileMode.Open,FileAccess.Read);//读入流
FileStreamoutFs=newFileStream(outFile,FileMode.OpenOrCreate,FileAccess.Write);//等待写入流
outFs.SetLength(0);//帮助读写的变量
byte[]byteIn=newbyte[100];//放临时读入的流
longreadLen=0;//读入流的长度
longtotalLen=inFs.Length;//读入流的总长度
inteverylen=0;//每次读入流的长度
DESdes=newDESCryptoServiceProvider();//将inFile加密后放到outFile
CryptoStreamencStream=newCryptoStream(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();
returntrue;//加密成功
}
catch(Exceptionex)
{
Response.Write(ex.Message.ToString());
returnfalse;//加密失败
}
}
///<summary>
///拷贝文件
///</summary>
publicvoidcopyFile()
{
filePathA=this.fei.PostedFile.FileName;//获取文件全部路径
stringfileName=this.fei.FileName;
stringpath=System.IO.Path.GetDirectoryName(filePathA);
filePathB=path+"\\1"+fileName;//重新设置文件名
File.Copy(filePathA,filePathB);
}
protectedvoidbtnOK_Click(objectsender,EventArgse)
{
copyFile();
if(EncryptDES(filePathB,filePathA,"mingrisoft"))
{
RegisterStartupScript("false","<script>alert('加密成功!\\n');</script>");
}
else
{
RegisterStartupScript("false","<script>alert('失败成功!\\n');</script>");
}
File.Delete(filePathB);
}
protectedvoidbtnCancel_Click(objectsender,EventArgse)
{
copyFile();
if(DecryptDES(filePathB,filePathA,"mingrisoft"))
{
RegisterStartupScript("false","<script>alert('加密成功!\\n');</script>");
}
else
{
RegisterStartupScript("false","<script>alert('失败成功!\\n');</script>");
}
File.Delete(filePathB);
}
}
希望本文所述对大家的C#程序设计有所帮助。