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

1.1K
Views
decipher.final(): estado no admitido o incapaz de autenticar los datos

Tengo el siguiente código:

 const crypto = require("crypto"); const algorithm = "aes-256-gcm"; const message = "This is a secret message"; const iv = crypto.randomBytes(12); const sKey = crypto.randomBytes(32); const cipher = crypto.createCipheriv(algorithm, sKey, iv); const decipher = crypto.createDecipheriv(algorithm, sKey, iv) let encryptedData = cipher.update(message, "utf-8", "hex"); encryptedData += cipher.final("hex"); let decData = decipher.update(encryptedData, "hex", "utf-8"); decData += decipher.final("utf-8"); console.log("Encrypted message: " + encryptedData); console.log("Decrypted message: ", decData)

Cuando lo ejecuto, recibo la siguiente excepción:

 node:internal/crypto/cipher:193 const ret = this[kHandle].final(); ^ Error: Unsupported state or unable to authenticate data at Decipheriv.final (node:internal/crypto/cipher:193:29) at Object.<anonymous> ([...]/crypto-test.js:15:21) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47

Se lanza un error al llamar a decipher.final() . ¿Cómo puedo arreglarlo?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Cuando utilice un cifrado autenticado (que es aes-256-gcm ), necesitará la etiqueta de autenticación del cifrado; de lo contrario Decipher.final() arrojará (como puede ver) ya que la etiqueta de autenticación que tiene (o en este caso, no) no coincide con lo que cree que es la etiqueta de autenticación del texto descifrado.

 const crypto = require('crypto'); const algorithm = 'aes-256-gcm'; const message = 'This is a secret message'; const iv = crypto.randomBytes(12); const sKey = crypto.randomBytes(32); const cipher = crypto.createCipheriv(algorithm, sKey, iv); let encryptedData = cipher.update(message, 'utf-8', 'hex'); encryptedData += cipher.final('hex'); const authTag = cipher.getAuthTag().toString("hex"); // <- new console.log({authTag, encryptedData}); // for debugging const decipher = crypto.createDecipheriv(algorithm, sKey, iv); decipher.setAuthTag(Buffer.from(authTag, 'hex')); // <- new let decData = decipher.update(encryptedData, 'hex', 'utf-8'); decData += decipher.final('utf-8'); console.log('Decrypted message: ', decData)
about 4 years ago · Juan Pablo Isaza Report
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!