探讨 RSACryptoServiceProvider 数字签名问题!
1、先用 RSACryptoServiceProvider 生成公、私钥
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); privateK = rsa.ToXmlString(true); publicK = rsa.ToXmlString(false);
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { try { rsa.FromXmlString(privateK); // 加密对象 RSAPKCS1SignatureFormatter f = new RSAPKCS1SignatureFormatter(rsa); f.SetHashAlgorithm("SHA1"); //hash后的数据只能通过密钥解密(为了保证数据的安全,可以用对称加密加密一下数据) byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes("shuju"); SHA1Managed sha = new SHA1Managed(); byte[] result = sha.ComputeHash(source); string s = Convert.ToBase64String(result); byte[] b = f.CreateSignature(result); str = Convert.ToBase64String(b); succes(); } catch (Exception es) { fail(); } }
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { try { rsa.FromXmlString(publicK); RSAPKCS1SignatureDeformatter f = new RSAPKCS1SignatureDeformatter(rsa); f.SetHashAlgorithm("SHA1"); byte[] key = Convert.FromBase64String(str); SHA1Managed sha = new SHA1Managed(); byte[] name = sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes("shuju")); string s = Convert.ToBase64String(name); if (f.VerifySignature(name, key)) succes(); else fail(); } catch (Exception ee) { fail(); } }