// AES 加密 public byte[] AESEncrypt(string text) { byte[] data = Encoding.Unicode.GetBytes(text); SymmetricAlgorithm aes = Rijndael.Create(); aes.Key = keyArray; aes.IV = keyArray; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.Zeros; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); byte[] cipherBytes = ms.ToArray(); // 得到加密后的字节数组 cs.Close(); ms.Close(); aes.Clear(); return cipherBytes; } } }
// AES 解密 public string AESDecrypt(byte[] data) { SymmetricAlgorithm aes = Rijndael.Create(); aes.Key = keyArray; aes.IV = keyArray; aes.Mode = CipherMode.CBC; aes.Padding = PaddingMode.Zeros; byte[] decryptBytes = new byte[data.Length]; using (MemoryStream ms = new MemoryStream(data)) { using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read)) { cs.Read(decryptBytes, 0, decryptBytes.Length); cs.Close(); ms.Close(); } } aes.Clear(); return System.Text.Encoding.Unicode.GetString(decryptBytes).Replace("\0", " "); }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-28 14:30:38