【问题标题】:How to get the modulus and exponent of an RSA public key in Node.js如何在 Node.js 中获取 RSA 公钥的模数和指数
【发布时间】:2020-08-13 09:58:09
【问题描述】:

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

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

我需要模数和指数,以便可以在我的JWSJWK 部分中使用它们:

alg: 'RS256',
jwk: {
    kty: 'RSA',
    e: '...',
    n: '...'
},
nonce,
url: directory.newAccount

我已设法使用以下行将公钥从 base64 解码为 hex,但我不确定下一步该怎么做:

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

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



编辑 1

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

【问题讨论】:

标签: javascript node.js rsa public-key


【解决方案1】:

扩展@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: '...'
}

【讨论】:

    【解决方案2】:

    有为浏览器和 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."};
            }
        }
    

    你有你的模数和指数。

    对于节点上的公钥验证,我会推荐
    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()+" )"}
    

    我只是提出了一个想法。库中还有其他功能足以确保公钥正常。

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

    openssl asn1parse -in your_public.key
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2014-04-16
      • 2018-06-10
      • 2011-03-08
      • 2012-07-25
      • 2019-09-22
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      相关资源
      最近更新 更多