Decrypt AES 256 - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:AES

Description

Decrypt AES 256

Demo Code


using System.Text;
using System.Security.Cryptography;
using System;/*from   w  ww . j  a  v  a  2 s.  c  o m*/
using Org.BouncyCastle.Utilities.Encoders;

public class Main{
        public static string Decrypt_AES256(string cipherText, string keyStr)
        {
            RijndaelManaged aes = new RijndaelManaged();
            aes.BlockSize = 128;
            aes.KeySize = 256;

            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;

            // Initialization vector.
            // It could be any value or generated using a random number generator.
            byte[] keyArr = Base64.Decode(keyStr);
            byte[] ivArr = { 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 7, 7, 7, 7 };

            aes.Key = keyArr;
            aes.IV = ivArr;

            ICryptoTransform decrypto = aes.CreateDecryptor();

            byte[] encryptedBytes = Convert.FromBase64CharArray(cipherText.ToCharArray(), 0, cipherText.Length);
            byte[] decryptedData = decrypto.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
            return Encoding.GetString(decryptedData);
        }
}

Related Tutorials