using System.Collections; |
002 |
using System.Configuration; |
003 |
using System.Data; |
004 |
using System.Linq; |
005 |
using System.Web; |
006 |
using System.Web.Security; |
007 |
using System.Web.UI; |
008 |
using System.Web.UI.HtmlControls; |
009 |
using System.Web.UI.WebControls; |
010 |
using System.Web.UI.WebControls.WebParts; |
011 |
using System.Xml.Linq; |
012 |
using System.Text; |
013 |
using System.IO; |
014 |
using System.Security.Cryptography; |
015 |
016 |
public partial class Default2 : System.Web.UI.Page |
017 |
{ |
018 |
protected void Page_Load( object sender, EventArgs e) |
019 |
{ |
020 |
021 |
} |
022 |
private static byte [] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; //自定义密匙 |
023 |
private string filePathA; //储存文件路径 |
024 |
private string filePathB; //储存文件复制后的路径 |
025 |
/// <summary> |
026 |
/// 文件加密 |
027 |
/// </summary> |
028 |
/// <param name="inFile">文件储存路径</param> |
029 |
/// <param name="outFile">储存文件复制的路径</param> |
030 |
/// <param name="encryptKey"></param> |
031 |
/// <returns></returns> |
032 |
public bool EncryptDES( string inFile, string outFile, string encryptKey) |
033 |
{ |
034 |
byte [] rgb = Keys; |
035 |
try |
036 |
{ |
037 |
byte [] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); |
038 |
FileStream inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read); //读入流 |
039 |
FileStream outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write); // 等待写入流 |
040 |
outFs.SetLength(0); //帮助读写的变量 |
041 |
byte [] byteIn = new byte [100]; //放临时读入的流 |
042 |
long readLen = 0; //读入流的长度 |
043 |
long totalLen = inFs.Length; //读入流的总长度 |
044 |
int everylen=0; //每次读入流的长度 |
045 |
DES des = new DESCryptoServiceProvider(); //将inFile加密后放到outFile |
046 |
CryptoStream encStream = new CryptoStream(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 |
return true ; //加密成功 |
057 |
} |
058 |
catch (Exception ex) |
059 |
{ |
060 |
Response.Write(ex.Message.ToString()); |
061 |
return false ; //加密失败 |
062 |
} |
063 |
} |
064 |
065 |
066 |
public bool DecryptDES( string inFile, string outFile, string encryptKey) |
067 |
{ |
068 |
byte [] rgb = Keys; |
069 |
try |
070 |
{ |
071 |
byte [] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); |
072 |
FileStream inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read); //读入流 |
073 |
FileStream outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write); // 等待写入流 |
074 |
outFs.SetLength(0); //帮助读写的变量 |
075 |
byte [] byteIn = new byte [100]; //放临时读入的流 |
076 |
long readLen = 0; //读入流的长度 |
077 |
long totalLen = inFs.Length; //读入流的总长度 |
078 |
int everylen=0; //每次读入流的长度 |
079 |
DES des = new DESCryptoServiceProvider(); //将inFile加密后放到outFile |
080 |
CryptoStream encStream = new CryptoStream(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 |
return true ; //加密成功 |
091 |
} |
092 |
catch (Exception ex) |
093 |
{ |
094 |
Response.Write(ex.Message.ToString()); |
095 |
return false ; //加密失败 |
096 |
} |
097 |
} |
098 |
/// <summary> |
099 |
/// 拷贝文件 |
100 |
/// </summary> |
101 |
public void copyFile() |
102 |
{ |
103 |
filePathA = this .fei.PostedFile.FileName; //获取文件全部路径 |
104 |
string fileName = this .fei.FileName; |
105 |
string path = System.IO.Path.GetDirectoryName(filePathA); |
106 |
filePathB = path + "\\1" + fileName; //重新设置文件名 |
107 |
File.Copy(filePathA, filePathB); |
108 |
} |
109 |
110 |
protected void btnOK_Click( object sender, 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 |
protected void btnCancel_Click( object sender, 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 | using System; |
11 | using System.IO; |
12 | using System.Text; |
13 | using System.Security.Cryptography; |
14 |
15 | namespace Cjf.Components.Security |
16 | { |
17 |
/// <summary> |
18 |
/// DES加密解密字符串 |
19 |
/// </summary> |
20 |
public class DesEncryption |
21 |
{ |
22 |
//默认密钥向量 |
23 |
private static byte [] 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 |
public static string EncryptDES( string encryptString, string encryptKey = "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 = new DESCryptoServiceProvider(); |
39 |
MemoryStream mStream = new MemoryStream(); |
40 |
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); |
41 |
cStream.Write(inputByteArray, 0, inputByteArray.Length); |
42 |
cStream.FlushFinalBlock(); |
43 |
return Convert.ToBase64String(mStream.ToArray()); |
44 |
} |
45 |
catch |
46 |
{ |
47 |
return null ; |
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 |
public static string DecryptDES( string decryptString, string decryptKey = "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 = new DESCryptoServiceProvider(); |
65 |
MemoryStream mStream = new MemoryStream(); |
66 |
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); |
67 |
cStream.Write(inputByteArray, 0, inputByteArray.Length); |
68 |
cStream.FlushFinalBlock(); |
69 |
return Encoding.UTF8.GetString(mStream.ToArray()); |
70 |
} |
71 |
catch |
72 |
{ |
73 |
return null ; |
74 |
} |
75 |
} |
76 |
} |
77 | } |