Ejemplo n.º 1
0
 public ResponseModel NewEncryptionKey(AutomationKeyModel automationModel, string Email, string UserName, string PlatformCode)
 {
     //Generate IV
     AesCryptoServiceProvider cryptoProvider = new AesCryptoServiceProvider();
     cryptoProvider.KeySize = 256;
     cryptoProvider.GenerateIV();
     string IV = Convert.ToBase64String(cryptoProvider.IV);
     //Encrypt Model
     byte[] data = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(automationModel));
     byte[] key = Convert.FromBase64String(provisionerKey);
     string hash = Convert.ToBase64String(new HMACSHA512(key).ComputeHash(data));
     string data2 = Encrypt(key, Convert.FromBase64String(IV), Newtonsoft.Json.JsonConvert.SerializeObject(automationModel));
     //Construct the request model
     RequestModel requestModel = new RequestModel
     {
         UserName = UserName,
         PlatformCode = PlatformCode,
         IV = IV,
         Data = data2,
         Hash = hash
     };
     var req = new WebClient();
     req.BaseAddress = "https://provisioning.datamotion.com:8888/company/NewEncryptionKey";
     req.Headers.Clear();
     req.Headers.Add("Accept", "application/json");
     req.Headers.Add(Headers.Email, Email);
     req.Headers.Add(Headers.Iv, IV);
     try
     {
         var request = req.UploadData("https://provisioning.datamotion.com:8888/company/NewEncryptionKey", Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestModel)));
         ResponseModel response = JsonConvert.DeserializeObject<ResponseModel>(Encoding.UTF8.GetString(request));
         response.Data = Decrypt(key, Convert.FromBase64String(response.IV), response.Data);
         return response;
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
Ejemplo n.º 2
0
 public ResponseModel NewEncryptionKey(AutomationKeyModel automationModel, string Email, string UserName, string PlatformCode)
 {
     //Generate IV
     AesCryptoServiceProvider cryptoProvider = new AesCryptoServiceProvider();
     cryptoProvider.KeySize = 256;
     cryptoProvider.GenerateIV();
     string IV = Convert.ToBase64String(cryptoProvider.IV);
     //Encrypt Model
     byte[] data = Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(automationModel));
     byte[] key = Convert.FromBase64String(provisionerKey);
     string hash = Convert.ToBase64String(new HMACSHA512(key).ComputeHash(data));
     string data2 = Encrypt(key, Convert.FromBase64String(IV), Newtonsoft.Json.JsonConvert.SerializeObject(automationModel));
     //Construct the request model
     RequestModel requestModel = new RequestModel
     {
         UserName = UserName,
         PlatformCode = PlatformCode,
         IV = IV,
         Data = data2,
         Hash = hash
     };
     var req = new HttpClient();
     req.BaseAddress = new Uri("https://provisioning.datamotion.com:8888/company/NewEncryptionKey");
     req.DefaultRequestHeaders.Accept.Clear();
     req.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
     req.DefaultRequestHeaders.Add(Headers.Email, Email);
     req.DefaultRequestHeaders.Add(Headers.Iv, IV);
     var request = req.PostAsJsonAsync("https://provisioning.datamotion.com:8888/company/NewEncryptionKey", requestModel);
     if (request.Result.IsSuccessStatusCode)
     {
         DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ResponseModel));
         ResponseModel response = new ResponseModel();
         response = (ResponseModel)serializer.ReadObject(request.Result.Content.ReadAsStreamAsync().Result);
         response.Data = Decrypt(key, Convert.FromBase64String(response.IV), response.Data);
         return response;
     }
     else
     {
         throw new Exception(request.Result.Content.ReadAsStringAsync().Result);
     }
 }