/// <summary>尝试解码令牌,即使失败,也会返回用户信息和有效时间</summary> /// <param name="token">令牌</param> /// <param name="user">用户信息</param> /// <param name="expire">有效时间</param> /// <returns>解码结果,成功或失败</returns> public Boolean TryDecode(String token, out String user, out DateTime expire) { if (token.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(token)); } if (Key.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Key)); } user = null; expire = DateTime.MinValue; // Base64拆分数据和签名 var p = token.IndexOf('.'); var data = token.Substring(0, p).ToBase64(); var sig = token.Substring(p + 1).ToBase64(); // 拆分数据和有效期 var str = data.ToStr(); p = str.LastIndexOf(','); user = str.Substring(0, p); var secs = str.Substring(p + 1).ToInt(); expire = secs.ToDateTime(); // 验证签名 //if (!DSAHelper.Verify(data, Key, sig)) throw new InvalidOperationException("签名验证失败!"); if (!DSAHelper.Verify(data, Key, sig)) { return(false); } return(true); }
/// <summary>令牌解码得到用户和有效期</summary> /// <param name="token">令牌</param> /// <param name="expire">有效期</param> /// <returns></returns> public String Decode(String token, out DateTime expire) { if (token.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(token)); } if (Key.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(Key)); } expire = DateTime.MinValue; // Base64拆分数据和签名 var p = token.IndexOf('.'); var data = token.Substring(0, p).ToBase64(); var sig = token.Substring(p + 1).ToBase64(); // 验证签名 //if (!DSAHelper.Verify(data, Key, sig)) throw new InvalidOperationException("签名验证失败!"); if (!DSAHelper.Verify(data, Key, sig)) { return(null); } // 拆分数据和有效期 var str = data.ToStr(); p = str.LastIndexOf(','); var user = str.Substring(0, p); var secs = str.Substring(p + 1).ToInt(); expire = new DateTime(1970, 1, 1).AddSeconds(secs); return(user); }
private void btnDSA2_Click(Object sender, EventArgs e) { var buf = GetBytes(); var pass = rtPass.Text; var v = rtResult.Text; if (v.Contains("\n\n")) { v = v.Substring(null, "\n\n"); } var sign = GetBytes(v); var rs = DSAHelper.Verify(buf, pass, sign); if (rs) { MessageBox.Show("验证通过", "DSA数字签名", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("验证失败", "DSA数字签名", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void btnDSA2_Click(Object sender, EventArgs e) { var buf = GetBytes(); var pass = rtPass.Buffer.Text; var v = rtResult.Buffer.Text; if (v.Contains("\n\n")) { v = v.Substring(null, "\n\n"); } var sign = GetBytes(v); var rs = DSAHelper.Verify(buf, pass, sign); if (rs) { MessageBox.Show("DSA数字签名验证通过"); } else { MessageBox.Show("DSA数字签名验证失败"); } }