AES加密及解密
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace Dome1 { public class Encryption { private string encryptKey = "4h!@w$rng,i#$@x1%)5^3(7*5P31/Ee0"; //默认密钥向量 private byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F }; /// <summary> /// 加密 /// </summary> /// <param name="encryptString"></param> /// <returns></returns> public string Encrypt(string encryptString) { if (string.IsNullOrEmpty(encryptString)) return string.Empty; RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32)); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor(); byte[] inputData = Encoding.UTF8.GetBytes(encryptString); byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length); return Convert.ToBase64String(encryptedData); } /// <summary> /// 解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public string Decrypt(string decryptString) { if (string.IsNullOrEmpty(decryptString)) return string.Empty; try { RijndaelManaged rijndaelProvider = new RijndaelManaged(); rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32)); rijndaelProvider.IV = Keys; ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor(); byte[] inputData = Convert.FromBase64String(decryptString); byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length); return Encoding.UTF8.GetString(decryptedData); } catch { return ""; } } } }
public class AESHelper { /// <summary> /// 密钥 /// </summary> /// <returns></returns> private static string GetKeyString() { string KeyString = "123456789123456789123456789abcde"; return KeyString.Substring(0, 32); } /// <summary> /// 偏移量 /// </summary> /// <returns></returns> private static string GetIVString() { string IVString = "123456789abcdefg"; return IVString.Substring(0, 16); } /// <summary> /// 256位AES加密 /// </summary> /// <param name="toEncrypt"></param> /// <returns></returns> public static string Encrypt(string toEncrypt) { try { RijndaelManaged rm = new RijndaelManaged(); rm.Key = UTF8Encoding.UTF8.GetBytes(GetKeyString()); rm.IV = UTF8Encoding.UTF8.GetBytes(GetIVString()); rm.Mode = CipherMode.CBC;//加密方式 rm.Padding = PaddingMode.PKCS7;//填充 ICryptoTransform cryptoTransform = rm.CreateEncryptor(); // 256-AES key byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt); byte[] resultArray = cryptoTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } catch (Exception e) { return ""; } } /// <summary> /// 256位AES解密 /// </summary> /// <param name="toDecrypt"></param> /// <returns></returns> public static string Decrypt(string toDecrypt) { try { if (toDecrypt.Length == 16 || toDecrypt.Length == 18 || string.IsNullOrEmpty(toDecrypt)) { return toDecrypt; } RijndaelManaged rm = new RijndaelManaged(); rm.Key = UTF8Encoding.UTF8.GetBytes(GetKeyString()); rm.IV = UTF8Encoding.UTF8.GetBytes(GetIVString()); rm.Mode = CipherMode.CBC;//加密方式 rm.Padding = PaddingMode.PKCS7;//填充 ICryptoTransform cryptoTransform = rm.CreateDecryptor(); // 256-AES key byte[] toDecryptArray = Convert.FromBase64String(toDecrypt); byte[] resultArray = cryptoTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } catch (Exception e) { return ""; } } }