# Decrypt encrypted text postman

**URL:** <https://community.postman.com/t/decrypt-encrypted-text-postman/73840>\
**Category:** Help Hub\
**Tags:** tests, collections, json, encryption\
**Created:** [January 15, 2025, 1:56pm UTC](https://community.postman.com/t/decrypt-encrypted-text-postman/73840 "2025-01-15T13:56:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![coderharish](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/coderharish/32/31310_2.png) [@coderharish](https://community.postman.com/u/coderharish)\
**Post date:** [January 15, 2025, 1:56pm UTC](https://community.postman.com/t/decrypt-encrypted-text-postman/73840/1 "2025-01-15T13:56:28Z")

</div>

Below is my Pre Request Script working Fine

```auto
const CryptoJS = require('crypto-js');

// Function to encrypt data
function encryptData(plainData, key, iv) {
    const encrypted = CryptoJS.AES.encrypt(plainData, CryptoJS.enc.Utf8.parse(key), {
        iv: CryptoJS.enc.Utf8.parse(iv),
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7,
    });
    return encrypted.toString();
}

// Encryption key and IV (use environment variables for security)
const key = pm.environment.get('encryptionKey') || '8264754987545454'; // Replace with your key
const iv = pm.environment.get('encryptionIV') || '764851343443456184'; // Replace with your IV

// Raw request body
const requestBody = {
    "CustCode": "H0000972"
}
;

// Encrypt the request payload
const encryptedPayload = encryptData(JSON.stringify(requestBody), key, iv);

// Prepare the final payload to include the encrypted content and IV
const finalPayload = {
    content: encryptedPayload,
    iv: CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(iv)),
};

// Set the encrypted payload as the request body
pm.request.body.raw = JSON.stringify(finalPayload);

// Log the encrypted payload for debugging
console.log('Encrypted Payload:', finalPayload);

```

After Getting actual response on json body response

```auto
{\"content\":\"DZBjHydiOa8LvGYUPOqxVF4Qf7UdclpzTV8ih2WGsrIat8d1FHSLJ4bRaXqtJ20v+67XxtH9kuzAxVWhvG3U3tPWbmtJZxUh3VU=\"}"

```

Error in response on console  
Invalid response structure or missing “content” field:

```auto
{"content":"DZBjHyd+jKRK422hX8YGJSkK4/gvmETwjyoeYjj+jrDfasY6/1CqCuixGQ21+dP1oH4aoU8dPApXVZXax3yucLw+LDJVgyEZ7tenBboMwAc3kGGdJxvatkXnge94YnxTdOblzVdaiDzXm5m1tseFn2URZGV6lwQaZCoQUIAByZ2CoQpY6cpFwvezwo9utbkv9vyzqsHzKKfLKKPomN7Cgi6q1merAZ8nMV9M/8akGqL1AeabmtJZxUh3VU="}

```

```auto
Error: Invalid response structure or missing "content" field.

```

Error getting

```auto
console response : {"content":" response \"}"
Json Body Response :- "{\"content\":\" body response \"}" correct

```
