AES加密 AESCrypt 类

    /// <summary>
    /// AES加密
    /// </summary>
    public sealed class AESCrypt
    {
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="i_Context"></param>
        /// <returns></returns>
        static public string Encode(string text)
        {
            byte[] key = new byte[] { 132, 149, 17, 104, 128, 101, 170, 180, 191, 28, 127, 149, 144, 121, 200, 130 };
            byte[] iv = new byte[] { 198, 12, 49, 123, 101, 128, 103, 212, 112, 122, 133, 35, 150, 130, 210, 181 };
            try
            {
                RijndaelManaged rijn = new RijndaelManaged();
                ICryptoTransform trans = rijn.CreateEncryptor(key, iv);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cs);
                try
                {
                    sw.Write(text);
                    sw.Flush();
                }
                finally
                {
                    sw.Close();
                }
                byte[] _text = ms.GetBuffer();
                int k = 0;
                for (k = _text.Length - 1; k >= 0; k--)
                {
                    if (_text[k] != 0)
                    {
                        break;
                    }
                }
                int len = k + 1;
                if (len % 2 != 0)
                {//必须保证取的长度为偶数
                    len += 1;
                }
                return Convert.ToBase64String(ms.GetBuffer(), 0, len);
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="i_Context"></param>
        /// <returns></returns>
        static public string Decode(string text)
        {
            byte[] key = new byte[] { 132, 149, 17, 104, 128, 101, 170, 180, 191, 28, 127, 149, 144, 121, 200, 130 };
            byte[] iv = new byte[] { 198, 12, 49, 123, 101, 128, 103, 212, 112, 122, 133, 35, 150, 130, 210, 181 };
            try
            {
                RijndaelManaged rijn = new RijndaelManaged();
                ICryptoTransform trans = rijn.CreateDecryptor(key, iv);
                byte[] data = Convert.FromBase64String(text);
                MemoryStream ms = new MemoryStream(data);
                CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(cs);
                string r_data = string.Empty;
                try
                {
                    r_data = sr.ReadToEnd();
                }
                finally
                {
                    sr.Close();
                }
                return r_data;
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
        }

    }

    /// <summary>
    /// SHA1摘要
    /// </summary>
    public sealed class SHA1Crypt
    {
        /// <summary>
        /// Base64编码
        /// </summary>
        /// <param name="i_Content"></param>
        /// <returns></returns>
        static public string MakeCode(string text)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            byte[] cb = utf8.GetBytes(text);
            byte[] sb = sha1.ComputeHash(cb);
            return Convert.ToBase64String(sb);
        }
    }

    /// <summary>
    /// MD5摘要
    /// </summary>
    public sealed class MD5Crypt
    {
        /// <summary>
        /// 32位编码
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public string MakeCode(string text)
        {
            byte[] _bytes = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text));
            StringBuilder _result = new StringBuilder();
            for (int i = 0; i < _bytes.Length; i++)
            {
                string _hex = _bytes[i].ToString("x");
                if (_hex.Length == 1)
                {
                    _result.Append("0");
                }
                _result.Append(_hex);
            }
            return _result.ToString();
        }
    }

 

posted @ 2014-04-22 14:09  dragon.net  阅读(717)  评论(0编辑  收藏  举报