加密解密(Base64、AES、MD5、HMACSHA256)

/// /// Base64加密 /// /// 待加密的明文 /// 编码方式 /// public static string EncodeBase64(string source, Encoding encode) { byte[] bytes = encode.GetBytes(source); return Convert.ToBase64String(bytes); }/// /// Base64加密 /// /// 待加密的明文 /// /// /// /// 参看SecurityUtil.EncodeBase64(string,Encoding)方法的说明 public static string EncodeBase64(string source) { return EncodeBase64(source, Encoding.UTF8); }/// /// Base64解密 /// /// 待解密的明文 /// 编码方式 /// public static string DecodeBase64(string source, Encoding encode) { string decode = ""; byte[] bytes = Convert.FromBase64String(source); decode = encode.GetString(bytes); return decode; }/// /// AES加密 /// /// 待加密的内容 /// 加密密钥 /// /// /// /// public static string AESEncrypt(string context, byte[] keyBytes,int keySize,int blockSize,byte[] ivBytes) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = keySize; rijndaelCipher.BlockSize = blockSize; // 加密密钥 rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); byte[] plainText = Encoding.UTF8.GetBytes(context); byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length); return Convert.ToBase64String(cipherBytes); }/// /// AES解密 /// /// /// /// 【加密解密(Base64、AES、MD5、HMACSHA256)】 /// /// /// public static string AESDecrypt(string context, byte[] keyBytes, int keySize, int blockSize, byte[] ivBytes) { RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; rijndaelCipher.KeySize = keySize; rijndaelCipher.BlockSize = blockSize; byte[] encryptedData = https://www.it610.com/article/Convert.FromBase64String(context); rijndaelCipher.Key = keyBytes; rijndaelCipher.IV = ivBytes; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); return Encoding.UTF8.GetString(plainText); }//16位加密 /// /// MD516位加密 /// /// 待加密的数据 /// 编码方式 /// public static string Encrypt16(string encryptData, Encoding encodingType) { byte[] bEncrypt = encodingType.GetBytes(encryptData); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(bEncrypt, 4, 8)); t2 = t2.Replace("-", ""); return t2; }//32位加密 /// /// MD532位加密(大写) /// /// 待加密的数据 /// 编码方式 /// public static string Encrypt32(string encryptData, Encoding encodingType) { byte[] bEncrypt = encodingType.GetBytes(encryptData); byte[] bComputeHash = MD5.Create().ComputeHash(bEncrypt); StringBuilder sb = new StringBuilder(); foreach (byte item in bComputeHash) { sb.Append(item.ToString("X2")); } return sb.ToString(); }//32位加密 /// /// MD532位加密(小写) /// /// 待加密的数据 /// 编码方式 /// public static string Encrypt32Lower(string encryptData, Encoding encodingType) { byte[] bEncrypt = encodingType.GetBytes(encryptData); byte[] bComputeHash = MD5.Create().ComputeHash(bEncrypt); StringBuilder sb = new StringBuilder(); foreach (byte item in bComputeHash) { sb.Append(item.ToString("x2")); } return sb.ToString(); }//64位加密 /// /// MD564位加密 /// /// 待加密的数据 /// 编码方式 /// public static string Encrypt64(string encryptData, Encoding encodingType) { byte[] bEncrypt = encodingType.GetBytes(encryptData); //string pwd = ""; MD5 md5 = MD5.Create(); //实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择  byte[] s = md5.ComputeHash(bEncrypt); return Convert.ToBase64String(s); }/// /// 字符串 str 按照简体中文(ASCIIEncoding.ASCII)的编码方式 /// /// 待编码的内容 /// public static byte[] GetBytesByASCIIEncoding(string encodingData) { return ASCIIEncoding.ASCII.GetBytes(encodingData); }public static byte[] GetSHA1HashBytes(byte[] encryptData) { return new SHA1CryptoServiceProvider().ComputeHash(encryptData); }/// /// HmacSHA256加密64位 /// /// 待加密内容 /// 加密密钥 /// public static string HmacSHA256Encrypt64(string message,string secret) { return HmacSHA256Encrypt64(message,secret,new System.Text.ASCIIEncoding()); }/// /// HmacSHA256加密64位 /// /// 待加密内容 /// 加密密钥 /// 编码方式 /// public static string HmacSHA256Encrypt64(string message, string secret,Encoding encoding) { secret = secret ?? ""; byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return Convert.ToBase64String(hashmessage); } }


    推荐阅读