c#Aes復号化とオブジェクトシーケンス化

11186 ワード


aes復号化
public class AesCryptto

    {

        private string key = "hjyf57468jhmuist";

        private string iv = "ud547io0d98eddl2d";



        /// <summary>

        /// aes 

        /// </summary>

        /// <param name="str"></param>

        /// <returns></returns>

        public string Encrpty(string str)

        {

            try

            {

                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

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

                aes.IV = Encoding.UTF8.GetBytes(iv);

                aes.Mode = CipherMode.CBC;

                aes.Padding = PaddingMode.Zeros;



                MemoryStream memory = new MemoryStream();



                using (CryptoStream stream = new CryptoStream(memory, aes.CreateEncryptor(), CryptoStreamMode.Write))

                {

                    byte[] buffer = Encoding.UTF8.GetBytes(str);

                    stream.Write(buffer, 0, buffer.Length);

                    stream.FlushFinalBlock();

                }



                string result = Convert.ToBase64String(memory.ToArray());



                // url , "/" 

                result = Uri.EscapeDataString(result);



                // url , "/" , 

                //result = Uri.EscapeDataString(result);



                return result;

            }

            catch

            {

                return str;

            }

        }



        /// <summary>

        /// aes 

        /// </summary>

        /// <param name="str"></param>

        /// <returns></returns>

        public string DeEncrpty(string str)

        {

            try

            {

                AesCryptoServiceProvider aes = new AesCryptoServiceProvider();

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

                aes.IV = Encoding.UTF8.GetBytes(iv);

                aes.Mode = CipherMode.CBC;

                aes.Padding = PaddingMode.Zeros;



                str = Uri.UnescapeDataString(str);

                byte[] buffer = Convert.FromBase64String(str);

                MemoryStream memory = new MemoryStream(buffer);



                using (CryptoStream stream = new CryptoStream(memory, aes.CreateDecryptor(), CryptoStreamMode.Write))

                {

                    stream.Write(buffer, 0, buffer.Length);

                    stream.FlushFinalBlock();



                }



                return Encoding.UTF8.GetString(memory.ToArray());

            }

            catch

            {

                return str;

            }

        }

    }

 
オブジェクトのシーケンス化
public class SerializeHelper

    {

        /// <summary>

        ///  

        /// </summary>

        /// <param name="user"></param>

        /// <returns></returns>

        public string Serialize<T>(T entity)

        {

            try

            {

                BinaryFormatter formatter = new BinaryFormatter();



                MemoryStream memory = new MemoryStream();



                formatter.Serialize(memory, entity);



                return Encoding.Default.GetString(memory.ToArray());

            }

            catch

            {

                return "";

            }

        }



        /// <summary>

        ///  

        /// </summary>

        /// <param name="str"></param>

        /// <returns></returns>

        public T DeSerialize<T>(string str)

        {

            try

            {

                BinaryFormatter formatter = new BinaryFormatter();

                MemoryStream memory = new MemoryStream(Encoding.Default.GetBytes(str));



                return (T)formatter.Deserialize(memory);

            }

            catch

            {

                return default(T);

            }

        }

    }

オブジェクトをシーケンス化する場合は、クラスの前に[Serializable()]を付ける必要があります.あるフィールドをシーケンス化できないようにするには、そのフィールドの前に[NonSerialized()]を付ける必要があります.属性は無効です.
[Serializable()]

    public class User

    {

        public string LoginName { get; set; }



        public string Password { get; set; }



        [NonSerialized()]

        private DateTime regTime;



        public DateTime RegTime

        {

            get { return regTime; }

            set { regTime = value; }

        }

    }

 
この2つの機能を併用すると、クッキーにログイン情報などを格納するのに使用できます.