how to do encryption/decryption of API request/response body using private key public key?

Amit Kumar13
Tera Expert

I have to encrypt/decrypt the request/response body using the private key and public key.
I have used the crypto js but as per third party we have use the public key and private key instead of key and IV in crypto JS.|
Is there any feasibilty, please let me know.

Thanks!
@Ankur Bawiskar Please provide your input as this integration is with APIGEE

5 REPLIES 5

danmjunqueira
Kilo Guru

Yes, it’s feasible to use public and private key encryption in JavaScript, but not with CryptoJS directly — CryptoJS is primarily designed for symmetric encryption (i.e., using a shared key and IV, like AES).

If your requirement is to use asymmetric encryption (e.g., RSA with a public/private key pair), you need to use a library that supports it, such as:


Recommended Libraries for Asymmetric Encryption in Javascript:
1. Forge
A widely used library for RSA and PKI operations.

Installation:
npm install node-forge

Example:

const forge = require('node-forge');

// Generate keys (in production you use pre-generated keys)
const keypair = forge.pki.rsa.generateKeyPair(2048);

// Encrypt with public key
const encrypted = keypair.publicKey.encrypt("Hello, world", 'RSA-OAEP');

// Decrypt with private key
const decrypted = keypair.privateKey.decrypt(encrypted, 'RSA-OAEP');

console.log(decrypted); // Output: Hello, world

2. SubtleCrypto (Web Crypto API) – Browser Native
If you're working in a browser environment (not Node.js), you can use the native window.crypto.subtle.

Note: It supports only SPKI/PKCS8 format keys.


Use Case Mapping:
Request: Encrypt the request body using the public key (third party decrypts with their private key).

Response: The third party encrypts the response using their private key, and you decrypt with the public key (though this is more commonly used for signature verification, not encryption).

Important Notes:
Asymmetric encryption is not used to encrypt large data directly. It’s typically used to:

Encrypt a random symmetric key (e.g., AES key)

Then use that symmetric key to encrypt the actual payload (called hybrid encryption)

Always validate that the key format (PEM, DER, etc.) and algorithm (RSA-OAEP, PKCS1, etc.) match what the third party expects.




Is there any possibility to do this in this way.
It seems like they are using crypto only.
Please check the snapshot and suggest.

Thanks

Ankur Bawiskar
Tera Patron
Tera Patron

@Amit Kumar13 

how did you use the crypto js 3rd party to encrypt and decrypt?

share that please so that it helps to help you better.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader


They have shared me these functions nothing else. Shared above in screenshot
I also don't have any idea.
Do we have any feasibility in SeviceNow ?
I couldn't find anything yet.