在asp.net开发过程中,为了更好的维护和修改,有些东西,我们需要把这些东西写到web.config中,但是为了安全考虑,有些敏感信息容易泄漏,比如连接字符串,如果受到黑客攻击,那么是非常危险的,所以对web.config的加密时非常必须的,下面介绍本人常用的两种加密解密方法:
第一种方式通过DESCryptoServiceProvider类加密
public class EnDeCrypt
{
private int key="longpaissrs";
public int Key
{
get { return key; }
set { key = value; }
}
public static string Encrypt(string encryptString)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);