翻譯|使用教程|編輯:楊鵬連|2021-06-09 11:39:30.603|閱讀 378 次
概述:說到軟件安全保護,數據加密技術是網絡中最基本的安全技術,小編為大家介紹了常用數據加密和解密方法匯總,以及給出相關實現代碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關內容推薦:
以TripleDES為例,結合dotnet分析加密解密的各個步驟
六、非對稱加密之RSA加密和解密的講解
RSA公鑰加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美國麻省理工學院)開發(fā)的。RSA取名來自開發(fā)他們三者的名字。RSA是目前最有影響力的公鑰加密算法,它能夠抵抗到目前為止已知的所有密碼攻擊,已被ISO推薦為公鑰數據加密標準。RSA算法基于一個十分簡單的數論事實:將兩個大素數相乘十分容易,但那時想要對其乘積進行因式分解卻極其困難,因此可以將乘積公開作為加密密鑰。RSA算法是第一個能同時用于加密和數字簽名的算法,也易于理解和操作。
RSA是被研究得最廣泛的公鑰算法,從提出到現在已近二十年,經歷了各種攻擊的考驗,逐漸為人們接受,普遍認為是目前最優(yōu)秀的公鑰方案之一。RSA的安全性依賴于大數的因子分解,但并沒有從理論上證明破譯RSA的難度與大數分解難度等價。即RSA的重大缺陷是無法從理論上把握它的保密性能如何,而且密碼學界多數人士傾向于因子分解不是NPC問題。
RSA的缺點主要有:
A)產生密鑰很麻煩,受到素數產生技術的限制,因而難以做到一次一密。
B)分組長度太大,為保證安全性,n 至少也要 600bits以上,使運算代價很高,尤其是速度較慢,較對稱密碼算法慢幾個數量級;且隨著大數分解技術的發(fā)展,這個長度還在增加,不利于數據格式的標準化。目前,SET(Secure Electronic Transaction)協(xié)議中要求CA采用2048bits長的密鑰,其他實體使用1024比特的密鑰。C)RSA密鑰長度隨著保密級別提高,增加很快。下表列出了對同一安全級別所對應的密鑰長度。
 RSA算法是一種非對稱密碼算法,所謂非對稱,就是指該算法需要一對密鑰,使用其中一個加密,則需要用另一個才能解密。
RSA的算法涉及三個參數,n、e1、e2。
其中,n是兩個大質數p、q的積,n的二進制表示時所占用的位數,就是所謂的密鑰長度。
e1和e2是一對相關的值,e1可以任意取,但要求e1與(p-1)*(q-1)互質;再選擇e2,要求(e2*e1)mod((p-1)*(q-1))=1。
(n及e1),(n及e2)就是密鑰對。
RSA加解密的算法完全相同,設A為明文,B為密文,則:A=B^e1 mod n;B=A^e2 mod n;
e1和e2可以互換使用,即:
A=B^e2 mod n;B=A^e1 mod n;
需引用using System.Security.Cryptography;
七、ASP.NET(C#)常用加密類調用的講解
/// <summary>
/// RSA加密
/// </summary>
/// <param name="publickey"></param>
/// <param name="content"></param>
/// <returns></returns>
public static string RSAEncrypt(string publickey, string content)
{
publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> Convert.ToBase64String(cipherbytes);
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> RSA解密
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="privatekey"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="content"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> RSADecrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> privatekey, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> content)
{
privatekey </span>= <span style="line-height:1.5;color:rgb(128,0,0);">@"</span><span style="line-height:1.5;color:rgb(128,0,0);"><RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue></span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">;
RSACryptoServiceProvider rsa </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> RSACryptoServiceProvider();
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span><span style="line-height:1.5;">[] cipherbytes;
rsa.FromXmlString(privatekey);
cipherbytes </span>= rsa.Decrypt(Convert.FromBase64String(content), <span style="line-height:1.5;color:rgb(0,0,255);">false</span><span style="line-height:1.5;">);
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> Encoding.UTF8.GetString(cipherbytes);
}<br></span></pre>
/// <summary>
/// MD5 加密靜態(tài)方法
/// </summary>
/// <param name="EncryptString">待加密的密文</param>
/// <returns>returns</returns>
public static string MD5Encrypt(string EncryptString)
{
if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得為空")); }
MD5 m_ClassMD5 = new MD5CryptoServiceProvider();
string m_strEncrypt = "";
try
{
m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
}
catch (ArgumentException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_ClassMD5.Clear(); }
return m_strEncrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> DES 加密(數據加密標準,速度較快,適用于加密大量數據的場合)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待加密的密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptKey"></span><span style="line-height:1.5;color:rgb(0,128,0);">加密的密鑰</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></span><span style="line-height:1.5;color:rgb(0,128,0);">returns</span><span style="line-height:1.5;color:rgb(128,128,128);"></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> DESEncrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> EncryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> EncryptKey)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(EncryptString)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密文不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(EncryptKey)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (EncryptKey.Length != <span style="line-height:1.5;color:rgb(128,0,128);">8</span>) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰必須為8位</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btIV = { <span style="line-height:1.5;color:rgb(128,0,128);">0x12</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x34</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x56</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x78</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x90</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xAB</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xCD</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xEF</span><span style="line-height:1.5;"> };
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strEncrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
DESCryptoServiceProvider m_DESProvider </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> DESCryptoServiceProvider();
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btEncryptString =<span style="line-height:1.5;"> Encoding.Default.GetBytes(EncryptString);
MemoryStream m_stream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> MemoryStream();
CryptoStream m_cstream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
m_cstream.Write(m_btEncryptString, </span><span style="line-height:1.5;color:rgb(128,0,128);">0</span><span style="line-height:1.5;">, m_btEncryptString.Length);
m_cstream.FlushFinalBlock();
m_strEncrypt </span>=<span style="line-height:1.5;"> Convert.ToBase64String(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_cstream.Close(); m_cstream.Dispose();
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (IOException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (CryptographicException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (ArgumentException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">finally</span><span style="line-height:1.5;"> { m_DESProvider.Clear(); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strEncrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> DES 解密(數據加密標準,速度較快,適用于加密大量數據的場合)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待解密的密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptKey"></span><span style="line-height:1.5;color:rgb(0,128,0);">解密的密鑰</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></span><span style="line-height:1.5;color:rgb(0,128,0);">returns</span><span style="line-height:1.5;color:rgb(128,128,128);"></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> DESDecrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> DecryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> DecryptKey)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(DecryptString)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密文不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(DecryptKey)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (DecryptKey.Length != <span style="line-height:1.5;color:rgb(128,0,128);">8</span>) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰必須為8位</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btIV = { <span style="line-height:1.5;color:rgb(128,0,128);">0x12</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x34</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x56</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x78</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x90</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xAB</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xCD</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xEF</span><span style="line-height:1.5;"> };
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strDecrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
DESCryptoServiceProvider m_DESProvider </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> DESCryptoServiceProvider();
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btDecryptString =<span style="line-height:1.5;"> Convert.FromBase64String(DecryptString);
MemoryStream m_stream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> MemoryStream();
CryptoStream m_cstream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
m_cstream.Write(m_btDecryptString, </span><span style="line-height:1.5;color:rgb(128,0,128);">0</span><span style="line-height:1.5;">, m_btDecryptString.Length);
m_cstream.FlushFinalBlock();
m_strDecrypt </span>=<span style="line-height:1.5;"> Encoding.Default.GetString(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_cstream.Close(); m_cstream.Dispose();
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (IOException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (CryptographicException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (ArgumentException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">finally</span><span style="line-height:1.5;"> { m_DESProvider.Clear(); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strDecrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> RC2 加密(用變長密鑰對大量數據進行加密)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待加密密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptKey"></span><span style="line-height:1.5;color:rgb(0,128,0);">加密密鑰</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></span><span style="line-height:1.5;color:rgb(0,128,0);">returns</span><span style="line-height:1.5;color:rgb(128,128,128);"></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> RC2Encrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> EncryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> EncryptKey)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(EncryptString)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密文不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(EncryptKey)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (EncryptKey.Length < <span style="line-height:1.5;color:rgb(128,0,128);">5</span> || EncryptKey.Length > <span style="line-height:1.5;color:rgb(128,0,128);">16</span>) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰必須為5-16位</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strEncrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btIV = { <span style="line-height:1.5;color:rgb(128,0,128);">0x12</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x34</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x56</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x78</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x90</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xAB</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xCD</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xEF</span><span style="line-height:1.5;"> };
RC2CryptoServiceProvider m_RC2Provider </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> RC2CryptoServiceProvider();
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btEncryptString =<span style="line-height:1.5;"> Encoding.Default.GetBytes(EncryptString);
MemoryStream m_stream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> MemoryStream();
CryptoStream m_cstream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
m_cstream.Write(m_btEncryptString, </span><span style="line-height:1.5;color:rgb(128,0,128);">0</span><span style="line-height:1.5;">, m_btEncryptString.Length);
m_cstream.FlushFinalBlock();
m_strEncrypt </span>=<span style="line-height:1.5;"> Convert.ToBase64String(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_cstream.Close(); m_cstream.Dispose();
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (IOException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (CryptographicException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (ArgumentException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">finally</span><span style="line-height:1.5;"> { m_RC2Provider.Clear(); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strEncrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> RC2 解密(用變長密鑰對大量數據進行加密)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待解密密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptKey"></span><span style="line-height:1.5;color:rgb(0,128,0);">解密密鑰</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></span><span style="line-height:1.5;color:rgb(0,128,0);">returns</span><span style="line-height:1.5;color:rgb(128,128,128);"></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> RC2Decrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> DecryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> DecryptKey)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(DecryptString)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密文不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(DecryptKey)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (DecryptKey.Length < <span style="line-height:1.5;color:rgb(128,0,128);">5</span> || DecryptKey.Length > <span style="line-height:1.5;color:rgb(128,0,128);">16</span>) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰必須為5-16位</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btIV = { <span style="line-height:1.5;color:rgb(128,0,128);">0x12</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x34</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x56</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x78</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0x90</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xAB</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xCD</span>, <span style="line-height:1.5;color:rgb(128,0,128);">0xEF</span><span style="line-height:1.5;"> };
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strDecrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
RC2CryptoServiceProvider m_RC2Provider </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> RC2CryptoServiceProvider();
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btDecryptString =<span style="line-height:1.5;"> Convert.FromBase64String(DecryptString);
MemoryStream m_stream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> MemoryStream();
CryptoStream m_cstream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
m_cstream.Write(m_btDecryptString, </span><span style="line-height:1.5;color:rgb(128,0,128);">0</span><span style="line-height:1.5;">, m_btDecryptString.Length);
m_cstream.FlushFinalBlock();
m_strDecrypt </span>=<span style="line-height:1.5;"> Encoding.Default.GetString(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_cstream.Close(); m_cstream.Dispose();
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (IOException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (CryptographicException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (ArgumentException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">finally</span><span style="line-height:1.5;"> { m_RC2Provider.Clear(); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strDecrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> 3DES 加密(基于DES,對一塊數據用三個不同的密鑰進行三次加密,強度更高)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待加密密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptKey1"></span><span style="line-height:1.5;color:rgb(0,128,0);">密鑰一</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptKey2"></span><span style="line-height:1.5;color:rgb(0,128,0);">密鑰二</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptKey3"></span><span style="line-height:1.5;color:rgb(0,128,0);">密鑰三</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></span><span style="line-height:1.5;color:rgb(0,128,0);">returns</span><span style="line-height:1.5;color:rgb(128,128,128);"></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> DES3Encrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> EncryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span> EncryptKey1, <span style="line-height:1.5;color:rgb(0,0,255);">string</span> EncryptKey2, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> EncryptKey3)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strEncrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
m_strEncrypt </span>=<span style="line-height:1.5;"> DESEncrypt(EncryptString, EncryptKey3);
m_strEncrypt </span>=<span style="line-height:1.5;"> DESEncrypt(m_strEncrypt, EncryptKey2);
m_strEncrypt </span>=<span style="line-height:1.5;"> DESEncrypt(m_strEncrypt, EncryptKey1);
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strEncrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> 3DES 解密(基于DES,對一塊數據用三個不同的密鑰進行三次加密,強度更高)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待解密密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptKey1"></span><span style="line-height:1.5;color:rgb(0,128,0);">密鑰一</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptKey2"></span><span style="line-height:1.5;color:rgb(0,128,0);">密鑰二</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptKey3"></span><span style="line-height:1.5;color:rgb(0,128,0);">密鑰三</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></span><span style="line-height:1.5;color:rgb(0,128,0);">returns</span><span style="line-height:1.5;color:rgb(128,128,128);"></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> DES3Decrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> DecryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span> DecryptKey1, <span style="line-height:1.5;color:rgb(0,0,255);">string</span> DecryptKey2, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> DecryptKey3)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strDecrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
m_strDecrypt </span>=<span style="line-height:1.5;"> DESDecrypt(DecryptString, DecryptKey1);
m_strDecrypt </span>=<span style="line-height:1.5;"> DESDecrypt(m_strDecrypt, DecryptKey2);
m_strDecrypt </span>=<span style="line-height:1.5;"> DESDecrypt(m_strDecrypt, DecryptKey3);
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strDecrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> AES 加密(高級加密標準,是下一代的加密算法標準,速度快,安全級別高,目前 AES 標準的一個實現是 Rijndael 算法)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待加密密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="EncryptKey"></span><span style="line-height:1.5;color:rgb(0,128,0);">加密密鑰</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> AESEncrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> EncryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> EncryptKey)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(EncryptString)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密文不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(EncryptKey)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strEncrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btIV = Convert.FromBase64String(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">Rkb4jvUy/ye7Cd7k89QQgQ==</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">);
Rijndael m_AESProvider </span>=<span style="line-height:1.5;"> Rijndael.Create();
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btEncryptString =<span style="line-height:1.5;"> Encoding.Default.GetBytes(EncryptString);
MemoryStream m_stream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> MemoryStream();
CryptoStream m_csstream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
m_csstream.Write(m_btEncryptString, </span><span style="line-height:1.5;color:rgb(128,0,128);">0</span><span style="line-height:1.5;">, m_btEncryptString.Length); m_csstream.FlushFinalBlock();
m_strEncrypt </span>=<span style="line-height:1.5;"> Convert.ToBase64String(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (IOException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (CryptographicException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (ArgumentException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">finally</span><span style="line-height:1.5;"> { m_AESProvider.Clear(); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strEncrypt;
}
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span><span style="line-height:1.5;color:rgb(0,128,0);"> AES 解密(高級加密標準,是下一代的加密算法標準,速度快,安全級別高,目前 AES 標準的一個實現是 Rijndael 算法)
</span><span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"></summary></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptString"></span><span style="line-height:1.5;color:rgb(0,128,0);">待解密密文</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><param name="DecryptKey"></span><span style="line-height:1.5;color:rgb(0,128,0);">解密密鑰</span><span style="line-height:1.5;color:rgb(128,128,128);"></param></span>
<span style="line-height:1.5;color:rgb(128,128,128);">///</span> <span style="line-height:1.5;color:rgb(128,128,128);"><returns></returns></span>
<span style="line-height:1.5;color:rgb(0,0,255);">public</span> <span style="line-height:1.5;color:rgb(0,0,255);">static</span> <span style="line-height:1.5;color:rgb(0,0,255);">string</span> AESDecrypt(<span style="line-height:1.5;color:rgb(0,0,255);">string</span> DecryptString, <span style="line-height:1.5;color:rgb(0,0,255);">string</span><span style="line-height:1.5;"> DecryptKey)
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(DecryptString)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密文不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">if</span> (<span style="line-height:1.5;color:rgb(0,0,255);">string</span>.IsNullOrEmpty(DecryptKey)) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span> (<span style="line-height:1.5;color:rgb(0,0,255);">new</span> Exception(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">密鑰不得為空</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">)); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">string</span> m_strDecrypt = <span style="line-height:1.5;color:rgb(128,0,0);">""</span><span style="line-height:1.5;">;
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btIV = Convert.FromBase64String(<span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;color:rgb(128,0,0);">Rkb4jvUy/ye7Cd7k89QQgQ==</span><span style="line-height:1.5;color:rgb(128,0,0);">"</span><span style="line-height:1.5;">);
Rijndael m_AESProvider </span>=<span style="line-height:1.5;"> Rijndael.Create();
</span><span style="line-height:1.5;color:rgb(0,0,255);">try</span><span style="line-height:1.5;">
{
</span><span style="line-height:1.5;color:rgb(0,0,255);">byte</span>[] m_btDecryptString =<span style="line-height:1.5;"> Convert.FromBase64String(DecryptString);
MemoryStream m_stream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> MemoryStream();
CryptoStream m_csstream </span>= <span style="line-height:1.5;color:rgb(0,0,255);">new</span><span style="line-height:1.5;"> CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
m_csstream.Write(m_btDecryptString, </span><span style="line-height:1.5;color:rgb(128,0,128);">0</span><span style="line-height:1.5;">, m_btDecryptString.Length); m_csstream.FlushFinalBlock();
m_strDecrypt </span>=<span style="line-height:1.5;"> Encoding.Default.GetString(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (IOException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (CryptographicException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (ArgumentException ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">catch</span> (Exception ex) { <span style="line-height:1.5;color:rgb(0,0,255);">throw</span><span style="line-height:1.5;"> ex; }
</span><span style="line-height:1.5;color:rgb(0,0,255);">finally</span><span style="line-height:1.5;"> { m_AESProvider.Clear(); }
</span><span style="line-height:1.5;color:rgb(0,0,255);">return</span><span style="line-height:1.5;"> m_strDecrypt;
}</span></pre>
2、數據加密和解密簡單代碼調用如下:
Response.Write("<br>-----------MD5加密---------------<br>");
Response.Write(SDKSecurity.MD5Encrypt("仰天一笑"));
Response.Write("<br>-----------DES加密---------------<br>");
Response.Write(SDKSecurity.DESEncrypt("仰天一笑", "anson-xu"));
Response.Write("<br>-----------DES解密---------------<br>");
Response.Write(SDKSecurity.DESDecrypt("l06JvJ45r/lb9iKzSXl47Q==", "anson-xu"));
Response.Write("<br>-----------AES加密---------------<br>");
Response.Write(SDKSecurity.AESEncrypt("仰天一笑", "ansonxuyu"));
Response.Write("<br>-----------AES解密---------------<br>");
Response.Write(SDKSecurity.AESDecrypt("avwKL+MO8+zoLHvzk0+TBA==", "ansonxuyu"));
3、數據加密和解密調用后運行效果圖如下:
網絡評價:加密的安全級別非常高,破解難度很大,但是加密數據多,需要注意系統(tǒng)的性能。
【下載試用】 |
【在線購買】 |
|
網絡評價:用好其虛擬機保護功能,將關鍵敏感代碼用虛擬機保護起來,能很好提高強度。
【下載試用】 |
【在線購買】 |
網絡評價:WinLicense主要比Themida多了一個協(xié)議,可以設定使用時間,運行次數等功能,兩者核心保護是一樣的。
【下載試用】 |
【在線購買】 |
|
慧都科技響應“全面加強知識產權保護,推動構建新發(fā)展格局”號召,加密解密產品為您的應用程序保駕護航!在線購買享受限時特惠,Go!>>
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn