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

543
Visualizações
CryptoJS AES decrypt Hex String?

This is sort of a part two to the question Trying to print hex string with CryptoJS

Basically, I have a function that uses CryptoJS to encrypt a message and spit out the cypher text as a hex string.

function EncryptAES(text, key) {
  var encrypted = CryptoJS.AES.encrypt(text, key);
  return encrypted.ciphertext.toString()
}

I want to write a function DecryptAES that takes the hex string, key, and salt (which I apparently need) and returns the original plaintext. The problem is I can't figure out how to get the hex string back into a form that CryptoJS.AES.decrypt will accept, and I can't find anywhere in the documentation where it actually explains how to do it. Maybe I overlooked something. Please help.

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

0

CryptoJS uses the WordArray data type internally and provides encoders for conversion. If you want to convert a hex encoded string into a WordArray, this is possible with the Hex encoder as follows:

CryptoJS.enc.Hex.parse(<your hex encoded string>)

or the other way around

<your WordArray>.toString(CryptoJS.enc.Hex)

or for short

<your WordArray>.toString()

Moreover, CryptoJS.AES.encrypt() returns a CipherParams object that encapsulates ciphertext and salt, among other things. This CipherParams object is to be passed to CryptoJS.AES.decrypt() for decryption.

With this, what you want can be implemented as follows:

function EncryptAES(text, key) {
    var encrypted = CryptoJS.AES.encrypt(text, key);
    return {ciphertext: encrypted.ciphertext.toString(), salt: encrypted.salt.toString()}
}

function DecryptAES(ciphertext, salt, key){
    var decrypted = CryptoJS.AES.decrypt({ciphertext:CryptoJS.enc.Hex.parse(ciphertext), salt:CryptoJS.enc.Hex.parse(salt)}, key); 
    return decrypted;
}

var {ciphertext, salt} = EncryptAES('The quick brown fox jumps over the lazy dog', 'my passphrase');
console.log('Ciphertext (hex): ', ciphertext);
console.log('Salt (hex): ', salt);

var decrypted = DecryptAES(ciphertext, salt, 'my passphrase');
console.log('Decrypted: ', decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>


However, there is an easier way to handle salt and ciphertext. For a CipherParams object, .toString() returns the data in the Base64 encoded OpenSSL format consisting of the hex encoding of Salted__ followed by the 8 bytes salt and the actual ciphertext. CryptoJS.AES.decrypt() implicitly converts such a Base64 string into a CipherParams object:

var encrypted = CryptoJS.AES.encrypt('The quick brown fox jumps over the lazy dog', 'my passphrase').toString();
console.log("Salt, ciphertext (OpenSSL format, Base64): ", encrypted);

var decrypted = CryptoJS.AES.decrypt(encrypted, 'my passphrase');
console.log("Decrypted: ", decrypted.toString(CryptoJS.enc.Utf8));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>

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