Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

459
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda