jwt 私钥_JWT公钥与私钥签名验证-有什么区别?

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.

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

export function certToPEM(cert) {

cert = cert.match(/.{1,64}/g).join('\n');

cert = `-----BEGIN CERTIFICATE-----\n${cert}\n-----END CERTIFICATE-----\n`;

return cert;

}

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

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

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)

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-----\n`;

pem += `${der.match(/.{1,64}/g).join('\n')}`;

pem += `\n-----END RSA PUBLIC KEY-----\n`;

return pem;

};

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?

解决方案

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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值