c# Aes加解密和对象序列化

 

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; }

        }

    }

 

这两个功能一起使用时可以用来在cookie中存储登陆信息等。

 

 

你可能感兴趣的:(序列化)