Javascript 使用WebCryptoAPI从私钥生成公钥
我正在使用并正在使用函数生成RSA密钥对。由于代码中存在一些错误,我删除了一些用户的公钥。我想知道是否有办法从私钥中生成公钥?我知道ssh密钥很容易实现。以下是生成RSA密钥对的示例代码:Javascript 使用WebCryptoAPI从私钥生成公钥,javascript,encryption,rsa,encryption-asymmetric,webcrypto-api,Javascript,Encryption,Rsa,Encryption Asymmetric,Webcrypto Api,我正在使用并正在使用函数生成RSA密钥对。由于代码中存在一些错误,我删除了一些用户的公钥。我想知道是否有办法从私钥中生成公钥?我知道ssh密钥很容易实现。以下是生成RSA密钥对的示例代码: const generateRSAKeys = (): Promise<CryptoKeyPair> => { return crypto.subtle.generateKey( { name: 'RSA-OAEP', modulusLengt
const generateRSAKeys = (): Promise<CryptoKeyPair> => {
return crypto.subtle.generateKey(
{
name: 'RSA-OAEP',
modulusLength: 2048
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: 'SHA-512' },
},
true,
['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'],
);
const generateRSAKeys=():Promise=>{
返回crypto.minute.generateKey(
{
名称:“RSA-OAEP”,
模长:2048
publicExponent:new Uint8Array([0x01,0x00,0x01]),
哈希:{name:'SHA-512'},
},
是的,
['encrypt'、'decrypt'、'wrapKey'、'unwrapKey'],
);
您可以通过导出私钥和导入导出的数据(如公共数据)来实现
const keys = await crypto.subtle.generateKey(
{
name: 'RSA-OAEP',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: { name: 'SHA-512' },
},
true,
['encrypt', 'decrypt', 'wrapKey', 'unwrapKey'],
);
// export private key to JWK
const jwk = await crypto.subtle.exportKey("jwk", keys.privateKey);
// remove private data from JWK
delete jwk.d;
delete jwk.dp;
delete jwk.dq;
delete jwk.q;
delete jwk.qi;
jwk.key_ops = ["encrypt", "wrapKey"];
// import public key
const publicKey = await crypto.subtle.importKey("jwk", jwk, { name: "RSA-OAEP",
hash: "SHA-512" }, true, ["encrypt", "wrapKey"]);
console.log(publicKey)
公钥基本上由模数和公指数组成,两者都是私钥的一部分。因此,是的,这当然是可能的(微不足道,甚至)。如何做完全取决于私钥存储的格式,以及您需要公钥的格式。挑剔:公钥指数不是私钥的一部分,但为了方便起见必须包含私钥的格式(包括PKCS#1中的格式,所以是的,几乎是通用的)。我如何为ECDSA调整此格式?