Skip to content Skip to sidebar Skip to footer

Generate Public Key From Private Key Using Webcrypto Api

I'm using Web Crypto API and am generating RSA Keypair using generateKey function. Because of some bugs in my code, I have deleted public key for some users. I'm wondering if there

Solution 1:

You can do it by exporting private key and importing exported data like public data

const keys = await crypto.subtle.generateKey(
  {
    name: 'RSA-OAEP',
    modulusLength: 2048,
    publicExponent: newUint8Array([0x01, 0x00, 0x01]),
    hash: { name: 'SHA-512' },
  },
  true,
  ['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'],
);

// export private key to JWKconst jwk = await crypto.subtle.exportKey("jwk", keys.privateKey);

// remove private data from JWKdelete jwk.d;
delete jwk.dp;
delete jwk.dq;
delete jwk.q;
delete jwk.qi;
jwk.key_ops = ["encrypt", "wrapKey"];

// import public keyconst publicKey = await crypto.subtle.importKey("jwk", jwk, { name: "RSA-OAEP", 
hash: "SHA-512" }, true, ["encrypt", "wrapKey"]);

console.log(publicKey)

enter image description here

Post a Comment for "Generate Public Key From Private Key Using Webcrypto Api"