最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年05月14日 10:21:40   作者:dingding  
這篇文章主要介紹了c#通過DES加密算法加密大文件的方法,涉及C#文件操作與DES加密的相關(guān)技巧,需要的朋友可以參考下

本文實例講述了c#通過DES加密算法加密大文件的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

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;//儲存文件復(fù)制后的路徑
  /// <summary>
  /// 文件加密
  /// </summary>
  /// <param name="inFile">文件儲存路徑</param>
  /// <param name="outFile">儲存文件復(fù)制的路徑</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;//重新設(shè)置文件名
   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#程序設(shè)計有所幫助。

相關(guān)文章

最新評論

侯马市| 澜沧| 孟津县| 天水市| 平乡县| 陵水| 顺义区| 绵阳市| 丹阳市| 日土县| 通许县| 阆中市| 柏乡县| 阜阳市| 琼海市| 尖扎县| 桃园县| 灌云县| 太和县| 洪雅县| 登封市| 灵川县| 莲花县| 福建省| 黄梅县| 宁化县| 马边| 镇康县| 远安县| 杨浦区| 曲阳县| 申扎县| 延津县| 陵川县| 阳朔县| 阳山县| 景宁| 博罗县| 淳安县| 兰坪| 柳州市|