Decrypt text with AES Key - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:AES

Description

Decrypt text with AES Key

Demo Code


using System.Text;
using System.Security.Cryptography;
using System.Linq;
using System.Collections.Generic;
using System;/*from w w  w  . j ava 2s . com*/

public class Main{

        private static string Decrypt(string key, string iv, string txt)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] ivArray = UTF8Encoding.UTF8.GetBytes(iv);
            byte[] txtArray = Convert.FromBase64String(txt);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.BlockSize = 128;
            rDel.KeySize = 128;
            rDel.Key = keyArray;
            rDel.IV = ivArray;
            rDel.Mode = CipherMode.CBC;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(txtArray, 0, txtArray.Length);

            return UTF8Encoding.UTF8.GetString(resultArray);
        }

        public static string Decrypt(string txt)
        {
            return Decrypt(ConfigHelper.GetConfigString("AESKey"), ConfigHelper.GetConfigString("AESIV"), txt);
        }
}

Related Tutorials