I want to convert JWT into JWE but all my tries have failed. I am trying React JS. I also tried in Express JS but faced the same error. I have used Node-Jose for my tries.
const createJWE = async () => {
// Payload to be included in the JWT
const payload = {
idToken: 'token',
};
try {
// Sign the JWT
const jwt = await jose.JWS.createSign({ format: 'compact' }, { key: rsaPublicKey }).update(JSON.stringify(payload)).final();
// Encrypt the JWT to create a JWE
const jwe = await jose.JWE.createEncrypt({ format: 'compact' }, { key: rsaPublicKey }).update(jwt).final();
console.log(jwe);
} catch (error) {
console.error('JWE Creation Error:', error);
}
}
createJWE();
The output
Based on the source for
createSign(...)
,key
in the second argument is expected to be a JWK, either as an object, or as a JSON string. So your error is derived from the value ofrsaPublicKey
being in the wrong format.As the value used for
key
is a JWK object/string, it should look similar to:Based on the error message, your
rsaPublicKey
probably contains the content of a PEM file (as the error complains the first character is a minus, but the second is not a number):To import a key in this format, you need to bring the key in using
keystore.add
(fromJWK.createKeyStore()
) orJWK.asKey
and specifying the format.Ultimately you should be using a private key to sign instead.
Note: Above keys were obtained from https://phpseclib.com/docs/rsa-keys