示例#1
0
        protected internal T Decrypt <T>(string data, string ek, string hmac, ErrorDetails error)
        {
            T model = default(T);

            if (String.IsNullOrEmpty(data))
            {
                if (error != null)
                {
                    dicParams.Add("ResponseError", error.message);
                }
            }
            else
            {
                try
                {
                    byte[] decryptREK = EncryptionUtils.AesDecrypt(ek, provider.DecryptedKey);
                    byte[] jsonData   = EncryptionUtils.AesDecrypt(data, decryptREK);
                    if (!String.IsNullOrEmpty(hmac))
                    {
                        string testHmac = EncryptionUtils.GenerateHMAC(jsonData, decryptREK);
                        this.LogMessage("HMAC Match:" + (hmac == testHmac));
                    }
                    string base64Payload = UTF8Encoding.UTF8.GetString(jsonData);
                    byte[] decodeJson    = Convert.FromBase64String(base64Payload);
                    string finalJson     = Encoding.UTF8.GetString(decodeJson);
                    dicParams["ResponsePayload"] = finalJson;
                    model = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(finalJson);
                }
                catch (Exception ex)
                {
                    dicParams.Add("ResponseError", ex.Message);
                }
            }
            return(model);
        }
示例#2
0
        protected internal UnsignedDataInfo EncryptBase64(string base64Payload)
        {
            UnsignedDataInfo info = new UnsignedDataInfo();

            byte[] jsonData = UTF8Encoding.UTF8.GetBytes(base64Payload);
            info.data = EncryptionUtils.AesEncrypt(jsonData, provider.DecryptedKey);
            info.hmac = EncryptionUtils.GenerateHMAC(jsonData, provider.DecryptedKey);
            return(info);
        }
        protected internal T Decrypt <T>(ResponseDataInfo output)
        {
            T model = default(T);

            if (output != null)
            {
                byte[] decryptREK = EncryptionUtils.AesDecrypt(output.rek, provider.DecryptedKey);
                byte[] jsonData   = EncryptionUtils.AesDecrypt(output.data, decryptREK);
                string testHmac   = EncryptionUtils.GenerateHMAC(jsonData, decryptREK);
                System.Console.WriteLine("HMAC Match:" + (output.hmac == testHmac));
                string base64Payload = UTF8Encoding.UTF8.GetString(jsonData);
                byte[] decodeJson    = Convert.FromBase64String(base64Payload);
                string finalJson     = Encoding.UTF8.GetString(decodeJson);
                model    = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(finalJson);
                LastJson = finalJson;
            }
            return(model);
        }
        protected internal UnsignedDataInfo Encrypt <T>(T input)
        {
            UnsignedDataInfo info = new UnsignedDataInfo();

            if (input != null)
            {
                string finalJson = JsonConvert.SerializeObject(input, Newtonsoft.Json.Formatting.Indented,
                                                               new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
                byte[] encodeJson    = UTF8Encoding.UTF8.GetBytes(finalJson);
                string base64Payload = Convert.ToBase64String(encodeJson);
                byte[] jsonData      = UTF8Encoding.UTF8.GetBytes(base64Payload);
                info.data = EncryptionUtils.AesEncrypt(jsonData, provider.DecryptedKey);
                info.hmac = EncryptionUtils.GenerateHMAC(jsonData, provider.DecryptedKey);
            }
            return(info);
        }