JWT 公钥与私钥签名验证——有什么区别? [英] JWT public key vs private key signature validation -- what is the difference?

查看:112
本文介绍了JWT 公钥与私钥签名验证——有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个库 node-jwks-rsa 来获取 JWTauth0 jwks.json 文件中的密钥,以验证我的应用程序在身份验证后检索的 id_token 实际上来自我的身份验证提供程序.

I am using this library, node-jwks-rsa, to fetch JWT keys from my auth0 jwks.json file in order to verify that the id_token my application retrieves after authentication is actually coming from my auth provider.

在幕后,它使用这种方法来构建公钥 PEM

Under the hood it uses this method to build a public key PEM

export function certToPEM(cert) {
  cert = cert.match(/.{1,64}/g).join('
');
  cert = `-----BEGIN CERTIFICATE-----
${cert}
-----END CERTIFICATE-----
`;
  return cert;
}

(使用 x50c 作为 .jwks 文件中的参数).

(Using the x50c as argument from the .jwks file).

然后我将它与 jsonwebtoken 结合使用来验证 JWT(id_token) 是有效.

which I then use in combination with jsonwebtoken to verify that the JWT(id_token) is valid.

这种验证方法与根据 jwks.json 文件的模数和指数生成私钥 (RSA) 并将其用于验证有何不同?(作为示例,请参阅此)

How is this method of verification different from generating a private key(RSA) from the modulus and exponent of the jwks.json file and using it for verification instead? (as example see this library)

此外,这里还有一个函数作为演示,它从一个 mod 和指数生成一个 PEM(取自 http://stackoverflow.com/questions/18835132/xml-to-pem-in-node-js)

Additionally here is function as demonstration that generates a PEM from a mod and exponent (taken from http://stackoverflow.com/questions/18835132/xml-to-pem-in-node-js)

export function rsaPublicKeyToPEM(modulusB64, exponentB64) {
    const modulus = new Buffer(modulusB64, 'base64');
    const exponent = new Buffer(exponentB64, 'base64');
    const modulusHex = prepadSigned(modulus.toString('hex'));
    const exponentHex = prepadSigned(exponent.toString('hex'));
    const modlen = modulusHex.length / 2;
    const explen = exponentHex.length / 2;

    const encodedModlen = encodeLengthHex(modlen);
    const encodedExplen = encodeLengthHex(explen);
    const encodedPubkey = '30' +
      encodeLengthHex(modlen + explen + encodedModlen.length / 2 + encodedExplen.length / 2 + 2) +
      '02' + encodedModlen + modulusHex +
      '02' + encodedExplen + exponentHex;

    const der = new Buffer(encodedPubkey, 'hex')
      .toString('base64');

    let pem = `-----BEGIN RSA PUBLIC KEY-----
`;
    pem += `${der.match(/.{1,64}/g).join('
')}`;
    pem += `
-----END RSA PUBLIC KEY-----
`;

    return pem;
  };

前面提到的 jsonwebtoken 库可以使用任何一种来验证 JWT——但为什么呢?如果这两种验证方法都可以验证 JWT 签名,为什么它们都存在?它们之间的权衡是什么?一个比另一个更安全吗?我应该使用哪个来最完整地验证?

The aforementioned jsonwebtoken library can verify a JWT using either -- but why? If both of these verification methods can validate a JWT signature why do they both exist? What are the tradeoffs between them? Is one more secure than the other? Which should I use to verify most fully?

推荐答案

使用 RSA 非对称密钥对,JWT 使用私钥签名并公开验证.您无法使用私钥验证数字签名

Using a RSA assymetric key pair, the JWT is signed with the private key and verified with the public. You can not verify a digital signature with the private key

模数和指数是公钥的组成部分,您可以使用它来构建 PEM 格式的公钥,PEM 格式是以 DER 二进制格式编码的公钥(模数和指数)的 base64 表示.您可以使用 PEM、DER 或模数和指数,因为它们包含相同的信息

Modulus and exponent are the components of the public key and you can use it to build the public key in PEM format, which is a base64 representation of the public key (modulus and exponent) encoded in DER binary format. You can use PEM, DER or modulus and exponent because the contain the same information

但是任何人都不能用模数和指数来构建私钥.他需要私有的 RSA 元素,这些元素必须保密,以便没有人可以为你签名.

But anybody can't build the private key with modulus and exponent. He would need the private RSA elements, which must be kept secret so that no one can sign for you.

这篇关于JWT 公钥与私钥签名验证——有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆