출처 : http://h5bak.tistory.com/148






key 부분은 24자리 문자가 들어가고 24자리가 안되면 =을 마지막에 채워서 24자리를 맞추어준다.



using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Security.Cryptography;

using System.Text;

 

 

public class AES

{

    public static string Encrypt(string Input, string key)

    {

        RijndaelManaged aes = new RijndaelManaged();

        aes.KeySize = 256;

        aes.BlockSize = 128;

        aes.Mode = CipherMode.CBC;

        aes.Padding = PaddingMode.PKCS7;

        aes.Key = Encoding.UTF8.GetBytes(key);

        aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

 

        var encrypt = aes.CreateEncryptor(aes.Key, aes.IV);

        byte[] xBuff = null;

        using (var ms = new MemoryStream())

        {

            using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))

            {

                byte[] xXml = Encoding.UTF8.GetBytes(Input);

                cs.Write(xXml, 0, xXml.Length);

            }

 

            xBuff = ms.ToArray();

        }

 

        string Output = Convert.ToBase64String(xBuff);

        return Output;

    }

 

 

    public static string Decrypt(string Input, string key)

    {

        RijndaelManaged aes = new RijndaelManaged();

        aes.KeySize = 256;

        aes.BlockSize = 128;

        aes.Mode = CipherMode.CBC;

        aes.Padding = PaddingMode.PKCS7;

        aes.Key = Encoding.UTF8.GetBytes(key);

        aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

 

        var decrypt = aes.CreateDecryptor();

        byte[] xBuff = null;

        using (var ms = new MemoryStream())

        {

            using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))

            {

                byte[] xXml = Convert.FromBase64String(Input);

                cs.Write(xXml, 0, xXml.Length);

            }

 

            xBuff = ms.ToArray();

        }

 

        string Output = Encoding.UTF8.GetString(xBuff);

        return Output;

    }

}

 

Posted by motolies
,