Using SuiteScript to encrypt a string as JWE

Hi everyone, I have a question regarding JWS and JWE using SuiteScript. Perhaps someone here can help me with their previous experience.

I’m currently working on a script to sign a payload using JWS. After signing, I need to encrypt it using JWE. I was able to accomplish the signing part by referring to an example I found on Stack Overflow, which I believe someone here may have answered.

However, I’m facing a challenge when it comes to using the built-in crypto module to encrypt the JWS payload in a JWE-compliant manner, using encryption algorithms like A256CBC-HS512 or RSA-OAEP-256. I haven’t been able to find a way or an example demonstrating this.

Does anyone here have experience with accomplishing this task? It goes without saying that I’m trying not to include any external library to achieve this.

Rookie Asked on June 15, 2023 in SuiteScript.
Add Comment
5 Answer(s)

Hi,

I think you might struggle to do this with the inbuilt crypto module but I think the crypto-js library might help:
https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js

I’ve used this elsewhere for talks not converted by native functionality (and even some stuff that is!).

I hope that helps.

Thanks,

Chris

Advanced Answered on June 15, 2023.
Add Comment

Thank you Chris for your quick response.

Well, probably I’ll need to install this module then, using the native one is indeed a bit cumbersome

Rookie Answered on June 16, 2023.
Add Comment

No problem. If you download the file, it’s a straightforwards as:

 

require(['./crypto-js'], function(CryptoJS) {
...
})
Advanced Answered on June 16, 2023.
Add Comment

When I include the above code in a SuiteScript:

cryptojs.AES.encrypt("test value", "test key")

I’ve got the following error

crypto-js.js:79 Uncaught Error: Native crypto module could not be used to get secure random number.
at cryptoSecureRandomInt (/SuiteScripts/crypto-js/crypto-js.js:79:16)
at random (/SuiteScripts/crypto-js/crypto-js.js:371:29)
at execute (/SuiteScripts/crypto-js/crypto-js.js:4215:25)
at encrypt (/SuiteScripts/crypto-js/crypto-js.js:4266:34)
at encrypt (/SuiteScripts/crypto-js/crypto-js.js:3630:33)
at :program (eval in context:1:1)
at encryptAES256 (/SuiteScripts/multiplier_reverse_sync_scheduled_script.js:43:13)
at execute (/SuiteScripts/multiplier_reverse_sync_scheduled_script.js:114:22)
cryptoSecureRandomInt @ crypto-js.js:79
random @ crypto-js.js:371
execute @ crypto-js.js:4215
encrypt @ crypto-js.js:4266
encrypt @ crypto-js.js:3630
:program @ 28/eval in context:1
encryptAES256 @ scheduled_script.js:43
execute @ scheduled_script.js

Please help. Thanks in Advance!

Rookie Answered on June 16, 2023.
Add Comment

The issue is covered here and the last comments seems to suggest that a slightly older version may be the way to go for now:

https://github.com/brix/crypto-js/issues/256

Advanced Answered on June 22, 2023.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   SuiteMigration   Become a Sponsor