c#通過(guò)DES加密算法加密大文件的方法
本文實(shí)例講述了c#通過(guò)DES加密算法加密大文件的方法。分享給大家供大家參考。具體實(shí)現(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;//儲(chǔ)存文件路徑
private string filePathB;//儲(chǔ)存文件復(fù)制后的路徑
/// <summary>
/// 文件加密
/// </summary>
/// <param name="inFile">文件儲(chǔ)存路徑</param>
/// <param name="outFile">儲(chǔ)存文件復(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];//放臨時(shí)讀入的流
long readLen = 0;//讀入流的長(zhǎng)度
long totalLen = inFs.Length;//讀入流的總長(zhǎng)度
int everylen=0;//每次讀入流的長(zhǎng)度
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];//放臨時(shí)讀入的流
long readLen = 0;//讀入流的長(zhǎng)度
long totalLen = inFs.Length;//讀入流的總長(zhǎng)度
int everylen=0;//每次讀入流的長(zhǎng)度
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);
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#中數(shù)組初始化、反轉(zhuǎn)和排序用法實(shí)例
這篇文章主要介紹了C#中數(shù)組初始化、反轉(zhuǎn)和排序用法,涉及C#中數(shù)組常見(jiàn)的定義、初始化、排序等操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
基于WPF實(shí)現(xiàn)經(jīng)典紙牌游戲
這篇文章主要為大家詳細(xì)介紹了如何溧陽(yáng)WPF實(shí)現(xiàn)經(jīng)典紙牌游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)WPF有一定的幫助,需要的可以參考一下2023-02-02
C# Pointer指針應(yīng)用實(shí)例簡(jiǎn)述
這篇文章主要介紹了C# Pointer指針應(yīng)用,對(duì)初學(xué)者很有借鑒學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-08-08
C#中事件的動(dòng)態(tài)調(diào)用實(shí)現(xiàn)方法
這篇文章主要介紹了C#中事件的動(dòng)態(tài)調(diào)用實(shí)現(xiàn)方法,對(duì)比傳統(tǒng)思路優(yōu)劣給出了一個(gè)新的解決方案,需要的朋友可以參考下2014-09-09
C#實(shí)現(xiàn)23種常見(jiàn)的設(shè)計(jì)模式的示例詳解
設(shè)計(jì)模式通常分為三個(gè)主要類別:創(chuàng)建型模式、結(jié)構(gòu)型模式和行為型模式,這些模式是用于解決常見(jiàn)的對(duì)象導(dǎo)向設(shè)計(jì)問(wèn)題的最佳實(shí)踐,本文為大家整理了23種常見(jiàn)的設(shè)計(jì)模式的實(shí)現(xiàn)代碼,需要的可以參考一下2023-06-06
C# Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)圓角無(wú)鋸齒按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

