C# 各种加密方法封装类,软创加密类,内含MD5加密解密、DES法解密加密、RC2加密解密、3DES加密解密,还有AES加解密等,使用时用到哪一种加密方法,可把代码单独摘录出来,本类比较综合,代码中包括注释,完整代码:
view sourceprint?001using System;
002using System.Collections.Generic;
003using System.Text;
004using System.IO;
005using System.Security.Cryptography;
006namespace CLB.Utility.CharTools
007{
008 ///
009 /// 软创加密类
010 ///
011 public static class Cryptography
012 {
013 ///
014 /// MD5 加密,静态方法
015 ///
016 /// 待加密的密文
017 /// returns
018 public static string MD5Encrypt(string EncryptString)
019 {
020 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
021 MD5 m_ClassMD5 = new MD5CryptoServiceProvider();
022 string m_strEncrypt = "";
023 try
024 {
025 m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
026 }
027 catch (ArgumentException ex) { throw ex; }
028 catch (CryptographicException ex) { throw ex; }
029 catch (Exception ex) { throw ex; }
030 finally { m_ClassMD5.Clear(); }
031 return m_strEncrypt;
032 }
033 ///
034 /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
035 ///
036 /// 待加密的密文
037 /// 加密的密钥
038 /// returns
039 public static string DESEncrypt(string EncryptString, string EncryptKey)
040 {
041 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
042 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
043 if (EncryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
044 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
045 string m_strEncrypt = "";
046 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
047 try
048 {
049 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
050 MemoryStream m_stream = new MemoryStream();
051 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
052 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
053 m_cstream.FlushFinalBlock();
054 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
055 m_stream.Close(); m_stream.Dispose();
056 m_cstream.Close(); m_cstream.Dispose();
057 }
058 catch (IOException ex) { throw ex; }
059 catch (CryptographicException ex) { throw ex; }
060 catch (ArgumentException ex) { throw ex; }
061 catch (Exception ex) { throw ex; }
062 finally { m_DESProvider.Clear(); }
063 return m_strEncrypt;
064 }
065 ///
066 /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
067 ///
068 /// 待解密的密文
069 /// 解密的密钥
070 /// returns
071 public static string DESDecrypt(string DecryptString, string DecryptKey)
072 {
073 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
074 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
075 if (DecryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
076 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
077 string m_strDecrypt = "";
078 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
079 try
080 {
081 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
082 MemoryStream m_stream = new MemoryStream();
083 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
084 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
085 m_cstream.FlushFinalBlock();
086 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
087 m_stream.Close(); m_stream.Dispose();
088 m_cstream.Close(); m_cstream.Dispose();
089 }
090 catch (IOException ex) { throw ex; }
091 catch (CryptographicException ex) { throw ex; }
092 catch (ArgumentException ex) { throw ex; }
093 catch (Exception ex) { throw ex; }
094 finally { m_DESProvider.Clear(); }
095 return m_strDecrypt;
096 }
097 ///
098 /// RC2 加密(用变长密钥对大量数据进行加密)
099 ///
100 /// 待加密密文
101 /// 加密密钥
102 /// returns
103 public static string RC2Encrypt(string EncryptString, string EncryptKey)
104 {
105 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
106 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
107 if (EncryptKey.Length < 5 || EncryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
108 string m_strEncrypt = "";
109 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
110 RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
111 try
112 {
113 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
114 MemoryStream m_stream = new MemoryStream();
115 CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
116 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
117 m_cstream.FlushFinalBlock();
118 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
119 m_stream.Close(); m_stream.Dispose();
120 m_cstream.Close(); m_cstream.Dispose();
121 }
122 catch (IOException ex) { throw ex; }
123 catch (CryptographicException ex) { throw ex; }
124 catch (ArgumentException ex) { throw ex; }
125 catch (Exception ex) { throw ex; }
126 finally { m_RC2Provider.Clear(); }
127 return m_strEncrypt;
128 }
129 ///
130 /// RC2 解密(用变长密钥对大量数据进行加密)
131 ///
132 /// 待解密密文
133 /// 解密密钥
134 /// returns
135 public static string RC2Decrypt(string DecryptString, string DecryptKey)
136 {
137 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
138 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
139 if (DecryptKey.Length < 5 || DecryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
140 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
141 string m_strDecrypt = "";
142 RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
143 try
144 {
145 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
146 MemoryStream m_stream = new MemoryStream();
147 CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
148 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
149 m_cstream.FlushFinalBlock();
150 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
151 m_stream.Close(); m_stream.Dispose();
152 m_cstream.Close(); m_cstream.Dispose();
153 }
154 catch (IOException ex) { throw ex; }
155 catch (CryptographicException ex) { throw ex; }
156 catch (ArgumentException ex) { throw ex; }
157 catch (Exception ex) { throw ex; }
158 finally { m_RC2Provider.Clear(); }
159 return m_strDecrypt;
160 }
161 ///
162 /// 3DES 加密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
163 ///
164 /// 待加密密文
165 /// 密钥一
166 /// 密钥二
167 /// 密钥三
168 /// returns
169 public static string DES3Encrypt(string EncryptString, string EncryptKey1, string EncryptKey2, stringEncryptKey3)
170 {
171 string m_strEncrypt = "";
172 try
173 {
174 m_strEncrypt = DESEncrypt(EncryptString, EncryptKey3);
175 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey2);
176 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey1);
177 }
178 catch (Exception ex) { throw ex; }
179 return m_strEncrypt;
180 }
181 ///
182 /// 3DES 解密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
183 ///
184 /// 待解密密文
185 /// 密钥一
186 /// 密钥二
187 /// 密钥三
188 /// returns
189 public static string DES3Decrypt(string DecryptString, string DecryptKey1, string DecryptKey2, stringDecryptKey3)
190 {
191 string m_strDecrypt = "";
192 try
193 {
194 m_strDecrypt = DESDecrypt(DecryptString, DecryptKey1);
195 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey2);
196 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey3);
197 }
198 catch (Exception ex) { throw ex; }
199 return m_strDecrypt;
200 }
201 ///
202 /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
203 ///
204 /// 待加密密文
205 /// 加密密钥
206 ///
207 public static string AESEncrypt(string EncryptString, string EncryptKey)
208 {
209 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
210 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
211 string m_strEncrypt = "";
212 byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
213 Rijndael m_AESProvider = Rijndael.Create();
214 try
215 {
216 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
217 MemoryStream m_stream = new MemoryStream();
218 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
219 m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_csstream.FlushFinalBlock();
220 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
221 m_stream.Close(); m_stream.Dispose();
222 m_csstream.Close(); m_csstream.Dispose();
223 }
224 catch (IOException ex) { throw ex; }
225 catch (CryptographicException ex) { throw ex; }
226 catch (ArgumentException ex) { throw ex; }
227 catch (Exception ex) { throw ex; }
228 finally { m_AESProvider.Clear(); }
229 return m_strEncrypt;
230 }
231 ///
232 /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
233 ///
234 /// 待解密密文
235 /// 解密密钥
236 ///
237 public static string AESDecrypt(string DecryptString, string DecryptKey)
238 {
239 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
240 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
241 string m_strDecrypt = "";
242 byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
243 Rijndael m_AESProvider = Rijndael.Create();
244 try
245 {
246 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
247 MemoryStream m_stream = new MemoryStream();
248 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
249 m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_csstream.FlushFinalBlock();
250 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
251 m_stream.Close(); m_stream.Dispose();
252 m_csstream.Close(); m_csstream.Dispose();
253 }
254 catch (IOException ex) { throw ex; }
255 catch (CryptographicException ex) { throw ex; }
256 catch (ArgumentException ex) { throw ex; }
257 catch (Exception ex) { throw ex; }
258 finally { m_AESProvider.Clear(); }
259 return m_strDecrypt;
260 }
261 }
262}
更多信息请查看IT技术专栏