# Guidline How to Using External Lib JSENScrypt in postman

**URL:** <https://community.postman.com/t/guidline-how-to-using-external-lib-jsenscrypt-in-postman/19069>\
**Category:** Help Hub\
**Tags:** encryption\
**Created:** [January 12, 2021, 3:23am UTC](https://community.postman.com/t/guidline-how-to-using-external-lib-jsenscrypt-in-postman/19069 "2021-01-12T03:23:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hoc.Tran](https://avatars.discourse-cdn.com/v4/letter/h/b2d939/32.png) [@Hoc.Tran](https://community.postman.com/u/Hoc.Tran)\
**Post date:** [January 12, 2021, 3:23am UTC](https://community.postman.com/t/guidline-how-to-using-external-lib-jsenscrypt-in-postman/19069/1 "2021-01-12T03:23:41Z")

</div>

1. URL to get jsEncrypt lib: [https://raw.githubusercontent.com/travist/jsencrypt/master/bin/jsencrypt.js](https://raw.githubusercontent.com/travist/jsencrypt/master/bin/jsencrypt.js)

2. save URL at step 1 in global Varaible with name “jsEncrypt”;

3. In Pre-request Script, write script as below:

```auto
    navigator = {};
    window = {};
    pm.sendRequest(pm.globals.get("jsEncrypt"), (err, res) => {
    var text = res.text();
    (new Function(text))();
    var jsencrypt = new JSEncrypt.JSEncrypt({
            default_key_size: 4096
    });
    jsencrypt.setKey( "// here is public key" );
    var result = jsencrypt.encrypt("// here is you clear value before you want to encrypt");
    console.log(CryptoJS.enc.Base64.parse(result).toString());

```

Hope can help you all.

---

<div class="post-metadata">

**Author:** ![ravi.mahajan](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@ravi.mahajan](https://community.postman.com/u/ravi.mahajan)\
**Post date:** [May 23, 2021, 10:46am UTC](https://community.postman.com/t/guidline-how-to-using-external-lib-jsenscrypt-in-postman/19069/2 "2021-05-23T10:46:23Z")

</div>

Hi Hoc.Tran,

Could you please help me to execute your code?  
I tried to execute your code in the Postmans Pre-request script but it is not working.  
It is having a syntax error and when the syntax error is resolved, JSEncrypt is not defined error is coming.

---

<div class="post-metadata">

**Author:** ![ravi.mahajan](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@ravi.mahajan](https://community.postman.com/u/ravi.mahajan)\
**Post date:** [May 23, 2021, 12:22pm UTC](https://community.postman.com/t/guidline-how-to-using-external-lib-jsenscrypt-in-postman/19069/3 "2021-05-23T12:22:48Z")

</div>

Below is the working code…  
window = {};  
pm.sendRequest(pm.globals.get(“jsEncrypt”), (err, res) =\> {  
var text = res.text();  
(new Function(text))();  
var jsencrypt = new window.JSEncrypt;  
jsencrypt.setPublicKey("//here is public key");  
var result = jsencrypt.encrypt("//here is you clear value before you want to encrypt");  
});

---

<div class="post-metadata">

**Author:** ![altea](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@altea](https://community.postman.com/u/altea)\
**Post date:** [November 8, 2022, 12:24pm UTC](https://community.postman.com/t/guidline-how-to-using-external-lib-jsenscrypt-in-postman/19069/4 "2022-11-08T12:24:47Z")

</div>

hi ravi, I’m getting error “window.JSEncrypt is not a constructor”

---

<div class="post-metadata">

**Author:** ![altea](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@altea](https://community.postman.com/u/altea)\
**Post date:** [November 10, 2022, 9:40am UTC](https://community.postman.com/t/guidline-how-to-using-external-lib-jsenscrypt-in-postman/19069/5 "2022-11-10T09:40:55Z")

</div>

it is working for me like this:

navigator= {};  
window = {};  
var a=eval(pm.environment.get(“jsencrypt”));  
var encrypt = new window.JSEncrypt();  
var base64publicKey = postman.getEnvironmentVariable(“publicKeyBase64”);  
var publicKey = atob(base64publicKey); //get public key by decode Base64  
publicKey = publicKey.replace(“-----BEGIN PUBLIC KEY-----”, “”);  
publicKey = publicKey.replace(“-----END PUBLIC KEY-----”, “”);  
publicKey = publicKey.replace(/\n/g, “”);  
encrypt.setPublicKey(publicKey);  
pm.environment.set(“usernameEncrypted”, encrypt.encrypt(pm.environment.get(“username”)));  
pm.environment.set(“passwordEncrypted”, encrypt.encrypt(pm.environment.get(“password”)));
