繁体   English   中英

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

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

我正在创建一个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解码为十六进制,但我不确定下一步该怎么做:

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

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



编辑 1

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

这就像小菜一碟一样容易; 它不需要 3rd 方库

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' }))

它将返回 object:

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

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

有为浏览器和 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 手册

openssl asn1parse -in your_public.key

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM