Aes Decrypt String with key - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:AES

Description

Aes Decrypt String with key

Demo Code


using System.Text;
using System.Security.Cryptography;
using System;//from   w w w  .  j a  va  2 s . com
using Org.BouncyCastle.Utilities.Encoders;

public class Main{
        public static string AesDecrypt(string str, string key)
        {
            if (string.IsNullOrEmpty(str)) return null;
            byte[] toEncryptArray = Convert.FromBase64String(str);
            byte[] keyByte = Convert.FromBase64String(key);

            System.Security.Cryptography.RijndaelManaged aes = new System.Security.Cryptography.RijndaelManaged
            {
                Key = keyByte,
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

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

            return Encoding.GetString(resultArray);
        }
}

Related Tutorials