程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#經由過程DES加密算法加密年夜文件的辦法

c#經由過程DES加密算法加密年夜文件的辦法

編輯:C#入門知識

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;//貯存文件復制後的途徑
  /// <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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved