Ejemplo n.º 1
0
 /// <summary>
 /// 翻译java源码AES
 /// </summary>
 public AESTest()
 {
     string key           = "ZcAnM9q1FwCnLvuEuyfGYQ==";
     var    encryptResult = AESHelper.Encrypt("test", key);
     var    decryptResult = AESHelper.Decrypt(encryptResult, key);
 }
Ejemplo n.º 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();
        }