C# MD5 加密解密 DES RC2 3DES AES等軟創加密類

來源:互聯網
上載者:User

  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}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.