我正在尝试使用REST Api来检索会话密钥,并且检索会话密钥的说明为
我没有访问服务器端代码的权限,但是它是C#,并且在我发送请求时它以{"Message":"Request failed hash compare"}
响应。我非常确定,该错误意味着数据正在被解密,但未正确解密,因为当它尝试将解密后的有效负载的哈希值与我在标头中传递的哈希值进行比较时会失败。我知道我的加密是正确的,因为当我将预加密的有效负载与解密的有效负载进行比较时,它们是完全相同的。有人可以告诉我服务器端C#代码为何/为什么不以相同的方式解密吗?
var aes = require('aes-js');
var crypto = require('crypto');
var request = require('request');
var aesjs = require('aes-js');
var fiddler = require("./fiddler");
fiddler.proxyRequests();
//fiddler.unproxyRequests();
var encryptionKey = "";
var automationId = "";
var timeStamp = new Date().toUTCString();
var user =
{
"Identity": {
"Email": "email"
},
"IpAddress":"",
"TimeStamp": timeStamp
};
console.log(JSON.stringify(user));
var iv = crypto.randomBytes(16).slice(0, 16);
console.log(iv);
var ivStr = iv.toString('base64');
var payloadByteArr = Buffer.from(JSON.stringify(user), 'base64');
var encryptionKeyByteArr = Buffer.from(encryptionKey, 'base64');
var hashedPayload = crypto.createHmac('SHA512', encryptionKeyByteArr.toString('binary')).update(payloadByteArr).digest('base64');
console.log(hashedPayload);
var cipher = crypto.createCipheriv('aes-256-cbc', encryptionKeyByteArr, iv);
var encrypted = cipher.update(new Buffer(JSON.stringify(user)));
var encryptedPayload = Buffer.concat([encrypted, cipher.final()]).toString('base64');
console.log(encryptedPayload);
function decrypt(encrypted, key, iv){
var decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
var dec = decipher.update(encrypted,'base64','utf8');
dec += decipher.final('utf-8');
var hashedPayloadTest = crypto.createHmac('SHA512', encryptionKeyByteArr.toString('binary')).update(new Buffer(dec, 'base64')).digest('base64');
console.log(hashedPayloadTest);
return dec;
}
console.log(decrypt(encryptedPayload, encryptionKeyByteArr, iv).toString('base64'));
request({
url: 'url',
method: "POST",
headers: {
"content-type": "application/json", "X-Email": "email",
"X-Iv": ivStr, "X-Company-Automation-ID": automationId, "X-Hash": hashedPayload
},
json: encryptedPayload
}, function(error, response, body){
console.log(JSON.stringify(body));
});