Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

458
Views
CryptoJS: Can't decrypt string sent from Browser in NodeJS sometimes

I have a web browser client that uses CryptoJS in JavaScript. It encrypts a string and sends it to a NodeJS server.

When I try to decrypt the string on my NodeJS server, sometimes it works, sometimes it doesn't. It either gives a "Malformed UTF-8" data error or a "Unexpected end of JSON input" error.

Browser JavaScript code (encryption):

// Using CryptoJS via: https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js

var ENCRYPTION_KEY = "6268890F-9B58-484C-8CDC-34F9C6A9";
var ENCRYPTION_IV = "6268890F-9B58-48";

var data = {"hey": "hello"};
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), ENCRYPTION_KEY, {iv: ENCRYPTION_IV}).toString();
console.log("ciphertext:", ciphertext);

$.ajax({

        url: "http://127.0.0.1:1234/testEndpoint/"
        type: "GET",
        timeout:2000,
        data: ciphertext,
        statusCode: {
            200: function (response) {
                console.log("Success!");
            },
        }
        
 });

NodeJS Express Server code (decryption):

const WEB_PORT = 1234;

var CryptoJS = require("crypto-js");
var AES = require("crypto-js/aes");

var ENCRYPTION_KEY = "6268890F-9B58-484C-8CDC-34F9C6A9";
var ENCRYPTION_IV = "6268890F-9B58-48";

const express = require('express')
const app = express();

app.listen(WEB_PORT, () => {
    console.log("Listening...");
});

app.get('/testEndpoint/', (req, res) => {
    let ciphertext = Object.keys(req.query)[0];
    console.log("ciphertext from browser:", ciphertext);

    var bytes = CryptoJS.AES.decrypt(ciphertext, ENCRYPTION_KEY, {iv: ENCRYPTION_IV});
    var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
    console.log("decryptedData:", decryptedData);       
});

Browser output (one time when it didn't work [1]):

ciphertext: U2FsdGVkX19Epx6VAqYM48Q63NOVzrA+5A4zswQTv08=

NodeJS output (one time when it didn't work [1]):

ciphertext from browser: U2FsdGVkX19Epx6VAqYM48Q63NOVzrA 5A4zswQTv08
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)

Browser output (another time when it worked [2]):

ciphertext: U2FsdGVkX19NRRozkZiJV/564MGE2keF4HHwqsEtkmw=

NodeJS output (another time when it worked [2]):

ciphertext from browser: U2FsdGVkX19NRRozkZiJV/564MGE2keF4HHwqsEtkmw
decryptedData: { hey: 'hello' }

Why does it only work sometimes, and other times it either gives an "Unexpected end of JSON input" or "Malformed UTF-8" error?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!