随着开发时间的变长,当初认为比较难的东西,现在渐渐也就变的不那么难了!特别对于一些经常很少使用的类,时间长了之后渐渐就陌生了。所以在这里写一些日后可能会用到的加密与解密。
一.AES加密算法和DES加密算法的效率比较:
下面是在网上看到的一段关于“ES加密程序和一个AES加密程序,比较两个程序进行大文件加密的效率”:
- 实验步骤:
- 实验结果:
- 结果分析:
当文件很小时,两个程序加密的时间差不多。但是当文件变大时,DES对文件的加密效率低于AES对文件的加密效率。可见AES具有比DES更好的安全性,效率!
二.AES和DES加密解密代码:
========DES加密========
/// <summary>
/// DES加密
/// </summary>
/// <param name="pToEncrypt">要加密的内容</param>
/// <param name="sKey">密钥</param>
/// <param name="StrMs">输出加密后的字符串</param>
/// <returns>加密后的byte数组</returns>
public static byte[] Encrypt(string pToEncrypt, string sKey, out string StrMs)
{
//访问数据加密标准(DES)算法的加密服务提供程序 (CSP) 版本的包装对象
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.PKCS7;
des.Key = Encoding.UTF8.GetBytes(sKey); //建立加密对象的密钥和偏移量
//des.IV = Encoding.UTF8.GetBytes(sKey); // 如果是用ECB模式,则IV不管是什么都不会影响加密/解密的结果 byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);//把字符串放到byte数组中 MemoryStream ms = new MemoryStream();//创建其支持存储区为内存的流
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);//定义将数据流链接到加密转换的流
cs.Write(inputByteArray, , inputByteArray.Length);//上面已经完成了把加密后的结果放到内存中去
cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
StrMs = ret.ToString();
return ms.ToArray();
} /// <summary>
/// DEs 解密过程
/// </summary>
/// <param name="pToDecrypt">被解密的字符串</param>
/// <param name="sKey">密钥(只支持8个字节的密钥,同前面的加密密钥相同)</param>
/// <returns>返回被解密的字符串</returns>
public static string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.PKCS7;
byte[] inputByteArray = new byte[pToDecrypt.Length / ];
for (int x = ; x < pToDecrypt.Length / ; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * , ), ));
inputByteArray[x] = (byte)i;
} des.Key = Encoding.UTF8.GetBytes(sKey); //建立加密对象的密钥和偏移量,此值重要,不能修改
//des.IV = Encoding.UTF8.GetBytes(sKey);// 如果是用ECB模式,则IV不管是什么都不会影响加密/解密的结果
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock(); return System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
/// <summary>
/// AES加密
/// </summary>
/// <param name="toEncrypt">要加密的内容</param>
/// <param name="strKey">密钥(16或者32位)</param>
/// <returns>Base64转码后的密文</returns>
public static string Encrypt(string toEncrypt, string strKey)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey);
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); RijndaelManaged rDel = new RijndaelManaged();//using System.Security.Cryptography;
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;//using System.Security.Cryptography;
rDel.Padding = PaddingMode.PKCS7;//using System.Security.Cryptography; ICryptoTransform cTransform = rDel.CreateEncryptor();//using System.Security.Cryptography;
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return Convert.ToBase64String(resultArray, , resultArray.Length);
} /// <summary>
/// AES解密
/// </summary>
/// <param name="toDecrypt">要解密的内容</param>
/// <param name="strKey">密钥(16或者32位)</param>
/// <returns>解密后的明文</returns>
public static string Decrypt(string toDecrypt, string strKey)
{
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strKey);
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt); RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = CipherMode.ECB;
rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, , toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray);
}
#endregion AES解密
}