AES 加解密,MD5加密。
aes 加密,生成公私钥有比较麻烦,可以通过支付宝提供的网站生成。https://miniu.alipay.com/keytool/create。
AES 加解密两种写法:
/// <summary>
/// AES 加密
/// </summary>
/// <param name="text"></param>
/// <param name="password"></param>
/// <param name="iv"></param>
/// <returns></returns>
public static string AESEncrypt(string text, string password, string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] encryptedData = Encoding.UTF8.GetBytes(text);
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
//此句如果没有要求,也可以不要。
rijndaelCipher.IV = new byte[16];
rijndaelCipher.Key = keyBytes;
byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
rijndaelCipher.IV = ivBytes;
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Convert.ToBase64String(plainText);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="text"></param>
/// <param name="password"></param>
/// <param name="iv"></param>
/// <returns></returns>
public static string AESDecrypt(string text, string password, string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] encryptedData = Convert.FromBase64String(text);
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
rijndaelCipher.IV = ivBytes;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="toEncrypt">明文</param>
/// <param name="key">密码</param>
/// <param name="iv">偏移量</param>
/// <returns></returns>
public static string AesEncrypt(string toEncrypt, string key, string iv)
{
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);//注意编码格式(utf8编码 UTF8Encoding)
byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
//如果没有偏移量,此句可以去掉
rDel.IV = ivArray;
rDel.Mode = CipherMode.CBC;
//rDel.Padding = PaddingMode.Zeros;
ICryptoTransform cTransform = rDel.CreateEncryptor();//加密
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="toDecrypt">解密</param>
/// <param name="key">密码</param>
/// <param name="iv">偏移量</param>
/// <returns></returns>
public static string AesDecrypt(string toDecrypt, string key, string iv)
{
byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);//注意编码格式(utf8编码 UTF8Encoding)
byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = keyArray;
//如果没有偏移量,此句可以去掉
rDel.IV = ivArray;
rDel.Mode = CipherMode.CBC;
//rDel.Padding = PaddingMode.Zeros;
ICryptoTransform cTransform = rDel.CreateDecryptor();//解密
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
}
MD5
一.使用16位、32位、64位MD5方法对用户名加密 1)16位的MD5加密 /// <summary> /// 16位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns> public static string MD5Encrypt16(string password) { var md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8); t2 = t2.Replace("-", ""); return t2; } 2)32位的MD5加密 /// <summary> /// 32位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns> public static string MD5Encrypt32(string password) { string cl = password; string pwd = ""; MD5 md5 = MD5.Create(); //实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 for (int i = 0; i < s.Length; i++) { // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 pwd = pwd + s[i].ToString("X"); } return pwd; } 3)64位的MD5加密 public static string MD5Encrypt64(string password) { string cl = password; //string pwd = ""; MD5 md5 = MD5.Create(); //实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); return Convert.ToBase64String(s); } 4)使用MD5为用户密码加密 /// <summary> /// 加密用户密码 /// </summary> /// <param name="password">密码</param> /// <param name="codeLength">加密位数</param> /// <returns>加密密码</returns> public static string md5(string password, int codeLength) { if (!string.IsNullOrEmpty(password)) { // 16位MD5加密(取32位加密的9~25字符) if (codeLength == 16) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower().Substring(8, 16); } // 32位加密 if (codeLength == 32) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower(); } } return string.Empty; } 由于MD5是不可逆的,所以加密之后就无法解密,取用户名和密码时候,需要再加密一边用户输入的数据与数据库中已加密的数据进行比对。如果比对结果一致,则可以判定登陆成功! 原文链接:https://blog.csdn.net/u011555996/article/details/107051146