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?