/// <summary> /// Verify signature with public key /// </summary> /// <param name="publicKey">Public key</param> /// <param name="data">Original data</param> /// <param name="signature">Signed data (Signature)</param> /// <returns>True(Verify OK)/False(Verify NG)</returns> public async Task <bool> VerifySignatureAsync(string publicKey, string data, string signature) { using (var rsaUtil = new RsaPkcs8Util(this.encoding, publicKey, string.Empty, this.keySize)) { var isOk = rsaUtil.VerifyData(data, signature, this.hashAlgorithm, this.signaturePadding); return(await Task.FromResult(isOk)); } }
/// <summary> /// JavaRsa公钥签名验证RSAWithSHA256 /// </summary> /// <param name="text">源数据</param> /// <param name="sign">签名</param> /// <param name="publicKey">java pem公钥</param> /// <param name="signFormat">base64/hex</param> public static bool JavaRsaSignVerify(string text, string sign, string publicKey, RsaSignFormat signFormat = RsaSignFormat.Base64) { var rsaProvider = new RsaPkcs8Util(Encoding.UTF8, publicKey); if (signFormat == RsaSignFormat.Hex) // => byte => base64 { sign = Convert.ToBase64String(FromHexString(sign)); } return(rsaProvider.VerifyData(text, sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); }
//验签 public static bool verifySign(string data, string sign) { try { RsaPkcs8Util m_pPublicRsaPkcs8Util = new RsaPkcs8Util(Encoding.GetEncoding(m_cConfigConstants.SYSTEM_ENCODING), m_cConfigConstants.PUBLIC_KEY); return(m_pPublicRsaPkcs8Util.VerifyData(digest(data), sign, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1)); } catch (Exception ex) { Log.Instance.Debug(ex); return(false); } }
/// <summary> /// RSA 加密,解密,签名和验签 /// </summary> public static void RSASignFunc(string privateKey, string publicKey, EnumUserType enumUserType) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); switch (enumUserType) { case EnumUserType.xml: RsaXmlUtil rsaXmlUtil = new RsaXmlUtil(Encoding.UTF8, publicKey, privateKey); var encrypt = rsaXmlUtil.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput = rsaXmlUtil.Decrypt(encrypt, RSAEncryptionPadding.Pkcs1); Console.Write("XML加解密是否成功:"); Console.WriteLine(encryptInput is "123456789"); var sign = rsaXmlUtil.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData = rsaXmlUtil.VerifyData("987654321", sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("XML签名是否成功"); Console.WriteLine(verifyData); break; case EnumUserType.pkcs1: RsaPkcs1Util rsaPkcs1Util = new RsaPkcs1Util(Encoding.UTF8, publicKey, privateKey); var encrypt2 = rsaPkcs1Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput2 = rsaPkcs1Util.Decrypt(encrypt2, RSAEncryptionPadding.Pkcs1); Console.Write("PKCS1加解密是否成功:"); Console.WriteLine(encryptInput2 is "123456789"); var sign2 = rsaPkcs1Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData2 = rsaPkcs1Util.VerifyData("987654321", sign2, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("PKCS1签名是否成功"); Console.WriteLine(verifyData2); break; default: RsaPkcs8Util rsaPkcs8Util = new RsaPkcs8Util(Encoding.UTF8, publicKey, privateKey); var encrypt3 = rsaPkcs8Util.Encrypt("123456789", RSAEncryptionPadding.Pkcs1); var encryptInput3 = rsaPkcs8Util.Decrypt(encrypt3, RSAEncryptionPadding.Pkcs1); Console.Write("PKCS8加解密是否成功:"); Console.WriteLine(encryptInput3 is "123456789"); var sign3 = rsaPkcs8Util.SignData("987654321", HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); var verifyData3 = rsaPkcs8Util.VerifyData("987654321", sign3, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1); Console.Write("PKCS8签名是否成功"); Console.WriteLine(verifyData3); break; } }
public static void getSign() { //var extReserved = HttpUtility.UrlEncode("Here to fill in the Merchant reservation information", Encoding.UTF8); //var sysReserved = HttpUtility.UrlEncode("on2k0wSrVHUWcx-Bn66rGwlEp0pw", Encoding.UTF8); var extReserved = "Here to fill in the Merchant reservation information"; var sysReserved = "on2k0wSrVHUWcx-Bn66rGwlEp0pw"; string data = "accessMode=0&amount=0.01&bankId=¤cy=&extReserved=" + extReserved + "¬ifyTime=1591930286051&orderId=WXd095362b5bb24bb4fa08239118c402&orderTime=2020-06-12 10:51:15&payType=17&productName=测试商品&requestId=dd957ade-c4d2-4c03-a2e1-abd800b2dad2&result=0&spending=0.00&sysReserved=" + sysReserved + "&timeOffset=&tradeTime=2020-06-12 10:51:25&userName=890086000300106484"; var rightSign = "c+0xqmQDApR3P6qn1wBhUP7dZ0mU+Nghmg9ddtI4V8Vd0QOvFGDItR2rEERbf00EjGONjhhCoEtSja6ezTbPjnKdDNfjgwbvTZ0ljneR1BrT3UjJvyilfRLxDPmu1I2ag5AYCfI5Bnb0TGgfFjDsLzNg+Mwpa8sJmddaF2xUynNA6o92IHeczr4WJwMtFFXKCtu2esmgNlyPfN5eT/8mZzKGiuR3QgZ7FHkcmHn0cFrv12pr7W1/LwnQW4gBJhm+mDXmWCGQpKGx5hYykX7VIwYoqtG1q4idTqHfUwh4zMYgLwnPb2xE1yDw0Pgj7fRbG45/3aKVFeALN3w5MjJrUQ=="; var publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwqTS1Jw8CS4w2Mb9Dp1TmQjnYvYpqj6ExZ7R/xzFwwEJXXUdak9WgNd13nWK7IBfNjHNTYVVaeY4BRn9uak5L3UuCaSMdpQZ4QMPMqfNoHr5oh5qOc3v23x9UEUg0ZybehI7serCa2tVro34hRKgHNSQTucWUHA61L6NAKieUP7lmUMwnbG6Q/KLvfAXX919CgFWRLDj8UjbyyId5cpM7nHI3RPbmI7+0MphrepG/h2MHKM3HKX03zEFhdS/ZslocJINipwLyO8kbt4DWfzdOjFI7TUxaJcZGkKAzZ4/+zmiHa/snt5i10QcZwri8wMQxlNquRu1cmYiXxVVxGqugwIDAQAB"; //rsa256 var b = new RsaPkcs8Util(Encoding.UTF8, publicKey); var ok = b.VerifyData(data, rightSign, System.Security.Cryptography.HashAlgorithmName.SHA256, System.Security.Cryptography.RSASignaturePadding.Pkcs1); Console.WriteLine(ok.ToString()); }
public bool CheckSign(string data, string sign) { return(rsaUtil.VerifyData(data, sign, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); }