public RSATest()
        {
            string publicKey  = "<RSAKeyValue><Modulus>raDY9rAJpWngHhliTIyOskUswZfUVU4h3AKzw/UpE3JOZEyDJkFaBLmPWG0YhkfaSAzTTyrw1YEsIb8hVdyOp/NCexmQir/1o5PEH+njXLCxU6s94cKRRjXhsUycBUL+FJTfFd7+G/VNUvIkiBznfjIxcl7gDCE2JFccFNeOBeU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
            string privateKey = "<RSAKeyValue><Modulus>raDY9rAJpWngHhliTIyOskUswZfUVU4h3AKzw/UpE3JOZEyDJkFaBLmPWG0YhkfaSAzTTyrw1YEsIb8hVdyOp/NCexmQir/1o5PEH+njXLCxU6s94cKRRjXhsUycBUL+FJTfFd7+G/VNUvIkiBznfjIxcl7gDCE2JFccFNeOBeU=</Modulus><Exponent>AQAB</Exponent><P>488kZrKIYANelwBfhn/tpq0lygJLu1UKc1Fe9fDbVpeNS/QTs09BJKJ+hytBoLWcAdlBgIgwkrzuIQ+aoudG9Q==</P><Q>wx1J6Os2EtAnrCV6IKXyTD9k+FRJrk51PcrHbF/ocg0SWwsMEr/Uvjl7F277Fc3gLJoAlH/Q9qXEWB7z/JMNMQ==</Q><DP>LcMVr9+bT5Y9sR2tEsquBbZwieTuK6KIp+OnrMYGeZ2WD7TnJDT+hAp0WljRJSR2Q1TOOLCi+UckmtWUnbbR5Q==</DP><DQ>YuFr/IN/Yudkw6Q2kWcoNSYHiKZGtPSEDC3BBRZR2cLwI32kv7fCHClLTWPsEW/FhTUCyD9Cak02BSaipcTscQ==</DQ><InverseQ>HPIWEmC70LHrvjPUFuUqB3Kd99j1V/+v2xsINx3H8Rq0+4DBK6aJeuVNuERmJ9Wi3fDlMGnE97Z7hb3ztDPueQ==</InverseQ><D>JFiScnmBW24ha0uXBz05NdpDNzdoTH8xmVO7e97bPWXlQRu/LIfrpyeM+U7FHn72E7UTmvP7PEZxDZNsEAQ+nFNIhB7WQ6w14vsYVs7ryawUAJa4rIPFKpRqodzP6iIJje+pvWOUzamYrsFNJuGFN+K+sm8S2YzUx3ZUnY8pmEE=</D></RSAKeyValue>";

            string content = "hello xinxianlong";

            var encryptContent = RSAHelper.Encrypt(content, publicKey);
            var decryptContent = RSAHelper.Decrypt(encryptContent, privateKey);

            bool result = content.Equals(decryptContent);
        }
Example #2
0
        static void Main(string[] args)
        {
            var logger = NLog.LogManager.GetCurrentClassLogger();


            #region 翻译测试

            var aesTest = new AESTest();
            var rsaTest = new RSATest();
            RSAExtensionsTest();
            #endregion


            #region 这个部分放在数据适配层

            //var merchantId = "100016";
            //var logId = AESHelper.getLogId(merchantId);
            //var data = new Dictionary<string, object>();
            //data.Add("log_id", logId);
            //data.Add("phone_number_type", 12);
            //data.Add("phone_number", "1064776352047");
            //var body = JsonConvert.SerializeObject(data);

            var merchantId   = "100016";
            var logId        = AESHelper.getLogId(merchantId);
            var data         = new Dictionary <string, object>();
            var phone_number = "1064867369749";
            data.Add("log_id", logId);
            data.Add("phone_number", phone_number); //1064867369745
            data.Add("validate_timestamp", ConvertDateTimeToInt(DateTime.Now));
            var body = JsonConvert.SerializeObject(data);

            #endregion



            #region 生成requestData

            string clientPrivateKey      = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKtSBfAfb35umUeTgaE0+eh8OunKBVR4k0/RrT6lukCFeB0F/8s7/Ku5n9z5Nv4Ebe41dRvAl+2WmKklAz3jePoBQ41qXKAXZI78a/ZIIKI0iFrIBwqpN5D488n2/wESaRhEsYe6Zw3u4/KFY0AVk/HlYDvmS2DtzZm6JcQwvfZnAgMBAAECgYEAgPbPzYB8d5pd/EmHzYiJj37lAlS3Sm0xx2Y5me07lZJjZsW7Vowjmkmzk65uvS0sa6MGMwv50joJVVqtZAs2Zw/x04fk8M5duW8Kr7uL90OEO5HEvg2bjuNAN9DSVGy1JJBDk2/+M3ubSx7N82TW37Rf8lgqnZxObS4cN8mkmyECQQDjJP1zDhsHcUqU77tibHEp21B+Nk6GN+1oC8qq6qrxrNzE4I9KM2SFfGkxYdh91xpr1jKcSRo7fMqM4uejR8VpAkEAwRWRMS8HUBMb0kQu/gLisvYw1UbJzZPLzFuAYv5r6AqM5h0l9aBVw55RwzaPm2upCTpL6rNXJtJSbXbOHf9TTwJBAJNBbMpFT8KQcNCDZpDVSrvfAZ0BKgEbit6UHmyVvAL3lRxRlLN/A+ECGdQ44bgbVnaoo6DsR4RfT5TsmU0if1kCQETYs9SduDXNGnZ26WqZDMxTDEZ/3yT2Ngy/859YqJEsceD7M7XJXctKgEzi/4GjebpYlwkwuTqWc92kJwp7J/ECQAlG5iNQUB8I8fxqsxq/9Zgr2vtN4NN0+pOQe7CZwlWhqjT9Q2dwcWXjV7lXAsTvs3WCWfSklRV39qUmAL28z/M=";
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
            var priKey = rsa.ConvertToXmlPrivateKey(clientPrivateKey);
            Debug.WriteLine("私钥:" + priKey);

            string serverPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCNMzyuOyRTkgtQpsNC64QT2I3GlNM+fzqiVzUUCqjYJdJJGv1Bzb4CK/SnlQVowY6LFzLkKW0G2rfChqbP3EDb6MUEbYJTQG+mSjuZwZtU44NwN2biT8SYMDkgjOuPThuSMzzLhzF+M8g5sTjgc7y96kygUFFFWDbHkaNr30582wIDAQAB";

            var pubKey = rsa.ConvertToXmlPublicJavaKey(serverPublicKey);
            Debug.WriteLine("公钥:" + pubKey);


            var responseData      = new Dictionary <string, object>();
            var aesKey            = "h2hK6QaQ7hl92zNnusRhdQ==";
            var dataEncryptResult = AESHelper.Encrypt(body, aesKey);
            responseData.Add("data", dataEncryptResult);

            responseData.Add("merchant_id", merchantId);

            var passEncryptResult = RSAHelper.Encrypt(aesKey, pubKey);
            responseData.Add("pass", passEncryptResult);

            StringBuilder sbSign = new StringBuilder();
            foreach (var item in responseData)
            {
                sbSign.Append($"{item.Key}={item.Value}&");
            }
            sbSign.Remove(sbSign.Length - 1, 1);
            var sign = sbSign.ToString();
            var signEncryptResult = RSAHelper.Sign(sign, priKey);
            responseData.Add("sign", signEncryptResult);

            var requestData = JsonConvert.SerializeObject(responseData);

            #endregion

            var postBody = new Dictionary <string, object>();
            postBody.Add("merchant_id", merchantId);
            postBody.Add("requestData", requestData);
            var         urlRequest  = "http://preview.exapi.10046.mi.com/v1/activate";
            var         test        = JsonConvert.SerializeObject(postBody);
            HttpClient  httpClient  = new HttpClient();
            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(postBody));
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var    task = httpClient.PostAsync(urlRequest, httpContent).Result;
            string res  = null;
            if (task.StatusCode == HttpStatusCode.OK)
            {
                res = task.Content.ReadAsStringAsync().Result;
                logger.Debug($"卡号:{phone_number},{res}");
            }

            Console.Read();
        }