Exemplo n.º 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");
        }
Exemplo n.º 2
0
        public void should_get_exception_when_sign_not_match()
        {
            Dictionary <String, String> paramters = new Dictionary <string, string>
            {
                { "biz_content", "test" },
                { "charset", "UTF-8" },
                { "sign", "DFM24dXl9tfymSvZvmwcjutWWDoCwF2b/6CqUY2ZdShmnucUGxKqpmDF0m4lu5cm2AKx2KxvrfyOHrpZUBhX9i3YKqj0CV1jDjWBdcFI2gGnNQeShdqGGDhuHgtVeQE8cH/Q7LWSfrJ3EZqKyvCXsAX2GQ7SuhTPbIrq8mlflcIQHIUxUfoucc2F/vBdumwYQ3bT+2P4+7z89AFrPIDQ5A4dh1l3dkHbMTaJrFAz5bSvo5gqNyVV/TLts28HeHvAAYDfGP4eWm5knnh/3AulLdOkj0+m8L/YUR9tUMvNMSB6IJFF7keKq4s6qovfXPJYGZUNfuWKp9EJwDALGD53zg==" }
            };

            AopException ex = Assert.Throws <AopException>(() => AlipaySignature.CheckSignAndDecrypt(
                                                               paramters, TestAccount.Sandbox.AlipayPublicKey, TestAccount.Sandbox.AppPrivateKey, true, true, "RSA", false));

            Assert.AreEqual(ex.Message.Contains("rsaCheck failure"), true);
        }
Exemplo n.º 3
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");
        }