Search code examples
javascriptservicenow

How to decrypt PKCS#8 encrpyted private key in javascript


I am working on ServiceNow - BOX integration using rest API. for generating access tokens, I need to generate a JWT.

I need to have a private key to sign that JWT.

They have provided the private key but it is in an encrypted format.

-----BEGIN ENCRYPTED PRIVATE KEY-----
*******Key here********** 
-----END ENCRYPTED PRIVATE KEY-----

I have also been provided with a pass code to decrypt it.

I am not sure how to decrypt above private key in ServiceNow.

They gave examples in other languages here: https://developer.box.com/docs/construct-jwt-claim-manually#section-2-decrypt-private-key

Please help me in getting this done in javascript pure implementation.

Thanks,

Ali


Solution

  • From what you posted above it looks like you are receiving the key in PKCS#8 format https://en.wikipedia.org/wiki/PKCS_8

    From some quick googling looks like this library may be your best bet for extracting this value. https://github.com/kjur/jsrsasign

    Here is code in that library that decodes this:

    https://github.com/kjur/jsrsasign/blob/d282c71cee92000c4807bcbf2212fedf3f22bd84/src/keyutil-1.0.js#L77

    https://github.com/kjur/jsrsasign/blob/d282c71cee92000c4807bcbf2212fedf3f22bd84/src/keyutil-1.0.js#L557-L571

    Here they are using in a unit test. Probably similar how you will need to call.

    https://github.com/kjur/jsrsasign/blob/d282c71cee92000c4807bcbf2212fedf3f22bd84/test/qunit-do-crypto-sigini.html#L222