How to Generate and validate JWE in node js?

橙三吉。 提交于 2019-12-04 17:46:07

It looks like the IV is not correctly encoded using Base64 Url. On my computer, when I execute the following line of code:

console.log(iv);
console.log(base64url.encode(iv, "base64"));

Then the two lines are identical. When I modify your code:

var jweToken = headerBase64Url + "." + base64url.encode(encryptedKey, "base64") + "." + base64url.encode(iv, "base64") + "." + base64url.encode(chipherText, "base64") +
"." + base64url.encode(chipherTextAuthTag, "base64");

lnto these lines:

var bufferedIV = new Buffer(iv);
var jweToken = headerBase64Url + "." + base64url.encode(encryptedKey, "base64") + "." + base64url.encode(bufferedIV, "base64") + "." + base64url.encode(chipherText, "base64") +
"." + base64url.encode(chipherTextAuthTag, "base64");

Then it works fine ; I can load the resulting JWE using my PHP library.

From my understanding, the error comes from the base64url dependency that does not correctly encode the IV.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!