使用.NET框架中的System.Security.Cryptography命名空间中的AesManaged类来实现,下面这个是我以前写过的可以参考:
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
class AesExample
{
static void Main()
{
string plainText = "Hello, world!";
byte[] key = new byte[32];
byte[] iv = new byte[16];
// Generate a random key and IV
using (AesManaged aes = new AesManaged())
{
aes.GenerateKey();
aes.GenerateIV();
key = aes.Key;
iv = aes.IV;
}
// Encrypt the string using the key and IV
byte[] encrypted = EncryptStringToBytes_Aes(plainText, key, iv);
// Decrypt the encrypted string using the key and IV
string decrypted = DecryptStringFromBytes_Aes(encrypted, key, iv);
// Display the original data and the decrypted data
Console.WriteLine("Original: {0}", plainText);
Console.WriteLine("Encrypted: {0}", Convert.ToBase64String(encrypted));
Console.WriteLine("Decrypted: {0}", decrypted);
}
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] key, byte[] iv)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] key, byte[] iv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an AesManaged object
// with the specified key and IV.
using (AesManaged aes = new AesManaged())
{
aes.Key = key;
aes.IV = iv;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}