Ejemplo n.º 1
0
        /// <summary>
        /// 验证返回答名与值
        /// </summary>
        /// <param name="signContent"></param>
        /// <param name="sign"></param>
        /// <param name="charset"></param>
        /// <returns></returns>
        bool RSACheckContent(string signContent, string sign, string charset, string signType)
        {
            try
            {
                var sPublicKeyPEM = File.ReadAllText("D:/alipay/alipay_rsa_public_key.pem");
                if ("RSA2".Equals(signType))
                {
                    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);

                    bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), "SHA256", Convert.FromBase64String(sign));
                    return(bVerifyResultOriginal);
                }
                else
                {
                    var rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);
                    var sha1                  = SHA1.Create();
                    var signBase64            = Convert.FromBase64String(sign);
                    var bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, signBase64);

                    return(bVerifyResultOriginal);
                }
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 2
0
        public static bool RSACheckSign(IDictionary <string, string> parameters, string sign, string signType = "RSA")
        {
            string charset = "UTF-8";

            try
            {
                string signContent = GetSignDataContent(parameters, true);
                if ("RSA2".Equals(signType))
                {
                    string sPublicKeyPEM = File.ReadAllText(AliPayPublicKey);

                    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);

                    bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), "SHA256", Convert.FromBase64String(sign));
                    return(bVerifyResultOriginal);
                }
                else
                {
                    string sPublicKeyPEM         = File.ReadAllText(AliPayPublicKey);
                    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);

                    SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                    bool bVerifyResultOriginal     = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, Convert.FromBase64String(sign));
                    return(bVerifyResultOriginal);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 3
0
        public void Test1()
        {
            try
            {
                string prikey = "MIIEpAIBAAKCAQEAqOTxQbDUUkm/CS/eVTHgARhqCfs5mWe/FImEIDyPrsExswLUbc6pIXBulqtYOJ9Nl4fljNgXJbh4CyoUtiM5U9jf5yoPthJq667rPwtRVOIDnmNWKZNHponXjFBQs/juNkUgdX4kcWx695cWGkm+SRXCQQsKcSsqubFwLgXRP9HErfPwColtfmwVZVNXWA4ZtHTrSczbVDplMS8Fg73eprwIxYvpOFxFhAAwC4+Xw753Zd+u0P05YhXrQ/76g1YqNBkiHlOpWtwLVdL+zU9Nk5PxRGgjUqvJpDUxwjEHCEDxD4fxdgS+ml5pm0SsRKyXFm2+ZyPDlu7LQdx28kgAeQIDAQABAoIBAQCZvGQa/qIWEUjSTP9dn1uew6FqWoLwr64QPKjHSzkiwnrBzradCXuMk7ImdeuFBwTzBOGfledkD4k96F0X+fIp74TH/9JzUkp/JCbAqUWsNVtD5no8t/KFln1dHJKJb/Mt9Th5mowDrtIz4xRcc3nBPwwmdq1XnAp1Ix3Q1rzFEGX4Ndce6q2YkbpAMKgl3HeIbgKrKHOxmJJ9hJ3rUzc24D+e2dR9eYCym576RrxxuHISysa4N41MSdZq5jXaaWmwIGg68v5wEVJcEkmAc91webKAfy1SVdQiAl8I1xSI3JDUXloWrUAX3ZnRcknjEmhcs7by5QrOnbTd8ZR5Mkt9AoGBAPU4QLSj5QdNz+HjoRdfxda2lK1OPy3O/jReCE6NnqTbvVygQjFdlmYnGFh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YvpOFxFhAAwC4+Xw753Zd+u0P05YhXrQ/76g1YqNBkiHlOpWtwLVdL+zU9Nk5PxRGgjUqvJpDUxwjEHCEDxD4fxdgS+ml5pm0SsRKyXFm2+ZyPDlu7LQdx28kgAeQIDAQAB";

                string abc = "abc123";

                RSACryptoServiceProvider rsaCsp = AlipaySignature.LoadCertificateString(prikey, "RSA2");
                string r1 = Convert.ToBase64String(AlipaySignature.RSAEncrypt(rsaCsp, Encoding.UTF8.GetBytes(abc)));
                string r2 = Encoding.UTF8.GetString(AlipaySignature.RSADecrypt(rsaCsp, Convert.FromBase64String(r1)));

                var sPublicKeyPEM = "-----BEGIN PUBLIC KEY-----\r\n";
                sPublicKeyPEM += pubkey;
                sPublicKeyPEM += "-----END PUBLIC KEY-----\r\n\r\n";
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.PersistKeyInCsp = false;
                RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);
                string r11 = Convert.ToBase64String(AlipaySignature.RSAEncrypt(rsa, Encoding.UTF8.GetBytes(abc)));
                string r22 = Encoding.UTF8.GetString(AlipaySignature.RSADecrypt(rsaCsp, Convert.FromBase64String(r1)));
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 4
0
        public static bool verify2(string content, string signedString, string publicKey, string input_charset)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            rsa.PersistKeyInCsp = false;
            RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, publicKey);
            bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(input_charset).GetBytes(content), "SHA256", Convert.FromBase64String(signedString));

            return(bVerifyResultOriginal);
        }
Ejemplo n.º 5
0
        public void Test()
        {
            string pubkey        = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwVpnkgBwj0046UHTXFcd0DmfORewgQXHBs+tggYex49kc9NIkYrFGi8aPjxA3J9azR6vUcY5Hso4W3FaretNm3b7zt9jFdmvZfEftgNEG4buuQ+/NrG5XvexrOxkIy0DZ9XMNPt8lWf3LIGYZguUnvKJhlBnUbIdTp4/A+EkPG2khV4O31ajm5i7oxTVOR/I9tpcG0i1QXARGYI2WZY9TEtuSHWWAyI0+TYsam1Ii4j+9TUxzqecWX+iOmFCMIr0Hq6xDxLUjXRN5CBKIuv6bHtYQdwZ+CKt9pdj8K7K4+b0cLWX6dFZdrbm0Gb6UEIZZGwC7fUInXt4PvNxFOGySwIDAQAB";
            var    sPublicKeyPEM = "-----BEGIN PUBLIC KEY-----\r\n";

            sPublicKeyPEM += pubkey;
            sPublicKeyPEM += "-----END PUBLIC KEY-----\r\n\r\n";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            rsa.PersistKeyInCsp = false;
            RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);
            string r11 = Convert.ToBase64String(AlipaySignature.RSAEncrypt(rsa, Encoding.UTF8.GetBytes("中123abc")));
        }
Ejemplo n.º 6
0
        public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset, string signType, bool keyFromFile)
        {
            try
            {
                if (string.IsNullOrEmpty(charset))
                {
                    charset = "utf-8";
                }

                string sPublicKeyPEM;

                if (keyFromFile)
                {
                    sPublicKeyPEM = File.ReadAllText(publicKeyPem);
                }
                else
                {
                    sPublicKeyPEM  = "-----BEGIN PUBLIC KEY-----\r\n";
                    sPublicKeyPEM += publicKeyPem;
                    sPublicKeyPEM += "-----END PUBLIC KEY-----\r\n\r\n";
                }


                if ("RSA2".Equals(signType))
                {
                    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);

                    bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), "SHA256", Convert.FromBase64String(sign));
                    return(bVerifyResultOriginal);
                }
                else
                {
                    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                    rsa.PersistKeyInCsp = false;
                    RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);

                    SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
                    bool bVerifyResultOriginal     = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, Convert.FromBase64String(sign));
                    return(bVerifyResultOriginal);
                }
            }
            catch
            {
                return(false);
            }
        }