Encrypt
1using System;
2using System.Security.Cryptography;
3using System.IO;
4using System.Text;
5
6/**//// <summary>
7/// Help to process 数据加密、解密,文件的加密解密
8/// </summary>
9/// <created>By Peter Lee @ 2006-03-22</created>
10/// <modified>By Peter Lee @ 2006-04-15</modified>
11public sealed class Encrypt
12{
13 private Encrypt()
14
{ }
15 /**//// <summary>
16 /// 只能是长度为8的ASCII码
17 /// </summary>
18 private const string DESKEY = "Ha1d4yOu";
19 /**//// <summary>
20 /// 使用DES加密算法加密,并返回加密过程成功与否。
21 /// </summary>
22 /// <param name="strText">明文</param>
23 /// <param name="sOutText">当加密成功时输出密文</param>
24 /// <returns></returns>
25 public static bool DesEncrypt(string strText, out string sOutText)
26
{
27 string strEncrKey = Encrypt.DESKEY;
28 sOutText = string.Empty;
29 byte[] byKey = null;
30 byte[] IV =
{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
31 try
32
{
33 byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, strEncrKey.Length));
34 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
35 byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
36 MemoryStream ms = new MemoryStream();
37 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
38 cs.Write(inputByteArray, 0, inputByteArray.Length);
39 cs.FlushFinalBlock();
40 sOutText = Convert.ToBase64String(ms.ToArray());
41 return true;
42 }
43 catch
44
{
45 sOutText = string.Empty;
46 return false;
47 }
48 }
49 /**//// <summary>
50 /// 使用DES算法解密,并返回解密成功与否。
51 /// </summary>
52 /// <param name="strText">要解密的密文。</param>
53 /// <param name="sOutText">当解密成功时输出明文</param>
54 /// <returns></returns>
55 public static bool DesDecrypt(string strText, out string sOutText)
56
{
57 string sDecrKey = Encrypt.DESKEY;
58 sOutText = string.Empty;
59 byte[] byKey = null;
60 byte[] IV =
{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
61 byte[] inputByteArray = new Byte[strText.Length];
62 try
63
{
64 byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
65 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
66 inputByteArray = Convert.FromBase64String(strText);
67 MemoryStream ms = new MemoryStream();
68 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
69 cs.Write(inputByteArray, 0, inputByteArray.Length);
70 cs.FlushFinalBlock();
71 System.Text.Encoding encoding = new System.Text.UTF8Encoding();
72 sOutText = encoding.GetString(ms.ToArray());
73 return true;
74 }
75 catch
76
{
77 sOutText = string.Empty;
78 return false;
79 }
80
81 }
82 /**//// <summary>
83 /// 使用DES加密算法加密文件。
84 /// </summary>
85 /// <param name="m_InFilePath">要加密的文件所在路径。</param>
86 /// <param name="m_OutFilePath">加密后输出文件所在路径,有同名文件存在时会先删除。</param>
87 /// <returns></returns>
88 public static bool DesEncrypt(string m_InFilePath, string m_OutFilePath)
89
{
90 -= OLD =-#region -= OLD =-
91// string strEncrKey = Encrypt.DESKEY;
92// byte[] byKey = null;
93// byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
94// try
95// {
96// byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
97// FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
98// FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
99// fout.SetLength(0);
100// byte[] bin = new byte[100];
101// long rdlen = 0;
102// long totlen = fin.Length;
103// int len;
104// DES des = new DESCryptoServiceProvider();
105// CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
106// while (rdlen < totlen)
107// {
108// len = fin.Read(bin, 0, 100);
109// encStream.Write(bin, 0, len);
110// rdlen = rdlen + len;
111// }
112//
113// encStream.Close();
114// fout.Close();
115// fin.Close();
116// return true;
117// }
118// catch
119// {
120// return false;
121// }
122 #endregion
123
124 if(!System.IO.File.Exists( m_InFilePath ))
125
{
126 return false;
127 }
128 string strText = FileIO.ReadTextFile( m_InFilePath );
129 if(null == strText)
130
{
131 return false;
132 }
133 string sOutText;
134 if(!Encrypt.DesEncrypt(strText,out sOutText))
135
{
136 return false;
137 }
138 return FileIO.WriteToFile( m_OutFilePath,sOutText);
139 }
140 /**//// <summary>
141 /// 使用DES算法解密被加密的文件。
142 /// </summary>
143 /// <param name="m_InFilePath">要解密的文件所在路径。</param>
144 /// <param name="m_OutFilePath">解密后输出文件路径,有同名文件存在时会先删除。</param>
145 /// <returns></returns>
146 public static bool DesDecrypt(string m_InFilePath, string m_OutFilePath)
147
{
148 -= OLD =-#region -= OLD =-
149// string sDecrKey = Encrypt.DESKEY;
150// byte[] byKey = null;
151// byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
152// try
153// {
154// byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
155// FileStream fin = new FileStream(m_InFilePath, FileMode.Open, FileAccess.Read);
156// FileStream fout = new FileStream(m_OutFilePath, FileMode.OpenOrCreate, FileAccess.Write);
157// fout.SetLength(0);
158//
159// byte[] bin = new byte[100];
160// long rdlen = 0;
161// long totlen = fin.Length;
162// int len;
163//
164// DES des = new DESCryptoServiceProvider();
165// CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
166//
167// while (rdlen < totlen)
168// {
169// len = fin.Read(bin, 0, 100);
170// encStream.Write(bin, 0, len);
171// rdlen = rdlen + len;
172// }
173//
174// encStream.Close();
175// fout.Close();
176// fin.Close();
177// return true;
178// }
179// catch
180// {
181// return false;
182// }
183 #endregion
184
185 if(!System.IO.File.Exists(m_InFilePath))
186
{
187 return false;
188 }
189 string strText = FileIO.ReadTextFile( m_InFilePath );
190 if(null == strText)
191
{
192 return false;
193 }
194 string sOutText;
195 if(!Encrypt.DesDecrypt(strText,out sOutText))
196
{
197 return false;
198 }
199 return FileIO.WriteToFile(m_OutFilePath,sOutText);
200 }
201 /**//// <summary>
202 /// 使用MD5加密指定的明文并返回加密结果。
203 /// </summary>
204 /// <param name="strText">要加密的字符串</param>
205 /// <returns></returns>
206 public static string MD5Encrypt(string strText)
207
{
208 MD5 md5 = new MD5CryptoServiceProvider();
209 byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText));
210 return System.Text.Encoding.Default.GetString(result);
211 }
212 /**//// <summary>
213 /// 使用MD5解密指定的密文并返回解密结果。
214 /// </summary>
215 /// <param name="strText"></param>
216 /// <returns></returns>
217 public static string MD5Decrypt(string strText)
218
{
219 MD5 md5 = new MD5CryptoServiceProvider();
220 byte[] result = md5.TransformFinalBlock(System.Text.Encoding.Default.GetBytes(strText), 0, strText.Length);
221 return System.Text.Encoding.Default.GetString(result);
222 }
223}