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 = "