Search code examples
node.jsexpressencryptioncryptojs

Crypto-js and Express How can I prevent crypto-js from adding slashes into encrypted IDs to prevent server from reading it as a new sub route?


I am encrypting a user id using crypto-js to send it via url like the following for the user to validate it on click:

http://localhost:3000/api/customer/activate/U2FsdGVkX1DXzzLuf9TgBf31Mc2V/QBVAN05PovlNM

This is the encrypted user id:

U2FsdGVkX1DXzzLuf9TgBf31Mc2V/QBVAN05PovlNM

Using crypto-js encrypt:

crypto.AES.encrypt(term.toString(), config.CRYPTO_PASSPHRASE_RES).toString(crypto.enc.Utf8)

When I run the API, it return route not found, and when I remove / from the encrypted user id, it worked.

How can I prevent crypto-js from adding / into encrypted IDs?


Solution

  • The solution was to use encodeURIComponent() and then decodeURIComponent() like the following:

    encodeURIComponent(crypto.AES.encrypt(term.toString(), config.CRYPTO_PASSPHRASE_RES).toString());
    

    It will replace / into something else.