加密解密类

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

}

posted @ 2018-07-06 15:58  GU天乐乐乐!  阅读(148)  评论(1编辑  收藏  举报