CSharp examples for System.Security.Cryptography:AES
AES Decrypt string key and data
using System.Web; using System.Text; using System.Security.Cryptography; using System.Linq; using System.IO;/*w ww . j a v a2 s . c om*/ using System.Collections.Generic; using System; public class Main{ public static string AESDecrypt(string data, string key) { Byte[] encryptedBytes = Convert.FromBase64String(data); Byte[] bKey = new Byte[32]; Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(bKey.Length)), bKey, bKey.Length); MemoryStream mStream = new MemoryStream(encryptedBytes); RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.PKCS7; aes.KeySize = 128; aes.Key = bKey; CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read); try { byte[] tmp = new byte[encryptedBytes.Length + 32]; int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32); byte[] ret = new byte[len]; Array.Copy(tmp, 0, ret, 0, len); return Encoding.UTF8.GetString(ret); } finally { cryptoStream.Close(); mStream.Close(); aes.Clear(); } } }