zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    可自主二次开发的微信云控客服crm系统软件(带源码)
    个人微信号二次开发sdk协议,微信个人号开发API接口
    最新的微信SCRM客服系统
    微信个人号客服系统淘宝客发单机器人sdk服务端接口列表
    sdk定制开发微信群控云控客服系统教程
    web版微信自动发消息(实现微信个人号机器人)
    Adobe Audition 基本使用
    MPEG-7 视觉描述符
    图像检索:几种基于纹理特征的图像检索算法
    div+css基础教程
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3680428.html
Copyright © 2011-2022 走看看