Aes Encoding - CSharp System.Security.Cryptography

CSharp examples for System.Security.Cryptography:AES

Description

Aes Encoding

Demo Code


using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System;/*from   w  w w  .ja  v a  2 s.  c  o m*/

public class Main{
        /// AES encryption
        public static string AesEncoding(string data, string key, Encoding encoding)
        {
            var hashMd5 = new MD5CryptoServiceProvider();
            byte[] keyArray = hashMd5.ComputeHash(encoding.GetBytes(key));
            byte[] toEncryptArray = encoding.GetBytes(data);
            RijndaelManaged rijndaelManaged = new System.Security.Cryptography.RijndaelManaged();
            rijndaelManaged.Key = keyArray;
            rijndaelManaged.Mode = System.Security.Cryptography.CipherMode.ECB;
            rijndaelManaged.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            ICryptoTransform cTransform = rijndaelManaged.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
}

Related Tutorials