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

715
Views
Error: Longitud de clave no válida y Error: Longitud de IV no válida usando el algoritmo AES-256-CBC

Tengo 2 funciones para cifrar y descifrar con el algoritmo AES-256-CBC:

 import * as crypto from "crypto"; export const encrypt = (text: string, key: string, iv: string) => { const cipher = crypto.createCipheriv("aes-256-cbc", key, iv); let result = cipher.update(text, "utf8", "hex"); result += cipher.final("hex"); return result; }; export const decrypt = (text: string, key: string, iv: string) => { const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv); let result = decipher.update(text, "hex", "utf8"); result += decipher.final("utf8"); return result; };

El problema es con la llave y el IV. Tuve que generar IV y clave como esta:

 crypto.randomBytes(8).toString('hex') // IV crypto.randomBytes(16).toString('hex') // Key

Estaba tratando de cambiar la longitud de esta manera, pero tuve 2 errores:

 crypto.randomBytes(16).toString('hex') // IV crypto.randomBytes(32).toString('hex') // Key

Error: Invalid key length y Error: Invalid IV length

Pero descubrí que la clave debe tener 32 bytes, no 16. ¿Qué pasa?

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

0

Está codificando incorrectamente en hexadecimal su clave y IV. Suelte toString('hex') de ambos, estos argumentos no deben estar codificados en hexadecimal.

La longitud correcta de la clave y IV es de 32 y 16 bytes respectivamente. Mediante la codificación hexadecimal de las cadenas, está produciendo cadenas dos veces más largas de lo necesario, donde cada byte va desde un valor entre 0 y 255 a una representación hexadecimal de dos caracteres entre 00 y ff .

Por ejemplo, la matriz de bytes [255, 255, 255] se convierte en la matriz de caracteres ['f', 'f', 'f', 'f', 'f', 'f'] .

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!