MD5加密封装16位和32位和AES加密封装
public class EncryptionHelper { #region AES加密【对称加密】高级加密标准Rijndael加密法 /// <summary> /// 密钥长度则可以是128,192或256位(下限128,上限256;必须32的倍数) /// </summary> public static int KeySize { get; set; } = 128; /// <summary> ///(长度=KeySize/8)/// </summary> private const string keyDefault = "HeiHeiHaHa123456"; /// <summary> /// 获取随机的加密密钥 /// </summary> /// <param name="key">返回随机秘钥</param> /// <param name="iV">返回随机初始化向量</param> /// <returns></returns> public static void GetGenerateKey(out string key, out string iV) { key = GetGenerateKey(); iV = GetGenerateIV(); } /// <summary> /// 获取随机的加密密钥 /// </summary> /// <returns></returns> public static string GetGenerateKey() { int n = KeySize / 8; char[] arrChar = new char[]{ 'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x', '0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z' }; StringBuilder num = new StringBuilder(); Random rnd = new Random(); for (int i = 0; i < n; i++) { num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); } return num.ToString(); } /// <summary> /// 获取随机的初始化向量 /// </summary> /// <returns></returns> public static string GetGenerateIV() { int n = KeySize / 8; char[] arrChar = new char[]{ 'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x', '0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z' }; StringBuilder num = new StringBuilder(); Random rnd = new Random(DateTime.Now.Millisecond); for (int i = 0; i < n; i++) { num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString()); } return num.ToString(); } /// <summary> /// AES加密 /// </summary> /// <param name="clearText">待加密字符(明文)</param> /// <param name="key">加密密钥</param> /// <param name="iv">初始化向量</param> /// <param name="CryptText">输出:已加密字符串</param> /// <returns>0:成功加密 -1:待加密字符串不为能空 -2:加密密钥不能为空 -3:初始化向量字节长度不为KEYSIZE/8 -4:其他错误</returns> public static string GetEncryptText(string clearText, out string msg,string key= keyDefault, string iv= keyDefault) { //密文 string cryptText = string.Empty; msg = string.Empty; clearText = clearText.Trim(); if (string.IsNullOrEmpty(clearText)) { msg = "待加密字符串不为能空"; return cryptText; } if (string.IsNullOrEmpty(key)) { msg = "加密密钥不能为空"; return cryptText; } if (string.IsNullOrEmpty(iv) || iv.Length != KeySize / 8) { msg = "初始化向量字节长度不为KEYSIZE/8"; return cryptText; } try { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = KeySize; rijndaelCipher.BlockSize = KeySize; byte[] pwdBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); byte[] keyBytes = new byte[KeySize / 8]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(clearText); byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length); cryptText = Convert.ToBase64String(cipherBytes); return cryptText; } catch (Exception ex) { msg = $"加密异常报错;{ex.Message}"; return cryptText; } } /// <summary> /// AES解密 /// </summary> /// <param name="cryptText">待解密字符串</param> /// <param name="key">加密密钥</param> /// <param name="iv">初始化向量</param> /// <param name="msg">异常信息</param> /// <returns>解密后的明文</returns> public static string GetDecryptText(string cryptText, string key, string iv, out string msg) { //解密的明文 string textData = string.Empty; msg = string.Empty; if (string.IsNullOrEmpty(cryptText)) { msg = "待解密字符串不为能空"; return textData; } if (string.IsNullOrEmpty(key)) { msg = "加密密钥不能为空"; return textData; } if (string.IsNullOrEmpty(iv) || iv.Length != KeySize / 8) { msg = "初始化向量字节长度不为KEYSIZE/8"; return textData; } try { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = KeySize; rijndaelCipher.BlockSize = KeySize; byte[] encryptedData = Convert.FromBase64String(cryptText); byte[] pwdBytes = Encoding.UTF8.GetBytes(key); byte[] ivBytes = Encoding.UTF8.GetBytes(iv); byte[] keyBytes = new byte[KeySize / 8]; int len = pwdBytes.Length; if (len > keyBytes.Length) { len = keyBytes.Length; } System.Array.Copy(pwdBytes, keyBytes, len); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); textData = Encoding.UTF8.GetString(plainText).Trim(); return textData; } catch (Exception ex) { msg = $"异常报错;{ex.Message}"; return textData; } } #endregion #region MD5 【非对称单项加密】 /// <summary> /// 获取32位MD5加密(默认UTF8编码格式) /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GetMD5Len32(string text) { MD5 md5 = MD5.Create(); //byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s)); byte[] t = md5.ComputeHash(Encoding.UTF8.GetBytes(text)); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < t.Length; i++) { stringBuilder.Append(t[i].ToString("x").PadLeft(2, '0')); } return stringBuilder.ToString(); } /// <summary> /// 16位Md5加密(默认UTF8编码格式) /// </summary> /// <param name="text"></param> /// <returns></returns> public static string GetMD5Len16(string text) { MD5 md5 = MD5.Create(); //byte[] t = md5.ComputeHash(UTF8Encoding.Default.GetBytes(text)); byte[] t = md5.ComputeHash(Encoding.UTF8.GetBytes(text)); string t2 = BitConverter.ToString(t, 4, 8); t2 = t2.Replace("-", ""); return t2; } #endregion }