翻译|使用教程|编辑:杨鹏连|2021-06-09 11:39:30.603|阅读 280 次
概述:说到软件安全保护,数据加密技术是网络中最基本的安全技术,小编为大家介绍了常用数据加密和解密方法汇总,以及给出相关实现代码。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
相关内容推荐:
以TripleDES为例,结合dotnet分析加密解密的各个步骤
六、非对称加密之RSA加密和解密的讲解
RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作。
RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一。RSA的安全性依赖于大数的因子分解,但并没有从理论上证明破译RSA的难度与大数分解难度等价。即RSA的重大缺陷是无法从理论上把握它的保密性能如何,而且密码学界多数人士倾向于因子分解不是NPC问题。
RSA的缺点主要有:
A)产生密钥很麻烦,受到素数产生技术的限制,因而难以做到一次一密。
B)分组长度太大,为保证安全性,n 至少也要 600bits以上,使运算代价很高,尤其是速度较慢,较对称密码算法慢几个数量级;且随着大数分解技术的发展,这个长度还在增加,不利于数据格式的标准化。目前,SET(Secure Electronic Transaction)协议中要求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 加密静态方法
/// </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、数据加密和解密调用后运行效果图如下:
网络评价:加密的安全级别非常高,破解难度很大,但是加密数据多,需要注意系统的性能。
【下载试用】 |
【在线购买】 |
|
网络评价:用好其虚拟机保护功能,将关键敏感代码用虚拟机保护起来,能很好提高强度。
【下载试用】 |
【在线购买】 |
网络评价:WinLicense主要比Themida多了一个协议,可以设定使用时间,运行次数等功能,两者核心保护是一样的。
【下载试用】 |
【在线购买】 |
|
慧都科技响应“全面加强知识产权保护,推动构建新发展格局”号召,加密解密产品为您的应用程序保驾护航!在线购买享受限时特惠,Go!>>
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn