用COM.GOOGLE.COMMON.IO.BASEENCODING- decodode base64->解码器感受:未识别的字符: -

发布于 2025-02-05 02:11:40 字数 3719 浏览 2 评论 0原文

一般目标

我想替换com.google.api.client.util.base64com.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

林空鹿饮溪 2025-02-12 02:11:41

像提到的评论一样,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) to BaseEncoding.base64Url().decode(pubN).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文