using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; namespace Nirvana.Common { /// /// AES加密、解密帮助类 /// public class AESEncrypt { /// /// 默认密钥-密钥的长度必须是32 /// private static string PublicKey = "liuzl_ybhdmob_19"; /// /// 默认向量 /// private const string Iv = "liuzlaiweimob019"; private static char[] base64CodeArray = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '=' }; /// /// AES加密 /// /// 需要加密字符串 /// 加密后字符串 public static String Encrypt(string str) { return Encrypt(str, PublicKey); } /// /// AES解密 /// /// 需要解密字符串 /// 解密后字符串 public static String Decrypt(string str) { return Decrypt(str, PublicKey); } /// /// AES加密 /// /// 需要加密的字符串 /// 32位密钥 /// 加密后的字符串 public static string Encrypt(string str, string key) { Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key); Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(str); var rijndael = Aes.Create(); rijndael.Key = keyArray; rijndael.Mode = System.Security.Cryptography.CipherMode.ECB; rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7; rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv); System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateEncryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// /// AES解密 /// /// 需要解密的字符串 /// 32位密钥 /// 解密后的字符串 public static string Decrypt(string str, string key) { try { Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key); //过滤特殊字符 string dummyData = str.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+"); if (dummyData.Length % 4 > 0) { dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '='); } Byte[] toEncryptArray = Convert.FromBase64String(dummyData); var rijndael = Aes.Create(); rijndael.Key = keyArray; rijndael.Mode = System.Security.Cryptography.CipherMode.ECB; rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7; rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv); System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateDecryptor(); Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return System.Text.Encoding.UTF8.GetString(resultArray); } catch (Exception) { return ""; } } public static bool IsBase64(string base64Str) { byte[] bytes = null; return IsBase64(base64Str, out bytes); } /// /// 是否base64字符串 /// /// 要判断的字符串 /// 字符串转换成的字节数组 /// private static bool IsBase64(string base64Str, out byte[] bytes) { //string strRegex = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"; bytes = null; if (string.IsNullOrEmpty(base64Str)) return false; else { if (base64Str.Contains(",")) base64Str = base64Str.Split(',')[1]; if (base64Str.Length % 4 != 0) return false; if (base64Str.Any(c => !base64CodeArray.Contains(c))) return false; } try { bytes = Convert.FromBase64String(base64Str); return true; } catch (FormatException) { return false; } } } }