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

142
Views
Desempaquetar y descifrar usando crypto.subtle con una clave privada generada por NodeJS

Estoy generando un par de claves en el servidor como este:

 crypto.generateKeyPair('rsa', { modulusLength: 4096, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem', cipher: 'aes-256-cbc', passphrase: password, } }, callback);

Quiero usar esta clave en el cliente. Lo que probé es

 function pad(arr, blocksize) { if (arr.length % blocksize === 0) return arr; let res = new Uint8Array(Math.floor(arr.length / blocksize) * blocksize + blocksize); res.set(arr, 0); for (let i = arr.length; i < res.length; i++) { res[i] = res.length - arr.length; } return res; } let ek = privateKey; let h = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"; let f = "\n-----END ENCRYPTED PRIVATE KEY-----\n"; ek = atob(ek.substring(h.length, ek.length - f.length).split("\n").join("")); ek = Uint8Array.from(ek, c => c.charCodeAt(0)); let pwd = password; pwd = Uint8Array.from(pwd, c => c.charCodeAt(0)); let padded = pad(pwd, 32); let pwdKey = await crypto.subtle.importKey( "raw", padded, { name: "AES-CBC", length: 256 }, false, ["unwrapKey"] ); console.log(pwdKey); let unwrapped = await crypto.subtle.unwrapKey( "pkcs8", ek, pwdKey, { name: "AES-CBC", length: 256, iv: crypto.getRandomValues(new Uint8Array(16)) }, { name: "RSA-PSS", hash: "SHA-512", publicExponent: new Uint8Array([1, 0, 1]), modulusLength: 4096 }, false, ["decrypt"] ); console.log(unwrapped);

También intenté rellenar la clave privada, no funcionó.

Pero el desempaquetado falla con DOMException y sin explicación. ¿Alguna idea de cómo hacer que funcione?

about 4 years ago · Juan Pablo Isaza
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!