国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

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

2020-01-24 01:52:02
字體:
來源:轉載
供稿:網友

本文實例講述了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 + "http://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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蕲春县| 内江市| 临沂市| 苏尼特右旗| 三河市| 门头沟区| 双江| 台中市| 定襄县| 昆明市| 肃宁县| 通河县| 那坡县| 北宁市| 井冈山市| 大新县| 梧州市| 盐城市| 武义县| 东乌珠穆沁旗| 福安市| 朔州市| 凤山市| 兴安县| 全州县| 黄石市| 文昌市| 开封市| 石首市| 红桥区| 屏山县| 荔浦县| 麦盖提县| 思茅市| 景德镇市| 浑源县| 镶黄旗| 斗六市| 阿图什市| 陕西省| 青神县|