using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.Helper { /// /// 创建AES加密解密 add fyf 20211122 /// public class AESHelper { /// /// 默认密钥-密钥的长度必须是32 /// private const string PublicKey = "9848461354184618"; /// /// 默认向量 /// private const string Iv = "dfkladnasldnfdcv"; /// /// 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 = new System.Security.Cryptography.RijndaelManaged(); 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) { Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key); Byte[] toEncryptArray = Convert.FromBase64String(str); var rijndael = new System.Security.Cryptography.RijndaelManaged(); 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); } } }