如下的内容是关于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)
{
}
public bool EncryptDES(string inFile, string outFile, string encryptKey)
{
byte[] rgb = Keys;
try
{
byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
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();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
public bool DecryptDES(string inFile, string outFile, string encryptKey)
{
byte[] rgb = Keys;
try
{
byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
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();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
public void copyFile()
{
string fileName = this.fei.FileName;
string path = System.IO.Path.GetDirectoryName(filePathA);
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);
}
}
网友评论