




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、C# MD5 加密解密 DES RC2 3DES AES等軟創(chuàng)加密類_ C# 各種加密方法封裝類,軟創(chuàng)加密類,內(nèi)含MD5加密解密、DES法解密加密、RC2加密解密、3DES加密解密,還有AES加解密等,用法時用到哪一種加密方法,可把代碼單獨摘錄出來,本類比較綜合,代碼中包括說明,完整代碼: view sourceprint?001using System; 002using System.Collections.Generic; 003using System.Text; 004using System.IO; 005using System.Security.Cryptography; 00
2、6namespace CLB.Utility.CharTools 007 008 / 009 / 軟創(chuàng)加密類 010 / 011 public static class Cryptography 012 013 / 014 / MD5 加密,靜態(tài)方法 015 / 016 / 待加密的密文 017 / returns 018 public static string MD5Encrypt(string EncryptString) 019 020 if (string.IsNullOrEmpty(EncryptString) throw (new Exception(密文不得為空); 021 M
3、D5 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
4、; 029 catch (Exception ex) throw ex; 030 finally m_ClassMD5.Clear(); 031 return m_strEncrypt; 032 033 / 034 / DES 加密(數(shù)據(jù)加密標準,速度較快,適用于加密大量數(shù)據(jù)的場合) 035 / 036 / 待加密的密文 037 / 加密的密鑰 038 / returns 039 public static string DESEncrypt(string EncryptString, string EncryptKey) 040 041 if (string.IsNullOrEmpty(En
5、cryptString) 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
6、 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(EncryptK
7、ey), 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 cat
8、ch (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 解密(數(shù)據(jù)加密標準,速度較快,適用于加密大量數(shù)據(jù)的場合) 067 / 068 / 待解密的密文 069 / 解密的密鑰 070
9、/ 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位); 0
10、76 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();
11、 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.Ge
12、tString(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_DESPr
13、ovider.Clear(); 095 return m_strDecrypt; 096 097 / 098 / RC2 加密(用變長密鑰對大量數(shù)據(jù)進行加密) 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.IsNullO
14、rEmpty(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 RC2CryptoServiceProvi
15、der(); 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
16、 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 (C
17、ryptographicException 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 解密(用變長密鑰對大量數(shù)據(jù)進行加密) 131 / 132 / 待解密密文 133 / 解密密鑰 134 / returns 135 public static string RC2Decrypt(string D
18、ecryptString, 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, 0x
19、56, 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 = n
20、ew 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
21、 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_st
22、rDecrypt; 160 161 / 162 / 3DES 加密(基于DES,對一塊數(shù)據(jù)用三個不同的密鑰進行三次加密,強度更高) 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_strE
23、ncrypt = 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,對一塊數(shù)據(jù)用三個不同的密鑰進行三次加密,強度更高) 183 / 184 / 待解密
24、密文 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_
25、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 標準的一個實現(xiàn)是 Rijndael 算法) 203 / 204 / 待加密密文 205 / 加密密鑰 206 / 207 public static string AESEncrypt(
26、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/ye7Cd7k89gQ=); 213 Rijndael m_
27、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), C
28、ryptoStreamMode.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
29、 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 標準的一個實現(xiàn)是 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.IsNullOrE
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 抵押合同六8篇
- 伸縮門采購合同合同
- 新零售模式下智慧物流配送優(yōu)化策略
- 灑水車合同5篇
- 商業(yè)保密協(xié)議書十
- 公司員工保底協(xié)議
- 2025年貴港貨運資格證培訓考試題
- 2025年寧夏貨車從業(yè)資格證答題軟件
- 陶瓷插芯市場分析及競爭策略分析報告
- 珠光材料市場分析及競爭策略分析報告
- berg平衡評定量表
- 中央空調(diào)維保方案
- EPC總承包項目財務(wù)管理要點
- 一年級下學期開學家長會
- 發(fā)動機飛輪殼加工工藝及其夾具設(shè)計
- 中國控制會議論文模板英文
- 前廳羅盤系統(tǒng)操作細則
- 迅達扶梯9300AE故障代碼
- 二年級下冊數(shù)學課件-第三單元 對稱圖形 ppt(29張) 北京版(2021秋)
- 六十四卦爻象全圖(彩色)(共6頁)
- 《各種各樣的橋》ppt課件
評論
0/150
提交評論