DES加密算法加密大文件

usingSystem.Collections;
002 usingSystem.Configuration;
003 usingSystem.Data;
004 usingSystem.Linq;
005 usingSystem.Web;
006 usingSystem.Web.Security;
007 usingSystem.Web.UI;
008 usingSystem.Web.UI.HtmlControls;
009 usingSystem.Web.UI.WebControls;
010 usingSystem.Web.UI.WebControls.WebParts;
011 usingSystem.Xml.Linq;
012 usingSystem.Text;
013 usingSystem.IO;
014 usingSystem.Security.Cryptography;
015
016 publicpartialclassDefault2 : System.Web.UI.Page
017 {
018 protectedvoidPage_Load(objectsender, EventArgs e)
019 {
020
021 }
022 privatestaticbyte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//自定义密匙
023 privatestringfilePathA;//储存文件路径
024 privatestringfilePathB;//储存文件复制后的路径
025 /// <summary>
026 /// 文件加密
027 /// </summary>
028 /// <param name="inFile">文件储存路径</param>
029 /// <param name="outFile">储存文件复制的路径</param>
030 /// <param name="encryptKey"></param>
031 /// <returns></returns>
032 publicboolEncryptDES(stringinFile,stringoutFile,stringencryptKey)
033 {
034 byte[] rgb = Keys;
035 try
036 {
037 byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
038 FileStream inFs =newFileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
039 FileStream outFs =newFileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
040 outFs.SetLength(0);//帮助读写的变量
041 byte[] byteIn =newbyte[100];//放临时读入的流
042 longreadLen = 0;//读入流的长度
043 longtotalLen = inFs.Length;//读入流的总长度
044 inteverylen=0;//每次读入流的长度
045 DES des =newDESCryptoServiceProvider();//将inFile加密后放到outFile
046 CryptoStream encStream =newCryptoStream(outFs, des.CreateEncryptor(rgb, rgbKeys), CryptoStreamMode.Write);
047 while(readLen < totalLen)
048 {
049 everylen = inFs.Read(byteIn, 0, 100);
050 encStream.Write(byteIn, 0, everylen);
051 readLen = readLen + everylen;
052 }
053 encStream.Close();
054 inFs.Close();
055 outFs.Close();
056 returntrue;//加密成功
057 }
058 catch(Exception ex)
059 {
060 Response.Write(ex.Message.ToString());
061 returnfalse;//加密失败
062 }
063 }
064
065
066 publicboolDecryptDES(stringinFile,stringoutFile,stringencryptKey)
067 {
068 byte[] rgb = Keys;
069 try
070 {
071 byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
072 FileStream inFs =newFileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
073 FileStream outFs =newFileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
074 outFs.SetLength(0);//帮助读写的变量
075 byte[] byteIn =newbyte[100];//放临时读入的流
076 longreadLen = 0;//读入流的长度
077 longtotalLen = inFs.Length;//读入流的总长度
078 inteverylen=0;//每次读入流的长度
079 DES des =newDESCryptoServiceProvider();//将inFile加密后放到outFile
080 CryptoStream encStream =newCryptoStream(outFs, des.CreateDecryptor(rgb, rgbKeys), CryptoStreamMode.Write);
081 while(readLen < totalLen)
082 {
083 everylen = inFs.Read(byteIn, 0, 100);
084 encStream.Write(byteIn, 0, everylen);
085 readLen = readLen + everylen;
086 }
087 encStream.Close();
088 inFs.Close();
089 outFs.Close();
090 returntrue;//加密成功
091 }
092 catch(Exception ex)
093 {
094 Response.Write(ex.Message.ToString());
095 returnfalse;//加密失败
096 }
097 }
098 /// <summary>
099 /// 拷贝文件
100 /// </summary>
101 publicvoidcopyFile()
102 {
103 filePathA =this.fei.PostedFile.FileName;//获取文件全部路径
104 stringfileName =this.fei.FileName;
105 stringpath = System.IO.Path.GetDirectoryName(filePathA);
106 filePathB = path +"\\1"+ fileName;//重新设置文件名
107 File.Copy(filePathA, filePathB);
108 }
109
110 protectedvoidbtnOK_Click(objectsender, EventArgs e)
111 {
112 copyFile();
113 if(EncryptDES(filePathB, filePathA,"mingrisoft"))
114 {
115 RegisterStartupScript("false","<script>alert('加密成功!\\n');</script>");
116 }
117 else
118 {
119 RegisterStartupScript("false","<script>alert('失败成功!\\n');</script>");
120 }
121 File.Delete(filePathB);
122 }
123 protectedvoidbtnCancel_Click(objectsender, EventArgs e)
124 {
125 copyFile();
126 if(DecryptDES(filePathB, filePathA,"mingrisoft"))
127 {
128 RegisterStartupScript("false","<script>alert('加密成功!\\n');</script>");
129 }
130 else
131 {
132 RegisterStartupScript("false","<script>alert('失败成功!\\n');</script>");
133 }
134 File.Delete(filePathB);
135 }
136 }
01 /**********************************************************
02 * CJF Development Library for Microsoft.NET
03 * Note:DES加密解密字符串
04 * Create Date:2011-11-15
05 * Author:崔俊峰
06 * Copyright (c) 2012,2015 Cjf Studio.China
07 * All Rigths Reserved!
08 **********************************************************/
09
10 usingSystem;
11 usingSystem.IO;
12 usingSystem.Text;
13 usingSystem.Security.Cryptography;
14
15 namespaceCjf.Components.Security
16 {
17 /// <summary>
18 /// DES加密解密字符串
19 /// </summary>
20 publicclassDesEncryption
21 {
22 //默认密钥向量
23 privatestaticbyte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
24
25 /// <summary>
26 /// DES加密字符串
27 /// </summary>
28 /// <param name="encryptString">待加密的字符串</param>
29 /// <param name="encryptKey">加密密钥,要求为8位</param>
30 /// <returns>加密成功返回加密后的字符串,失败返回null</returns>
31 publicstaticstringEncryptDES(stringencryptString,stringencryptKey ="11001100")
32 {
33 try
34 {
35 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
36 byte[] rgbIV = Keys;
37 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
38 DESCryptoServiceProvider dCSP =newDESCryptoServiceProvider();
39 MemoryStream mStream =newMemoryStream();
40 CryptoStream cStream =newCryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
41 cStream.Write(inputByteArray, 0, inputByteArray.Length);
42 cStream.FlushFinalBlock();
43 returnConvert.ToBase64String(mStream.ToArray());
44 }
45 catch
46 {
47 returnnull;
48 }
49 }
50
51 /// <summary>
52 /// DES解密字符串
53 /// </summary>
54 /// <param name="decryptString">待解密的字符串</param>
55 /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
56 /// <returns>解密成功返回解密后的字符串,失败返回null</returns>
57 publicstaticstringDecryptDES(stringdecryptString,stringdecryptKey ="11001100")
58 {
59 try
60 {
61 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
62 byte[] rgbIV = Keys;
63 byte[] inputByteArray = Convert.FromBase64String(decryptString);
64 DESCryptoServiceProvider DCSP =newDESCryptoServiceProvider();
65 MemoryStream mStream =newMemoryStream();
66 CryptoStream cStream =newCryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
67 cStream.Write(inputByteArray, 0, inputByteArray.Length);
68 cStream.FlushFinalBlock();
69 returnEncoding.UTF8.GetString(mStream.ToArray());
70 }
71 catch
72 {
73 returnnull;
74 }
75 }
76 }
77 }

你可能感兴趣的:(des)