Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

335
Visualizações
How to decrypt with PHP a string that is encrypted in CryptoJS in javascript

We encrypted a string in JS with this code by this JS library:

CryptoJS.AES.encrypt("Message", "Key").toString()

and then save it to the database for future use cases. Now we want to decrypt that hash in PHP by this code:

openssl_decrypt( "Message", "aes-128-ctr", "Key")

but it gives us a null value! How can we do it without making any changes in JS code?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

In JS side do this:

    var CryptoJSAesJson = {
      stringify: function (cipherParams) {
        var j = { ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) };
        if (cipherParams.iv) j.iv = cipherParams.iv.toString();
        if (cipherParams.salt) j.s = cipherParams.salt.toString();
        return JSON.stringify(j);
      },
      parse: function (jsonStr) {
        var j = JSON.parse(jsonStr);
        var cipherParams = CryptoJS.lib.CipherParams.create({
          ciphertext: CryptoJS.enc.Base64.parse(j.ct),
        });
        if (j.iv) cipherParams.iv = CryptoJS.enc.Hex.parse(j.iv);
        if (j.s) cipherParams.salt = CryptoJS.enc.Hex.parse(j.s);
        return cipherParams;
      },
    };

    let data = CryptoJS.AES.encrypt("Text", "Key", {
      format: CryptoJSAesJson,
    }).toString();

    //store the "data" in DB or another place

and in PHP Side do this:

     function cryptoJsAesDecrypt($passphrase, $jsonString){
         $jsondata = json_decode($jsonString, true);
         $salt = hex2bin($jsondata["s"]);
         $ct = base64_decode($jsondata["ct"]);
         $iv  = hex2bin($jsondata["iv"]);
         $concatedPassphrase = $passphrase.$salt;
         $md5 = array();
         $md5[0] = md5($concatedPassphrase, true);
         $result = $md5[0];
         for ($i = 1; $i < 3; $i++) {
             $md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true);
             $result .= $md5[$i];
         }
         $key = substr($result, 0, 32);
         $data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
         return $data;
    }

    echo cryptoJsAesDecrypt("Key",data);
    // will show "Text"

source: This Link

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda