Encrypt AES - CSharp Security

CSharp examples for Security:Cryptography

Description

Encrypt AES

Demo Code


using System.Web.UI.WebControls;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.IO.Compression;
using System.IO;//from   ww  w .  j  ava  2  s  .co  m
using System.Diagnostics;
using System.Collections.Generic;
using System;

public class Main{
        public static String EncryptAES(String str, string aeskey)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(aeskey);
            Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);

            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
}

Related Tutorials