c#通过DES加密算法加密大文件的方法

这篇文章主要介绍了c#通过DES加密算法加密大文件的方法,涉及C#文件操作与DES加密的相关技巧,需要的朋友可以参考下

本文实例讲述了c#通过DES加密算法加密大文件的方法。分享给大家供大家参考。具体实现方法如下:

 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;//储存文件复制后的路径 ///  /// 文件加密 ///  /// 文件储存路径 /// 储存文件复制的路径 ///  ///  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  /// 拷贝文件 ///  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", ""); } else { RegisterStartupScript("false", ""); } File.Delete(filePathB); } protected void btnCancel_Click(object sender, EventArgs e) { copyFile(); if (DecryptDES(filePathB, filePathA, "mingrisoft")) { RegisterStartupScript("false", ""); } else { RegisterStartupScript("false", ""); } File.Delete(filePathB); } }

希望本文所述对大家的C#程序设计有所帮助。

以上就是c#通过DES加密算法加密大文件的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程