ASP.NET 常用的字符串加密

时间:2021-03-05 09:22:52

字符串常用的加密有三种

1、MD5加密,这个常用于密码,单向加密,不可解密,有些在线解密的可以解大部份,用代码不能实现,如果不想让人解密,加密后随便截取一段就好了;

2、Base64位加密,通常加密后字符串尾会有两个 == ,可解密;

3、SHA加密,单向加密,安全性没MD5好。

以上不可解密都是相对的,这些加密算法都是跨平台的。

        using System.Web.Security;
using System.Security.Cryptography; /// <summary>
/// MD5函数
/// </summary>
/// <param name="val">原始字符串</param>
/// <returns>MD5结果</returns>
public static string MD5String(string val)
{
if (val.Trim().Equals(String.Empty) == true)
return String.Empty; byte[] b = Encoding.UTF8.GetBytes(val);
b = new MD5CryptoServiceProvider().ComputeHash(b);
string ret = String.Empty;
for (int i = 0; i < b.Length; i++)
ret += b[i].ToString("x").PadLeft(2, '0'); return ret;
} /// <summary>
/// SHA1字符串加密
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string SHA1(string obj)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(obj, "SHA1");
} /// <summary>
/// SHA256字符串加密
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string SHA256(string obj)
{
//return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(obj, "SHA256");
byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(obj);
SHA256 sha256 = new SHA256Managed();
byte[] retVal = sha256.ComputeHash(bytValue);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
} /// <summary>
/// Base64位加密
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static string EncryptBase64(string val)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(val));
} /// <summary>
/// Base64位解密
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public static string DecryptBase64(string val)
{
return Encoding.Default.GetString(Convert.FromBase64String(val));
} /// <summary>
/// BASE64位(MD5加密)[标准,非MD5加密再BASE64位加密]
/// </summary>
/// <param name="val">原始字符串</param>
/// <returns>MD5结果</returns>
public static string EncryptBase64AndMD5(string val)
{
if (val.Trim().Equals(String.Empty) == true)
return String.Empty; MD5CryptoServiceProvider MD5CSP = new MD5CryptoServiceProvider();
return Convert.ToBase64String(MD5CSP.ComputeHash(Encoding.UTF8.GetBytes(val)));
}

也可以定义一个KEY,自定义加密算法

        /// <summary>
/// DES加密字符串
/// </summary>
/// <param name="encryptString">待加密的字符串</param>
/// <param name="encryptKey">加密密钥,要求为8位</param>
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
public static string Encode(string encryptString, string encryptKey)
{
encryptKey = UtilsString.SubString(encryptKey, 8);
encryptKey = encryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
} /// <summary>
/// DES解密字符串
/// </summary>
/// <param name="decryptString">待解密的字符串</param>
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>
public static string Decode(string decryptString, string decryptKey)
{
decryptKey = UtilsString.SubString(decryptKey, 8);
decryptKey = decryptKey.PadRight(8, ' ');
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
byte[] rgbIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}