1
using
System;
2
using
System.IO;
3
using
System.Text;
4
using
System.Security.Cryptography;
5
6
namespace
ConsoleApplication1
7
{
8
public class DESEncryptor
9
{
10
私有成员#region 私有成员
11
/**//// <summary>
12
/// 输入字符串
13
/// </summary>
14
private string inputString=null;
15
/**//// <summary>
16
/// 输出字符串
17
/// </summary>
18
private string outString=null;
19
/**//// <summary>
20
/// 输入文件路径
21
/// </summary>
22
private string inputFilePath=null;
23
/**//// <summary>
24
/// 输出文件路径
25
/// </summary>
26
private string outFilePath=null;
27
/**//// <summary>
28
/// 加密密钥
29
/// </summary>
30
private string encryptKey=null;
31
/**//// <summary>
32
/// 解密密钥
33
/// </summary>
34
private string decryptKey=null;
35
/**//// <summary>
36
/// 提示信息
37
/// </summary>
38
private string noteMessage=null;
39
#endregion
40
41
公共属性#region 公共属性
42
/**//// <summary>
43
/// 输入字符串
44
/// </summary>
45
public string InputString
46
{
47
get
{return inputString;}
48
set
{inputString=value;}
49
}
50
/**//// <summary>
51
/// 输出字符串
52
/// </summary>
53
public string OutString
54
{
55
get
{return outString;}
56
set
{outString=value;}
57
}
58
/**//// <summary>
59
/// 输入文件路径
60
/// </summary>
61
public string InputFilePath
62
{
63
get
{return inputFilePath;}
64
set
{inputFilePath=value;}
65
}
66
/**//// <summary>
67
/// 输出文件路径
68
/// </summary>
69
public string OutFilePath
70
{
71
get
{return outFilePath;}
72
set
{outFilePath=value;}
73
}
74
/**//// <summary>
75
/// 加密密钥
76
/// </summary>
77
public string EncryptKey
78
{
79
get
{return encryptKey;}
80
set
{encryptKey=value;}
81
}
82
/**//// <summary>
83
/// 解密密钥
84
/// </summary>
85
public string DecryptKey
86
{
87
get
{return decryptKey;}
88
set
{decryptKey=value;}
89
}
90
/**//// <summary>
91
/// 错误信息
92
/// </summary>
93
public string NoteMessage
94
{
95
get
{return noteMessage;}
96
set
{noteMessage=value;}
97
}
98
#endregion
99
100
构造函数#region 构造函数
101
public DESEncryptor()
102
{
103
//
104
// TODO: 在此处添加构造函数逻辑
105
//
106
}
107
#endregion
108
109
DES加密字符串#region DES加密字符串
110
/**//// <summary>
111
/// 加密字符串
112
/// 注意:密钥必须为8位
113
/// </summary>
114
/// <param name="strText">字符串</param>
115
/// <param name="encryptKey">密钥</param>
116
public void DesEncrypt()
117
{
118
byte[] byKey = null;
119
byte[] IV =
{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
120
try
121
{
122
byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0, 8));
123
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
124
byte[] inputByteArray = Encoding.UTF8.GetBytes(this.inputString);
125
MemoryStream ms = new MemoryStream();
126
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
127
cs.Write(inputByteArray, 0, inputByteArray.Length);
128
cs.FlushFinalBlock();
129
this.outString = Convert.ToBase64String(ms.ToArray());
130
}
131
catch (System.Exception error)
132
{
133
this.noteMessage = error.Message;
134
}
135
}
136
#endregion
137
138
DES解密字符串#region DES解密字符串
139
/**//// <summary>
140
/// 解密字符串
141
/// </summary>
142
/// <param name="this.inputString">加了密的字符串</param>
143
/// <param name="decryptKey">密钥</param>
144
public void DesDecrypt()
145
{
146
byte[] byKey = null;
147
byte[] IV =
{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
148
byte[] inputByteArray = new Byte[this.inputString.Length];
149
try
150
{
151
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
152
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
153
inputByteArray = Convert.FromBase64String(this.inputString);
154
MemoryStream ms = new MemoryStream();
155
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
156
cs.Write(inputByteArray, 0, inputByteArray.Length);
157
cs.FlushFinalBlock();
158
System.Text.Encoding encoding = new System.Text.UTF8Encoding();
159
this.outString = encoding.GetString(ms.ToArray());
160
}
161
catch (System.Exception error)
162
{
163
this.noteMessage = error.Message;
164
}
165
}
166
#endregion
167
168
DES加密文件#region DES加密文件
169
/**//// <summary>
170
/// DES加密文件
171
/// </summary>
172
/// <param name="this.inputFilePath">源文件路径</param>
173
/// <param name="this.outFilePath">输出文件路径</param>
174
/// <param name="encryptKey">密钥</param>
175
public void FileDesEncrypt()
176
{
177
byte[] byKey = null;
178
byte[] IV =
{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
179
try
180
{
181
byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0, 8));
182
FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
183
FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
184
fout.SetLength(0);
185
//Create variables to help with read and write.
186
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
187
long rdlen = 0; //This is the total number of bytes written.
188
long totlen = fin.Length; //This is the total length of the input file.
189
int len; //This is the number of bytes to be written at a time.
190
DES des = new DESCryptoServiceProvider();
191
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
192
193
//Read from the input file, then encrypt and write to the output file.
194
while (rdlen < totlen)
195
{
196
len = fin.Read(bin, 0, 100);
197
encStream.Write(bin, 0, len);
198
rdlen = rdlen + len;
199
}
200
201
encStream.Close();
202
fout.Close();
203
fin.Close();
204
}
205
catch (System.Exception error)
206
{
207
this.noteMessage = error.Message.ToString();
208
}
209
}
210
#endregion
211
212
DES解密文件#region DES解密文件
213
/**//// <summary>
214
/// 解密文件
215
/// </summary>
216
/// <param name="this.inputFilePath">加密了的文件路径</param>
217
/// <param name="this.outFilePath">输出文件路径</param>
218
/// <param name="decryptKey">密钥</param>
219
public void FileDesDecrypt()
220
{
221
byte[] byKey = null;
222
byte[] IV =
{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
223
try
224
{
225
byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
226
FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
227
FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
228
fout.SetLength(0);
229
//Create variables to help with read and write.
230
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
231
long rdlen = 0; //This is the total number of bytes written.
232
long totlen = fin.Length; //This is the total length of the input file.
233
int len; //This is the number of bytes to be written at a time.
234
DES des = new DESCryptoServiceProvider();
235
CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
236
237
238
//Read from the input file, then encrypt and write to the output file.
239
while (rdlen < totlen)
240
{
241
len = fin.Read(bin, 0, 100);
242
encStream.Write(bin, 0, len);
243
rdlen = rdlen + len;
244
}
245
246
encStream.Close();
247
fout.Close();
248
fin.Close();
249
}
250
catch (System.Exception error)
251
{
252
this.noteMessage = error.Message.ToString();
253
}
254
}
255
#endregion
256
257
MD5#region MD5
258
/**//// <summary>
259
/// MD5 Encrypt
260
/// </summary>
261
/// <param name="strText">text</param>
262
/// <returns>md5 Encrypt string</returns>
263
public void MD5Encrypt()
264
{
265
MD5 md5 = new MD5CryptoServiceProvider();
266
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(this.inputString));
267
this.outString = System.Text.Encoding.Default.GetString(result);
268
}
269
#endregion
270
}
271
}
272

2

3

4

5

6

7



8

9



10


11


12

13

14

15


16

17

18

19


20

21

22

23


24

25

26

27


28

29

30

31


32

33

34

35


36

37

38

39

40

41


42


43

44

45

46



47



48



49

50


51

52

53

54



55



56



57

58


59

60

61

62



63



64



65

66


67

68

69

70



71



72



73

74


75

76

77

78



79



80



81

82


83

84

85

86



87



88



89

90


91

92

93

94



95



96



97

98

99

100


101

102



103

104

105

106

107

108

109


110


111

112

113

114

115

116

117



118

119



120

121



122

123

124

125

126

127

128

129

130

131

132



133

134

135

136

137

138


139


140

141

142

143

144

145



146

147



148

149

150



151

152

153

154

155

156

157

158

159

160

161

162



163

164

165

166

167

168


169


170

171

172

173

174

175

176



177

178



179

180



181

182

183

184

185

186

187

188

189

190

191

192

193

194

195



196

197

198

199

200

201

202

203

204

205

206



207

208

209

210

211

212


213


214

215

216

217

218

219

220



221

222



223

224



225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240



241

242

243

244

245

246

247

248

249

250

251



252

253

254

255

256

257


258


259

260

261

262

263

264



265

266

267

268

269

270

271

272
