如何在 Node.js 中获取 RSA public 密钥的模数和指数

How to get the modulus and exponent of an RSA public key in Node.js

我正在创建一个 ACME 客户端,我需要找到我使用以下代码生成的 RSA public 密钥的模数和指数:

crypto.generateKeyPairSync('rsa', {
    modulusLength: 4096,
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
    },
    privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem'
    }
});

我需要模数和指数,以便我可以在 JWK section of my JWS:

中使用它们
alg: 'RS256',
jwk: {
    kty: 'RSA',
    e: '...',
    n: '...'
},
nonce,
url: directory.newAccount

我已经设法使用以下行将 public 密钥从 base64 解码为 hex,但我没有确定接下来要做什么:

Buffer.from(publicKey, 'base64').toString('hex');

如何在 Node.js 中找到 RSA public 密钥的模数和指数?



编辑 1

我发现 Node.js 默认使用 public 指数 65537:Node.js documentation.

有为浏览器和 node.js 编写的库 效果很好。 You can find them here.

包括文件并尝试这个功能,

function RSAModulusAndExponent(pubkey) {
        var unarmor = /-----BEGIN PUBLIC KEY-----([A-Za-z0-9+\/=\s]+)-----END PUBLIC KEY-----/;
         try{
            var pubkeyAsn1 = ASN1.decode(Base64.decode(unarmor.exec(pubkey)[1]));
            var modulusRaw = pubkeyAsn1.sub[1].sub[0].sub[0];
            var modulusStart = modulusRaw.header + modulusRaw.stream.pos + 1;
            var modulusEnd = modulusRaw.length + modulusRaw.stream.pos + modulusRaw.header;
            var modulusHex = modulusRaw.stream.hexDump(modulusStart, modulusEnd);
            var modulus = Hex.decode(modulusHex);
            var exponentRaw = pubkeyAsn1.sub[1].sub[0].sub[1];
            var exponentStart = exponentRaw.header + exponentRaw.stream.pos;
            var exponentEnd = exponentRaw.length + exponentRaw.stream.pos + exponentRaw.header;
            var exponentHex = exponentRaw.stream.hexDump(exponentStart, exponentEnd);
            var exponent = Hex.decode(exponentHex);
            
            return { success:true, msg:{moduls: modulus, exponent: exponent}};
        }
        catch(err){ console.log(err)
            return { success:false, msg:"Failed validating RSA public key."};
        }
    }

你有你的模数和指数。

对于 public 节点上的密钥验证,我建议
node-jose (要么) NodeRSA

const key = new NodeRSA(pubKey);   

if(key.isPublic && !key.isEmpty() && key.getKeySize() > 0)
        return {error: false, msg : "RSA Public key validation success! (Reason, isPublic?"+key.isPublic()+", isEmpty?"+key.isEmpty()+", Key length : " + key.getKeySize()+" )"}
 else
        return {error: false, msg : "RSA Public key validation failed! (Reason, isPublic?"+key.isPublic()+", isEmpty?"+key.isEmpty()+", Key length : " + key.getKeySize()+" )"}

我只是提出了一个想法。库中还有其他函数足以确保 public 键正常。

此外,您在命令行上有节点。您可以使用 openssl 查看原始格式。在这里查看更多,OpenSSL Manual

openssl asn1parse -in your_public.key

扩展@Topaco 给出的评论,像这样使用库 pem-jwk

const pem2jwk = require('pem-jwk').pem2jwk

console.log(pem2jwk(pemPublicKey));
OUTPUT:
{
  kty: '...',
  n: '...',
  e: '...'
}

console.log(pem2jwk(pemPrivateKey));
OUTPUT:
{
  kty: '...',
  n: '...',
  e: '...',
  d: '...',
  p: '...',
  q: '...',
  dp: '...',
  dq: '...',
  qi: '...'
}

这很容易;它不需要第三方库

import { createPublicKey } from 'crypto'

const pemPublicKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAphRAj+tRfbrYwnSFbWrj
...
vQIDAQAB
-----END PUBLIC KEY-----
`
const publicKey = createPublicKey(pemPublicKey)
console.log(publicKey.export({ format: 'jwk' }))

它将return对象:

{
  kty: 'RSA',
  n: [modulus string],
  e: [exponent string]
}