C#对字符和文件的加密解密类

自己是个新手,刚学C#也不久。在做东西的时候需要对文件或者字符串加密,然后在查找相关资料后写了这个C#加密解密类!但总觉的欠缺太多的东西,于是贴出代码希望大大们给予指正!谢谢!

 

  1      public class strorfilepass    {
2 /// <summary>
3 /// 初始化安全类
4 /// </summary>
5 public strorfilepass()
6 {
7 ///默认密码
8 key = "www.qhjsw.net";
9 }
10 private string key; //默认密钥
11
12 private byte[] sKey;
13 private byte[] sIV;
14
15 #region 加密字符串
16 /// <summary>
17 /// 加密字符串
18 /// </summary>
19 /// <param name="inputStr">输入字符串</param>
20 /// <param name="keyStr">密码,可以为""</param>
21 /// <returns>输出加密后字符串</returns>
22 public string EncryptString(string inputStr, string keyStr)
23 {
24 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
25 if (keyStr == "")
26 keyStr = key;
27 byte[] inputByteArray = Encoding.Default.GetBytes(inputStr);
28 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
29 SHA1 ha = new SHA1Managed();
30 byte[] hb = ha.ComputeHash(keyByteArray);
31 sKey = new byte[8];
32 sIV = new byte[8];
33 for (int i = 0; i < 8; i++)
34 sKey[i] = hb[i];
35 for (int i = 8; i < 16; i++)
36 sIV[i - 8] = hb[i];
37 des.Key = sKey;
38 des.IV = sIV;
39 MemoryStream ms = new MemoryStream();
40 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
41 cs.Write(inputByteArray, 0, inputByteArray.Length);
42 cs.FlushFinalBlock();
43 StringBuilder ret = new StringBuilder();
44 foreach (byte b in ms.ToArray())
45 {
46 ret.AppendFormat("{0:X2}", b);
47 }
48 cs.Close();
49 ms.Close();
50 return ret.ToString();
51 }
52 /// <summary>
53 /// 解密字符串
54 /// </summary>
55 /// <param name="inputStr">要解密的字符串</param>
56 /// <param name="keyStr">密钥</param>
57 /// <returns>解密后的结果</returns>
58 public string DecryptString(string inputStr, string keyStr)
59 {
60 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
61 if (keyStr == "")
62 keyStr = key;
63 byte[] inputByteArray = new byte[inputStr.Length / 2];
64 for (int x = 0; x < inputStr.Length / 2; x++)
65 {
66 int i = (Convert.ToInt32(inputStr.Substring(x * 2, 2), 16));
67 inputByteArray[x] = (byte)i;
68 }
69 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
70 SHA1 ha = new SHA1Managed();
71 byte[] hb = ha.ComputeHash(keyByteArray);
72 sKey = new byte[8];
73 sIV = new byte[8];
74 for (int i = 0; i < 8; i++)
75 sKey[i] = hb[i];
76 for (int i = 8; i < 16; i++)
77 sIV[i - 8] = hb[i];
78 des.Key = sKey;
79 des.IV = sIV;
80 MemoryStream ms = new MemoryStream();
81 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
82 cs.Write(inputByteArray, 0, inputByteArray.Length);
83 cs.FlushFinalBlock();
84 StringBuilder ret = new StringBuilder();
85 return System.Text.Encoding.Default.GetString(ms.ToArray());
86 }
87 #endregion
88
89 #region 加密文件
90 /// <summary>
91 /// 加密文件
92 /// </summary>
93 /// <param name="filePath">输入文件路径</param>
94 /// <param name="savePath">加密后输出文件路径</param>
95 /// <param name="keyStr">密码,可以为""</param>
96 /// <returns></returns>
97 public bool EncryptFile(string filePath, string savePath, string keyStr)
98 {
99 try
100 {
101 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
102 if (keyStr == "")
103 keyStr = key;
104 FileStream fs = File.OpenRead(filePath);
105 byte[] inputByteArray = new byte[fs.Length];
106 fs.Read(inputByteArray, 0, (int)fs.Length);
107 fs.Close();
108 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
109 SHA1 ha = new SHA1Managed();
110 byte[] hb = ha.ComputeHash(keyByteArray);
111 sKey = new byte[8];
112 sIV = new byte[8];
113 for (int i = 0; i < 8; i++)
114 sKey[i] = hb[i];
115 for (int i = 8; i < 16; i++)
116 sIV[i - 8] = hb[i];
117 des.Key = sKey;
118 des.IV = sIV;
119 MemoryStream ms = new MemoryStream();
120 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
121 cs.Write(inputByteArray, 0, inputByteArray.Length);
122 cs.FlushFinalBlock();
123 fs = File.OpenWrite(savePath);
124 foreach (byte b in ms.ToArray())
125 {
126 fs.WriteByte(b);
127 }
128 fs.Close();
129 cs.Close();
130 ms.Close();
131 return true;
132 }
133 catch{
134 return false;
135 }
136 }
137 #endregion
138
139 #region 解密文件
140 /// <summary>
141 /// 解密文件
142 /// </summary>
143 /// <param name="filePath">输入文件路径</param>
144 /// <param name="savePath">解密后输出文件路径</param>
145 /// <param name="keyStr">密码,可以为""</param>
146 /// <returns></returns>
147 public bool DecryptFile(string filePath, string savePath, string keyStr)
148 {
149 try
150 {
151 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
152 if (keyStr == "")
153 keyStr = key;
154 FileStream fs = File.OpenRead(filePath);
155 byte[] inputByteArray = new byte[fs.Length];
156 fs.Read(inputByteArray, 0, (int)fs.Length);
157 fs.Close();
158 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
159 SHA1 ha = new SHA1Managed();
160 byte[] hb = ha.ComputeHash(keyByteArray);
161 sKey = new byte[8];
162 sIV = new byte[8];
163 for (int i = 0; i < 8; i++)
164 sKey[i] = hb[i];
165 for (int i = 8; i < 16; i++)
166 sIV[i - 8] = hb[i];
167 des.Key = sKey;
168 des.IV = sIV;
169 MemoryStream ms = new MemoryStream();
170 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
171 cs.Write(inputByteArray, 0, inputByteArray.Length);
172 cs.FlushFinalBlock();
173 fs = File.OpenWrite(savePath);
174 foreach (byte b in ms.ToArray())
175 {
176 fs.WriteByte(b);
177 }
178 fs.Close();
179 cs.Close();
180 ms.Close();
181
182 return true;
183 }
184 catch {
185 return false;
186 }
187 }
188 #endregion
189 # region 大文件加密
190 /// <summary>
191 /// 加密文件
192 /// </summary>
193 /// <param name="filePath">输入文件路径</param>
194 /// <param name="savePath">加密后输出文件路径</param>
195 /// <param name="keyStr">密码,可以为""</param>
196 /// <returns></returns>
197 public bool BEncryptFile(string filePath, string savePath, string keyStr)
198 {
199 try
200 {
201 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
202 if (keyStr == "")
203 keyStr = key;
204 FileStream fs = File.OpenRead(filePath);
205 byte[] inputByteArray = new byte[500];
206 long Myfilesize = fs.Length;
207 int Mycompletedsize = 0;
208 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
209 SHA1 ha = new SHA1Managed();
210 byte[] hb = ha.ComputeHash(keyByteArray);
211 sKey = new byte[8];
212 sIV = new byte[8];
213 for (int i = 0; i < 8; i++)
214 sKey[i] = hb[i];
215 for (int i = 8; i < 16; i++)
216 sIV[i - 8] = hb[i];
217 des.Key = sKey;
218 des.IV = sIV;
219 FileStream ms = File.OpenWrite(savePath);
220 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
221 while (Mycompletedsize < Myfilesize)
222 {
223 int length = fs.Read(inputByteArray, 0, 500);
224 cs.Write(inputByteArray, 0, length);
225 Mycompletedsize += length;
226 }
227 cs.FlushFinalBlock();
228 fs.Close();
229 fs.Dispose();
230 cs.Close();
231 cs.Dispose();
232 ms.Close();
233 ms.Dispose();
234 return true;
235 }
236 catch
237 {
238 return false;
239 }
240 }
241
242
243 /// <summary>
244 /// 解密文件
245 /// </summary>
246 /// <param name="filePath">输入文件路径</param>
247 /// <param name="savePath">解密后输出文件路径</param>
248 /// <param name="keyStr">密码,可以为""</param>
249 /// <returns></returns>
250 public bool BDecryptFile(string filePath, string savePath, string keyStr)
251 {
252 try
253 {
254 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
255 if (keyStr == "")
256 keyStr = key;
257 FileStream fs = File.OpenRead(filePath);
258 byte[] inputByteArray = new byte[500];
259 long Myfilesize = fs.Length;
260 int Mycompletedsize = 0;
261 byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
262 SHA1 ha = new SHA1Managed();
263 byte[] hb = ha.ComputeHash(keyByteArray);
264 sKey = new byte[8];
265 sIV = new byte[8];
266 for (int i = 0; i < 8; i++)
267 sKey[i] = hb[i];
268 for (int i = 8; i < 16; i++)
269 sIV[i - 8] = hb[i];
270 des.Key = sKey;
271 des.IV = sIV;
272 FileStream ms = File.OpenWrite(savePath);
273 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
274 while (Mycompletedsize < Myfilesize)
275 {
276 int length = fs.Read(inputByteArray, 0, 500);
277 cs.Write(inputByteArray, 0, length);
278 Mycompletedsize += length;
279 }
280 cs.FlushFinalBlock();
281
282 fs.Close();
283 fs.Dispose();
284 cs.Close();
285 cs.Dispose();
286 ms.Close();
287 ms.Dispose();
288
289 return true;
290 }
291 catch
292 {
293 return false;
294 }
295 }
296 #endregion
297 }

 

posted @ 2012-02-17 14:20  bluescreen  阅读(2164)  评论(6编辑  收藏  举报