• 加密解密类


    public static class EncryptUtils
    {

    /// <summary>
    /// 解析二次加密后的token
    /// </summary>
    /// <param name="token"></param>
    /// <param name="userId">返回解密后的Userid,如果是-1就需要调用Auth重新验证</param>
    /// <returns></returns>
    public static int DecryptToken(string token)
    {
    if (string.IsNullOrEmpty(token))
    {
    return -1;
    }

    if (token.Contains("DXoY1U2iK=") && token.Contains("sFd4DsAf82aS5+"))
    {
    var tokenSplit = token.Split(new string[] { "DXoY1U2iK=", "sFd4DsAf82aS5+" }, StringSplitOptions.RemoveEmptyEntries);
    if (tokenSplit.Length < 3)
    {
    return -1;
    }
    else
    {
    var timeStr = SOA.Api.ProjectTool.DESEncryptHelper.Base64Decrypt(tokenSplit[1], "159862");
    var userStr = SOA.Api.ProjectTool.DESEncryptHelper.Base64Decrypt(tokenSplit[2], "2674536");
    DateTime time;
    if (!DateTime.TryParse(timeStr, out time))
    {
    return -1;
    }
    if (time < DateTime.Now)
    {
    return -1;
    }
    int userId2;
    if (!int.TryParse(userStr, out userId2))
    {
    return -1;
    }
    return userId2;
    }
    }
    else
    {
    return -1;
    }
    }

    public static string Md5Code(string str)
    {
    StringBuilder PwdSb = new StringBuilder();
    string cl = str;
    MD5 md5 = MD5.Create();//实例化一个md5对像
    // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
    byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
    // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
    for (int i = 0; i < s.Length; i++)
    {
    // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
    //pwd = pwd + s[i].ToString("X2");
    PwdSb.Append(s[i].ToString("X2"));
    }
    return PwdSb.ToString();
    }

    /// <summary>
    /// GB2312编码方式 MD5加密
    /// </summary>
    /// <param name="encypStr"></param>
    /// <param name="charset"></param>
    /// <returns></returns>
    public static string GetMD5(string encypStr, string charset)
    {
    string retStr;
    MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();

    //创建md5对象
    byte[] inputBye;
    byte[] outputBye;

    //使用GB2312编码方式把字符串转化为字节数组.
    try
    {
    inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
    }
    catch (Exception ex)
    {
    inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
    }
    outputBye = m5.ComputeHash(inputBye);

    retStr = System.BitConverter.ToString(outputBye);
    retStr = retStr.Replace("-", "").ToUpper();
    return retStr;
    }

    /// <summary>
    /// Base64加密
    /// </summary>
    /// <param name="Message"></param>
    /// <returns></returns>
    public static string Base64Code(string Message)
    {
    byte[] bytes = Encoding.Default.GetBytes(Message);
    return Convert.ToBase64String(bytes);
    }

    /// <summary>
    /// Base64解密
    /// </summary>
    /// <param name="Message"></param>
    /// <returns></returns>
    public static string Base64Decode(string Message)
    {
    byte[] bytes = Convert.FromBase64String(Message);
    return Encoding.Default.GetString(bytes);
    }

    }

  • 相关阅读:
    编译gcc报错make[3]: Leaving directory `/usr/local/src/gcc-7.4.0/build/gcc' make[2]: *** [all-stage1-gcc] Error 2 处理
    ERROR 1176 (42000): Key 'XXX' doesn't exist in table 'XXX'报错处理
    /lib64/libc.so.6: version `GLIBC_2.18' not found报错解决
    Centos7上pkg-config的安装
    ERROR: Error in Log_event::read_log_event(): 'Found invalid event in binary log', data_len: 31, event_type: 35报错处理
    MySQL5.7主从复制slave报Last_Errno: 1146错误解决
    详述 hosts 文件的作用及修改 hosts 文件的方法
    Java Decompiler(Java反编译工具)
    使用Charles代理工具导致电脑无法正常访问网站(您的连接不是私密连接)
    阿里云服务器Svn-Server无法连接,阿里云服务器SVNServer配置
  • 原文地址:https://www.cnblogs.com/guzhengtao/p/20180706_1557.html
Copyright © 2020-2023  润新知