c#的AES加密解密问题 10
初学C#,AES加密已经调试成功,解密时遇到“System.Security.Cryptography.CryptographicException:填充无效,无法被移除...
初学C#,AES加密已经调试成功,解密时遇到“System.Security.Cryptography.CryptographicException:填充无效,无法被移除。”(图2的红框处)问题,错在哪里?
展开
2个回答
展开全部
不是初学,而是对.net中所有的加解密方式都没有弄清楚。不过别灰心——很多自以为会的人也没有弄清。
首先,.net中类库中支持各类摘要加解密方式。一般情况下我们将安全分为两类,一类是摘要,一类是加解密。加密解又分为对称与非对称加解密。
在.net体系中,不管是摘要还是加解密,为了统一算法方式,一律都是流方式进行的。不管是MD5摘要还是ADE/DES/TDES/RSA等等。一定要记住的第一条,是流方式进行的!
流——这个概念很多人也不清楚,因为stream的范围还是非常的大的,有网络流responseStream等,基本文本流,IO流等等,在加密时我们使用了一个流叫“加解密流”CryptStream,该流用来实现加解密及摘要算法等等。
那么CryptoStream是个什么流呢?它算是转换流,把一种形式转换成另一种形式,比如把密文转换成明文或把明文换成密文。正常的情况下,我们可以用流写流的方式来实现,比如MemoryStream来换流。当然,也可以把byte[]数组直接写到流中。
流这部分说完了,那么,其实我们如何区别CryptoStream是加密还是解密呢,除了说明之外,还是一个方式,就是看流的方式是读还是写!所以当我看到你的解密流中使用的方式竟然是Write,而不是Read,所以你对加解密方式并没有真正理解。
比如,我们可以转换后的流直接进行ReadToLine即可。
老实说,你这种写法有很大问题——有关stream大部分都是非托管,所以一定要记着Dispose,或Close(Close时会自动调用Dispose)。防止出现错误等内存泄露。
有于加解密流,我们经常一句话,加密方式是Write,解密方式是Read!
首先,.net中类库中支持各类摘要加解密方式。一般情况下我们将安全分为两类,一类是摘要,一类是加解密。加密解又分为对称与非对称加解密。
在.net体系中,不管是摘要还是加解密,为了统一算法方式,一律都是流方式进行的。不管是MD5摘要还是ADE/DES/TDES/RSA等等。一定要记住的第一条,是流方式进行的!
流——这个概念很多人也不清楚,因为stream的范围还是非常的大的,有网络流responseStream等,基本文本流,IO流等等,在加密时我们使用了一个流叫“加解密流”CryptStream,该流用来实现加解密及摘要算法等等。
那么CryptoStream是个什么流呢?它算是转换流,把一种形式转换成另一种形式,比如把密文转换成明文或把明文换成密文。正常的情况下,我们可以用流写流的方式来实现,比如MemoryStream来换流。当然,也可以把byte[]数组直接写到流中。
流这部分说完了,那么,其实我们如何区别CryptoStream是加密还是解密呢,除了说明之外,还是一个方式,就是看流的方式是读还是写!所以当我看到你的解密流中使用的方式竟然是Write,而不是Read,所以你对加解密方式并没有真正理解。
比如,我们可以转换后的流直接进行ReadToLine即可。
老实说,你这种写法有很大问题——有关stream大部分都是非托管,所以一定要记着Dispose,或Close(Close时会自动调用Dispose)。防止出现错误等内存泄露。
有于加解密流,我们经常一句话,加密方式是Write,解密方式是Read!
2018-10-17 · 知道合伙人互联网行家
关注
展开全部
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}
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}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |