AesEncode

using System;
using System.Text;
using System.Security.Cryptography;


        private static readonly byte[] aesKey = { 0x03, 0x00, 0x52, 0xc3, 0x03, 0x00, 0x52, 0xc3, 0x03, 0x00, 0x52, 0xc3, 0x03, 0x00, 0x52, 0xc3 };

        /// <summary>
        /// AES encode.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AesEncode(string value)
        {
            using (Aes aes = Aes.Create())
            {
                using (ICryptoTransform encryptor = aes.CreateEncryptor(aesKey, aesKey))
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(value);
                    buffer = encryptor.TransformFinalBlock(buffer, 0, buffer.Length);
                    return Convert.ToBase64String(buffer);
                }
            }
        }


        /// <summary>
        /// AES decode.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string AesDecode(string value)
        {
            using (Aes aes = Aes.Create())
            {
                using (ICryptoTransform decryptor = aes.CreateDecryptor(aesKey, aesKey))
                {
                    byte[] buffer = Convert.FromBase64String(value);
                    buffer = decryptor.TransformFinalBlock(buffer, 0, buffer.Length);
                    return Encoding.UTF8.GetString(buffer);
                }
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章