Java 如何使用RSA512算法创建签名JWT
我试图创建JWT(“JOT”)令牌,使我的api调用可信。当我尝试创建带有RSA512签名的令牌时,我得到一个错误,即 java.lang.IllegalArgumentException:必须使用RSA私钥计算RSA签名。javax.crypto.spec.SecretKeySpec类型的指定密钥不是RSA PrivateKey 我正在使用以下代码:Java 如何使用RSA512算法创建签名JWT,java,spring-boot,jwt,jwt-auth,Java,Spring Boot,Jwt,Jwt Auth,我试图创建JWT(“JOT”)令牌,使我的api调用可信。当我尝试创建带有RSA512签名的令牌时,我得到一个错误,即 java.lang.IllegalArgumentException:必须使用RSA私钥计算RSA签名。javax.crypto.spec.SecretKeySpec类型的指定密钥不是RSA PrivateKey 我正在使用以下代码: SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS512; lon
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.RS512;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(SECRET_KEY);
Key signingKey = new SecretKeySpec(apiKeySecretBytes,
signatureAlgorithm.getJcaName());
JwtBuilder builder = Jwts.builder().claim("uuid",
id).setIssuedAt(now).setExpiration(new Date(600000))
.signWith(signatureAlgorithm, signingKey);
注意:我的“密钥”是一个字符串,它是在线随机生成的私钥。
我的问题是如何从RSA密钥大小为4096的编码字符串中获取密钥对象。
4096由于我使用的是RSA512加密,建议对RSA512使用4096密钥。最初,我没有提供Base64编码的RSAkey,而我将其再次编码为Base64,这是我出现此错误的原因 java.lang.IllegalArgumentException:必须使用RSA私钥计算RSA签名。javax.crypto.spec.SecretKeySpec类型的指定密钥不是RSA PrivateKey。 当我提供RSAKey base 64编码或字节码私钥时,返回到错误下方 Base64编码密钥字节只能为HMAC签名指定。如果使用RSA或椭圆曲线,请改用signWith(SignatureAlgorithm,Key)方法。 每当我提供一个字符串/字节的私钥时,它总是在检查HMAC算法。请参阅下面来自JWTBuilder的代码
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, byte[] secretKey) {
Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
Assert.notEmpty(secretKey, "secret key byte array cannot be null or empty.");
Assert.isTrue(alg.isHmac(), "Key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
this.algorithm = alg;
this.keyBytes = secretKey;
return this;
}
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, String base64EncodedSecretKey) {
Assert.hasText(base64EncodedSecretKey, "base64-encoded secret key cannot be null or empty.");
Assert.isTrue(alg.isHmac(), "Base64-encoded key bytes may only be specified for HMAC signatures. If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.");
byte[] bytes = TextCodec.BASE64.decode(base64EncodedSecretKey);
return signWith(alg, bytes);
}
@Override
public JwtBuilder signWith(SignatureAlgorithm alg, Key key) {
Assert.notNull(alg, "SignatureAlgorithm cannot be null.");
Assert.notNull(key, "Key argument cannot be null.");
this.algorithm = alg;
this.key = key;
return this;
}
最好提供java.security.Key类型的私钥,并且必须是RSA密钥。我使用P12证书加载私钥。要在上验证您的令牌,您必须在
验证签名-您的-256位机密
下的右侧列中提供您的机密。错误说明了密钥类型,但您没有显示有关机密密钥
的详细信息。你可以发布密钥吗?我使用了一些随机密钥,比如“ADSFD564464GFASDF45DS4DFAS56F4ASD6FDA45FADS”,这对于HS密钥来说已经足够了,但肯定不是RSA密钥。RSA密钥如下所示:----开始RSA私钥----MIICXgIBAAKBgQDHikastc8+I81zCg/qww8dmr8mqvxqqqbpamu0rjxozvi47tvs…5vg08zngkffgr5rozditsk5dcetv97k a3Y+Nzl+XWTxDBWk4YPh2ZlKv402hZEfWBYxUDn5ZkH/bw==----结束RSA私钥----END RSA私钥----code>在此基础上,您可以创建用于测试的密钥。感谢@jsp提供的信息,它起作用了,但看起来我没有提供正确的签名,我能够生成,但仍然显示无效签名@jwt.io