• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

c#如何使用DES加密算法加密大文件

OC/C/C++ 水墨上仙 1391次浏览

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;//储存文件复制后的路径
     /// <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);
     }
 }


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明c#如何使用DES加密算法加密大文件
喜欢 (0)
加载中……