External library - forge

I am using this code

window = {};
    eval(pm.globals.get("forge_library"))
    const public_key = '-----BEGIN PUBLIC KEY-----\n' +
        'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDORoOSW2gbHl6s/YmS1jWxb954\n' +
        'X/jflZ2dK65oM/Bxii2Iba80IiC9+Sa1phmOVDAk+IVDsPNZ+YJ2Qg0hPmoLSLxe\n' +
        'f2A6ySJPl5su8TaGOuVZg1SRyk55bjHymQUnxryD/ml1EmBUaGcrs9FCiVBy38kg\n' +
        'eZNbCexucVQxn6OYlwIDAQAB\n' +
        '-----END PUBLIC KEY-----'

    const private_key = '-----BEGIN PRIVATE KEY-----\n' +
        'MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAM5Gg5JbaBseXqz9\n' +
        'iZLWNbFv3nhf+N+VnZ0rrmgz8HGKLYhtrzQiIL35JrWmGY5UMCT4hUOw81n5gnZC\n' +
        'DSE+agtIvF5/YDrJIk+Xmy7xNoY65VmDVJHKTnluMfKZBSfGvIP+aXUSYFRoZyuz\n' +
        '0UKJUHLfySB5k1sJ7G5xVDGfo5iXAgMBAAECgYEAk6KQZN4bQt2XsYS9RGUghOCm\n' +
        'f81g2NXCu00aROZ3vyvArxaiAVQzzwRWGkjJnb7PvoZJC0vIwKr+HxnjP9nmFufd\n' +
        '+0EnBT+imYSzrfZhfGGwyI6EIyy/XcoW5lf0xltx3w9mJicnR9kMzNtZ5mNGPMNn\n' +
        'CgAgjvZqnWYb+f6tb/ECQQD0tdpg8ts3puXclPe51my+LbKhEbyFSMzvtMTDCRmO\n' +
        'd0jrmZhQomsZacC8+l+2l6WTj5vrhVQlAVUeUJ7kldQNAkEA18q53wor6a4Cv0OL\n' +
        'xFzBWXRCMVFfyCWAFQUpTSGrIM/X4Lx30IZCShtvkdh1ky39b9T6lpOjES7MK4Dh\n' +
        'xttCMwJAUGBi6DEcm/zvxzIO5DVv5k9wOsNunoC4/4rqjf0xLcA0bV43z1RpxSEd\n' +
        'M3UxdvH8aqli10slxjnX0Ws9pWspCQJBALqSncgYzETbXaauqO5a4BUOrphjafPr\n' +
        'cGU8NCxrGsFg0p6NdO5G1pOqSvmHdIiPL9t8AjkkZs3Zb0+BvDOpqP8CQQDZhfh4\n' +
        '/c/Qzp4szj7+GXTZ1cmGwAuFo2/9uiumUAS3f19EpgoV9u9eyJ4gZPEBDvAjO961\n' +
        'kAjdja4DAy4SbCXy\n' +
        '-----END PRIVATE KEY-----'


    //encrypt text "plaintext"
    var publicKey = forge.pki.publicKeyFromPem(public_key);
    var encryptedText = forge.util.encode64(publicKey.encrypt("plaintext", 'RSA-OAEP', {
        md: forge.md.sha1.create(),
        mgf1: {
            md: forge.md.sha1.create()
        }
    }));
    console.log("encrypted text:" + encryptedText);

    // decrypt text
    var privateKey = forge.pki.privateKeyFromPem(private_key);
    var decryptedText = privateKey.decrypt(forge.util.decode64(encryptedText), 'RSA-OAEP', {
        md: forge.md.sha1.create(),
        mgf1: {
            md: forge.md.sha1.create()
        }
    });

    console.log("dectypted text:" + decryptedText);

The code above is from GitHub - loveiset/RSAForPostman: add rsa encryt for postman this site

forge_library variable is set to value from this site: https://unpkg.com/node-forge@1.0.0/dist/forge.min.js

I have an error
ReferenceError: URLSearchParams is not defined

What is the solution for this problem?

Start by building your code up line by line and console logging the variables as you create them.

Start with the encrypt block.

Hopefully this will then show you which line is causing the error.

eval(pm.globals.get("forge_library"))
this line cause an error

You can’t just consume the minified JS directly from the CDN (or copy it to an environment variable).

This is a node.JS application that requires a local server, from which you compile the forge.js that you can then EVAL using Postman.

Have a look here

Postman RSA Encryption - Stack Overflow

and here for something that doesn’t need compiling.

postman-util-lib | :rocket: A crypto utility library to be used from Postman Pre-request and Tests script tabs. (joolfe.github.io)

CryptoJS is available directly in the Postman sandbox, and support HmacSHA256 but not RS256. (You might be better off using the utility I linked earlier).

Manage API data and workflows using Postman JavaScript objects | Postman Learning Center

crypto-js - npm (npmjs.com)

Encrypt parameters using CryptoJS | Postman Answers | Postman API Network

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.