日期:2014-05-18 浏览次数:20818 次
protected void Button1_Click(object sender, EventArgs e) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { // 公钥 string pubkey = rsa.ToXmlString(false); txtPublic.Text = pubkey; // 私钥 string prikey = rsa.ToXmlString(true); txtPrivate.Text = prikey; } } /// <summary> /// 加密 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { string prikey = txtPrivate.Text; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(prikey); // 加密对象 RSAPKCS1SignatureFormatter f = new RSAPKCS1SignatureFormatter(rsa); f.SetHashAlgorithm("SHA1"); byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(sUserName.Text); SHA1Managed sha = new SHA1Managed(); byte[] result = sha.ComputeHash(source); byte[] b = f.CreateSignature(result); sCode.Text = Convert.ToBase64String(b); } } /// <summary> /// 验证注册码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button3_Click(object sender, EventArgs e) { string pubkey = txtPublic.Text; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { rsa.FromXmlString(pubkey); RSAPKCS1SignatureDeformatter f = new RSAPKCS1SignatureDeformatter(rsa); f.SetHashAlgorithm("SHA1"); byte[] key = Convert.FromBase64String(cCode.Text); SHA1Managed sha = new SHA1Managed(); byte[] name = sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cUserName.Text)); if (f.VerifySignature(name, key)) msg.Text = "验证成功"; else msg.Text = "不成功"; } } }