午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    C# MD5 加密解密 DES RC2 3DES AES等軟創(chuàng)加密類
    來源:易賢網(wǎng) 閱讀:1159 次 日期:2014-08-20 14:51:59
    溫馨提示:易賢網(wǎng)小編為您整理了“C# MD5 加密解密 DES RC2 3DES AES等軟創(chuàng)加密類”,方便廣大網(wǎ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;

    006namespace 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 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 加密(數(shù)據(jù)加密標(biāo)準(zhǔn),速度較快,適用于加密大量數(shù)據(jù)的場合)

    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 解密(數(shù)據(jù)加密標(biāo)準(zhǔn),速度較快,適用于加密大量數(shù)據(jù)的場合)

    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 加密(用變長密鑰對大量數(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.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 解密(用變長密鑰對大量數(shù)據(jù)進行加密)

    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,對一塊數(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_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,對一塊數(shù)據(jù)用三個不同的密鑰進行三次加密,強度更高)

    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 加密(高級加密標(biāo)準(zhǔn),是下一代的加密算法標(biāo)準(zhǔn),速度快,安全級別高,目前 AES 標(biāo)準(zhǔn)的一個實現(xiàn)是 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 解密(高級加密標(biāo)準(zhǔn),是下一代的加密算法標(biāo)準(zhǔn),速度快,安全級別高,目前 AES 標(biāo)準(zhǔn)的一個實現(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.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技術(shù)專欄

    更多信息請查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機網(wǎng)站地址:C# MD5 加密解密 DES RC2 3DES AES等軟創(chuàng)加密類
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報名

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報警專用圖標(biāo)