Web开发:AES加密解密的demo

可直接在控制台程序运行:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

public class AesEncryptionDemo
{
    public static byte[] Encrypt(string plainText, byte[] key, byte[] iv)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = key;
            aes.IV = iv;

            ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
                    cryptoStream.Write(plainBytes, 0, plainBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    return memoryStream.ToArray();
                }
            }
        }
    }

    public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv)
    {
        using (Aes aes = Aes.Create())
        {
            aes.Key = key;
            aes.IV = iv;

            ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

            using (MemoryStream memoryStream = new MemoryStream(cipherText))
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                {
                    byte[] plainBytes = new byte[cipherText.Length];
                    int decryptedByteCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);
                    return Encoding.UTF8.GetString(plainBytes, 0, decryptedByteCount);
                }
            }
        }
    }

    public static void Main()
    {
        string plainText = "123456";
        byte[] key = new byte[32]; // AES-256 使用 32 字节的密钥
        byte[] iv = new byte[16]; // AES 使用 16 字节的初始向量

        // 生成随机的密钥和初始向量key iv
        using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
        {
            rng.GetBytes(key);
            rng.GetBytes(iv);
        }

        string ivHex = BitConverter.ToString(iv).Replace("-", "").ToLower();//将密钥和初始向量转化为字符串存入数据库
        string keyHex = BitConverter.ToString(key).Replace("-", "").ToLower();

        byte[] ivHexs = Enumerable.Range(0, ivHex.Length)//将数据库密钥和初始向量字符串取出并转化为字节形式
                          .Where(x => x % 2 == 0)
                          .Select(x => Convert.ToByte(ivHex.Substring(x, 2), 16))
                          .ToArray();
        byte[] keyHexs = Enumerable.Range(0, keyHex.Length)
                          .Where(x => x % 2 == 0)
                          .Select(x => Convert.ToByte(keyHex.Substring(x, 2), 16))
                          .ToArray();

        byte[] cipherText = Encrypt(plainText, key, iv);//明文=》密文,其中key和iv为随机生成的密钥和初始向量(字节)
        string decryptedText = Decrypt(cipherText, key, iv);//密文=》明文,其中key和iv为随机生成的密钥和初始向量(字节)

        Console.WriteLine("Plain Text: " + plainText);// 明文
        Console.WriteLine("Cipher Text: " + Convert.ToBase64String(cipherText));//密文
        Console.WriteLine("Decrypted Text: " + decryptedText);//解密后的明文
    }
}