how to do encryption/decryption of API request/response body using private key public key?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2025 07:45 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-16-2025 08:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2025 09:34 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2025 09:38 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2025 09:48 PM
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.