用COM.GOOGLE.COMMON.IO.BASEENCODING- decodode base64->解码器感受:未识别的字符: -
一般目标
我想替换com.google.api.client.util.base64
用com.google.common.io.baseencoding
库库库,因为它已弃用(我正在使用Java 11)。
我使用这些库来验证RSA签名的JWT。要创建公共密钥,我将n的字符串表示形式和我的证书的e值转换为biginteger。
问题
我的解决方案总是抛出以下例外:com.google.common.io.io.baseencoding $ desodingException:未识别的字符: -
我想解释的字符串中有-
字符但是我不知道如何使用基本编码软件包来解决此问题。
有人遇到了这个问题并可以帮助我吗?
代码
是带有com.google.api.client.util.base64
解码的代码:
package com.example;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.util.Calendar;
import com.auth0.jwk.InvalidPublicKeyException;
import com.auth0.jwk.JwkException;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.google.api.client.util.Base64;
public final class App {
public final static String PUBLIC_KEY_ALGORITHM = "RSA";
/**
* Returns a {@link PublicKey} if the {@code 'alg'} is {@code 'RSA'}
*
* @return a public key
* @throws InvalidPublicKeyException if the key cannot be built or the key type is not RSA
*/
public static PublicKey getPublicKey() throws InvalidPublicKeyException {
try {
KeyFactory kf = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM);
byte[] decodedPubN = Base64.decodeBase64(PUB_N);
byte[] decodedPubE = Base64.decodeBase64(PUB_E);
BigInteger modulus = new BigInteger(1, decodedPubN);
BigInteger exponent = new BigInteger(1, decodedPubE);
return kf.generatePublic(new RSAPublicKeySpec(modulus, exponent));
} catch (InvalidKeySpecException e) {
throw new InvalidPublicKeyException("Invalid public key", e);
} catch (NoSuchAlgorithmException e) {
throw new InvalidPublicKeyException("Invalid algorithm to generate key", e);
}
}
/**
*
*
* @param args The arguments of the program.
* @throws JwkException
*/
public static void main(String[] args) throws JwkException {
DecodedJWT jwt = JWT.decode(TOKEN);
PublicKey publicKey = getPublicKey();
Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) publicKey, null);
algorithm.verify(jwt);
if (jwt.getExpiresAt().before(Calendar.getInstance().getTime())) {
throw new RuntimeException("Expired token!");
}
}
}
我使用com.google.common.io.baseencon.baseenconeconding 库:
/**
* Returns a {@link PublicKey} if the {@code 'alg'} is {@code 'RSA'}
*
* @return a public key
* @throws InvalidPublicKeyException if the key cannot be built or the key type is not RSA
*/
public static PublicKey getPublicKey() throws InvalidPublicKeyException {
try {
KeyFactory kf = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM);
BigInteger modulus = new BigInteger(1, BaseEncoding.base64().decode(pubN));
BigInteger exponent = new BigInteger(1, BaseEncoding.base64().decode(pubE));
return kf.generatePublic(new RSAPublicKeySpec(modulus, exponent));
} catch (InvalidKeySpecException e) {
throw new InvalidPublicKeyException("Invalid public key", e);
} catch (NoSuchAlgorithmException e) {
throw new InvalidPublicKeyException("Invalid algorithm to generate key", e);
}
}
General Goal
I want to replace the com.google.api.client.util.Base64
library with the com.google.common.io.BaseEncoding
library since it is deprecated (I am working with Java 11).
I use these libraries to verify an RSA-signed jwt. To create the public key I take the String representation of the n and the e value of my certificate and turn it into a BigInteger.
Problem
My solution always throws this exception: com.google.common.io.BaseEncoding$DecodingException: Unrecognized character: -
There are -
characters in the strings I want to decode, but I do not know how to work this out with the BaseEncoding Package.
Did someone encounter this problem and can help me?
Code
Here is the code with the com.google.api.client.util.Base64
decoding:
package com.example;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.util.Calendar;
import com.auth0.jwk.InvalidPublicKeyException;
import com.auth0.jwk.JwkException;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.google.api.client.util.Base64;
public final class App {
public final static String PUBLIC_KEY_ALGORITHM = "RSA";
/**
* Returns a {@link PublicKey} if the {@code 'alg'} is {@code 'RSA'}
*
* @return a public key
* @throws InvalidPublicKeyException if the key cannot be built or the key type is not RSA
*/
public static PublicKey getPublicKey() throws InvalidPublicKeyException {
try {
KeyFactory kf = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM);
byte[] decodedPubN = Base64.decodeBase64(PUB_N);
byte[] decodedPubE = Base64.decodeBase64(PUB_E);
BigInteger modulus = new BigInteger(1, decodedPubN);
BigInteger exponent = new BigInteger(1, decodedPubE);
return kf.generatePublic(new RSAPublicKeySpec(modulus, exponent));
} catch (InvalidKeySpecException e) {
throw new InvalidPublicKeyException("Invalid public key", e);
} catch (NoSuchAlgorithmException e) {
throw new InvalidPublicKeyException("Invalid algorithm to generate key", e);
}
}
/**
*
*
* @param args The arguments of the program.
* @throws JwkException
*/
public static void main(String[] args) throws JwkException {
DecodedJWT jwt = JWT.decode(TOKEN);
PublicKey publicKey = getPublicKey();
Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) publicKey, null);
algorithm.verify(jwt);
if (jwt.getExpiresAt().before(Calendar.getInstance().getTime())) {
throw new RuntimeException("Expired token!");
}
}
}
I refactored the getPublicKey() function with the com.google.common.io.BaseEncoding
library:
/**
* Returns a {@link PublicKey} if the {@code 'alg'} is {@code 'RSA'}
*
* @return a public key
* @throws InvalidPublicKeyException if the key cannot be built or the key type is not RSA
*/
public static PublicKey getPublicKey() throws InvalidPublicKeyException {
try {
KeyFactory kf = KeyFactory.getInstance(PUBLIC_KEY_ALGORITHM);
BigInteger modulus = new BigInteger(1, BaseEncoding.base64().decode(pubN));
BigInteger exponent = new BigInteger(1, BaseEncoding.base64().decode(pubE));
return kf.generatePublic(new RSAPublicKeySpec(modulus, exponent));
} catch (InvalidKeySpecException e) {
throw new InvalidPublicKeyException("Invalid public key", e);
} catch (NoSuchAlgorithmException e) {
throw new InvalidPublicKeyException("Invalid algorithm to generate key", e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像提到的评论一样,JWT使用base64url编码。我只需要更改
baseencoding.base64()。解码(pubn)
baseencoding.base64url()。解码(pubn)
。Like in the comments mentioned, a JWT uses Base64Url encoding. I just had to change
BaseEncoding.base64().decode(pubN)
toBaseEncoding.base64Url().decode(pubN)
.