Пример #1
0
        private static void CheckSignAndDecrypt()
        {
            // 参数构建
            var charset    = "UTF-8";
            var bizContent =
                "<XML><AppId><![CDATA[2013082200024893]]></AppId><FromUserId><![CDATA[2088102122485786]]></FromUserId><CreateTime>1377228401913</CreateTime><MsgType><![CDATA[click]]></MsgType><EventType><![CDATA[event]]></EventType><ActionParam><![CDATA[authentication]]></ActionParam><AgreementId><![CDATA[201308220000000994]]></AgreementId><AccountNo><![CDATA[null]]></AccountNo><UserInfo><![CDATA[{\"logon_id\":\"15858179811\",\"user_name\":\"许旦辉\"}]]></UserInfo></XML>";
            var publicKeyPem  = Path.Combine(GetCurrentPath(), "public-key.pem");
            var privateKeyPem = Path.Combine(GetCurrentPath(), "aop-sandbox-RSA-private-c#.pem");

            if (!File.Exists(publicKeyPem))
            {
                throw new FileNotFoundException();
            }
            if (!File.Exists(privateKeyPem))
            {
                throw new FileNotFoundException();
            }
            IDictionary <string, string> paramsMap = new Dictionary <string, string>();

            paramsMap.Add("biz_content", AlipaySignature.RSAEncrypt(bizContent, publicKeyPem, charset));
            paramsMap.Add("charset", charset);
            paramsMap.Add("service", "alipay.mobile.public.message.notify");
            paramsMap.Add("sign_type", "RSA");
            paramsMap.Add("sign", AlipaySignature.RSASign(paramsMap, privateKeyPem, null, "RSA"));

            // 验签&解密
            var resultContent = AlipaySignature.CheckSignAndDecrypt(paramsMap, publicKeyPem, privateKeyPem, true, true);

            Console.Write("resultContent=" + resultContent + "\n\r");
        }
Пример #2
0
        public void Test1()
        {
            try
            {
                string prikey = "MIIEpAIBAAKCAQEAqOTxQbDUUkm/CS/eVTHgARhqCfs5mWe/FImEIDyPrsExswLUbc6pIXBulqtYOJ9Nl4fljNgXJbh4CyoUtiM5U9jf5yoPthJq667rPwtRVOIDnmNWKZNHponXjFBQs/juNkUgdX4kcWx695cWGkm+SRXCQQsKcSsqubFwLgXRP9HErfPwColtfmwVZVNXWA4ZtHTrSczbVDplMS8Fg73eprwIxYvpOFxFhAAwC4+Xw753Zd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gBhLss3VGfel12/LL6BftySZw6/g/MXOYd6K18koN4spnjLjCUGnuI5CLio1ZMlcZOoww+NNrS1OLrCJSg84+c0CgYAkw6xcfaKLH39+jdc2DLVrip4JYv+p4v9GuQbn1C1aqjaxVR5ZjbYuIaJXhr8Z0qIFaYENBVAA0AM75lkZUcYwVNiIfSA2WSFlymoy90ovPIQaG5jaQXRv8hrjRJ1zQ/l5JzhngERVbi4BBcz2QfC9GxsEa4CYGciI/sMH44fUvg==";
                string pubkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqOTxQbDUUkm/CS/eVTHgARhqCfs5mWe/FImEIDyPrsExswLUbc6pIXBulqtYOJ9Nl4fljNgXJbh4CyoUtiM5U9jf5yoPthJq667rPwtRVOIDnmNWKZNHponXjFBQs/juNkUgdX4kcWx695cWGkm+SRXCQQsKcSsqubFwLgXRP9HErfPwColtfmwVZVNXWA4ZtHTrSczbVDplMS8Fg73eprwIxYvpOFxFhAAwC4+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)
            {
            }
        }
Пример #3
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")));
        }
Пример #4
0
        public static void CheckSignAndDecrypt()
        {
            string text    = "UTF-8";
            string content = "<XML><AppId><![CDATA[2013082200024893]]></AppId><FromUserId><![CDATA[2088102122485786]]></FromUserId><CreateTime>1377228401913</CreateTime><MsgType><![CDATA[click]]></MsgType><EventType><![CDATA[event]]></EventType><ActionParam><![CDATA[authentication]]></ActionParam><AgreementId><![CDATA[201308220000000994]]></AgreementId><AccountNo><![CDATA[null]]></AccountNo><UserInfo><![CDATA[{\"logon_id\":\"15858179811\",\"user_name\":\"许旦辉\"}]]></UserInfo></XML>";
            string text2   = GetCurrentPath() + "public-key.pem";
            string text3   = GetCurrentPath() + "aop-sandbox-RSA-private-c#.pem";
            IDictionary <string, string> dictionary = new Dictionary <string, string>();

            dictionary.Add("biz_content", AlipaySignature.RSAEncrypt(content, text2, text));
            dictionary.Add("charset", text);
            dictionary.Add("service", "alipay.mobile.public.message.notify");
            dictionary.Add("sign_type", "RSA");
            dictionary.Add("sign", AlipaySignature.RSASign(dictionary, text3, null, "RSA"));
            string str = AlipaySignature.CheckSignAndDecrypt(dictionary, text2, text3, isCheckSign: true, isDecrypt: true);

            Console.Write("resultContent=" + str + "\n\r");
        }
        public static void CheckSignAndDecrypt()
        {
            // 参数构建
            string charset       = "UTF-8";
            string bizContent    = "<XML><AppId><![CDATA[2013082200024893]]></AppId><FromUserId><![CDATA[2088102122485786]]></FromUserId><CreateTime>1377228401913</CreateTime><MsgType><![CDATA[click]]></MsgType><EventType><![CDATA[event]]></EventType><ActionParam><![CDATA[authentication]]></ActionParam><AgreementId><![CDATA[201308220000000994]]></AgreementId><AccountNo><![CDATA[null]]></AccountNo><UserInfo><![CDATA[{\"logon_id\":\"15858179811\",\"user_name\":\"许旦辉\"}]]></UserInfo></XML>";
            string publicKeyPem  = GetCurrentPath() + "public-key.pem";
            string privateKeyPem = GetCurrentPath() + "aop-sandbox-RSA-private-c#.pem";
            IDictionary <string, string> paramsMap = new Dictionary <string, string>
            {
                { "biz_content", AlipaySignature.RSAEncrypt(bizContent, publicKeyPem, charset) },
                { "charset", charset },
                { "service", "alipay.mobile.public.message.notify" },
                { "sign_type", "RSA" }
            };

            paramsMap.Add("sign", AlipaySignature.RSASign(paramsMap, privateKeyPem, null, "RSA"));

            // 验签&解密
            string resultContent = AlipaySignature.CheckSignAndDecrypt(paramsMap, publicKeyPem, privateKeyPem, true, true);

            System.Console.Write("resultContent=" + resultContent + "\n\r");
        }