使用RSA256 + 密码生成JWT令牌

huangapple 未分类评论53阅读模式
英文:

Generating JWT Token with RSA256 + Passcode

问题

我正在尝试使用带有密码的 RSA256 私钥来签署 JWT 令牌。

以下是我用来生成密码的代码:

byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(key);
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
JwtBuilder builder = Jwts.builder().setIssuedAt(now).signWith(SignatureAlgorithm.RS256, signingKey);
LOGGER.info(builder.compact());

我得到了以下错误:

java.lang.IllegalArgumentException: RSA signatures must be computed using an RSA PrivateKey. The specified key of type javax.crypto.spec.SecretKeySpec is not an RSA PrivateKey.

这个错误表明私钥存在问题,然而我已经使用 JavaScript 进行了验证,看起来工作正常(这允许我提供 passPhrase)。然而在 Java 中,我在查找关于如何提供 passPhrase 的文档方面遇到了困难。

英文:

I am attempting to sign a JWT token using a RSA256 private key that has a passphrase.

Here's the code that I am using the generate the passphrase:

byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(key);
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
JwtBuilder builder = Jwts.builder().setIssuedAt(now).signWith(SignatureAlgorithm.RS256, signingKey);
LOGGER.info(builder.compact());

I'm getting the following error:

> java.lang.IllegalArgumentException: RSA signatures must be computed using an RSA PrivateKey. The specified key of type javax.crypto.spec.SecretKeySpec is not an RSA PrivateKey.

The error implies that there is something invalid with the private key, however I did validate it using JavaScript and it seems to work fine (Which allows me to provide a passPhrase). However with Java I'm having trouble finding the documentation on providing a passPhrase.

huangapple
  • 本文由 发表于 2020年5月4日 13:14:08
  • 转载请务必保留本文链接:https://java.coder-hub.com/61585567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定