Java加载公钥

时间:2016-07-20 09:48:52

标签: java rsa public-key

我有一个返回的公钥,如

"kty" : "RSA",
"alg" : "RS256",
"ext" : false,
"n": "vswzzDmrqLSHUu61YDxUhM87hjcVjg42NwpFOyLQK8CyW5YRcr1YUkFRNDbb92MTNW3CsSWJX3DSuilnxf8n3_JW-A9R5JAqwmEygYIXuFcoJ_pb923bph0-ayWPBfD-qwYrELvpiEHBf1QSLJYkRb1wzAlwhCeYJorifu2WhCZoOVVYQAEyNqYF7AVhNImioT8-lhFWGqHp2Jt7-oXtCjVVyyShRHUMYyCRzGj1VGI6AU5DgVebXYD2GJawUhX    -AD2CzsX8lMXeaVu88sBU9XLL1Zb_cOvAC7wTXxcls0taKx-8PiWUWKjSg0-O2ZXbfFROyQpQYHQH0BkO8XRh8w"
"e" : "AQAB"

并且,我想用java加载它,我的代码就像

package key;

import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class PublicKeyReader {

    public static PublicKey get() throws Exception {
        String key = "vswzzDmrqLSHUu61YDxUhM87hjcVjg42NwpFOyLQK8CyW5YRcr1YUkFRNDbb92MTNW3CsSWJX3DSuilnxf8n3_JW-A9R5JAqwmEygYIXuFcoJ_pb923bph0-ayWPBfD-qwYrELvpiEHBf1QSLJYkRb1wzAlwhCeYJorifu2WhCZoOVVYQAEyNqYF7AVhNImioT8-lhFWGqHp2Jt7-oXtCjVVyyShRHUMYyCRzGj1VGI6AU5DgVebXYD2GJawUhX-AD2CzsX8lMXeaVu88sBU9XLL1Zb_cOvAC7wTXxcls0taKx-8PiWUWKjSg0-O2ZXbfFROyQpQYHQH0BkO8XRh8w";

        X509EncodedKeySpec spec = new X509EncodedKeySpec(key.getBytes());
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePublic(spec);
    }

    public static void main(String[] args) {
        try {
            new PublicKeyReader().get();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

我被异常抛出,java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format

如何正确加载?

1 个答案:

答案 0 :(得分:1)

仅Java方法(看看ma,没有库):

package nl.owlstead.stackoverflow;

import java.io.File;
import java.math.BigInteger;
import java.nio.file.Files;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LoadRSAKeyFromText {

    public static void main(String[] args) throws Exception {
        // parse the lines to find the modulus n and public exponent e
        List<String> all = Files.readAllLines(new File(args[0]).toPath());
        String nString = null, eString = null;
        for (String line : all) {
            Pattern nPattern = Pattern.compile("\"n\"\\s*:\\s*\"(.*?)\",?");
            Matcher nMatcher = nPattern.matcher(line);
            if (nMatcher.matches()) {
                nString = nMatcher.group(1).replaceAll("\\s+", "");
            }

            Pattern ePattern = Pattern.compile("\"e\"\\s*:\\s*\"(.*?)\",?");
            Matcher eMatcher = ePattern.matcher(line);
            if (eMatcher.matches()) {
                eString = eMatcher.group(1);
            }
        }

        // decode base 64 (with _ and -, so URL safe)
        Decoder urlDecoder = Base64.getUrlDecoder();
        byte[] nData = urlDecoder.decode(nString);
        byte[] eData = urlDecoder.decode(eString);

        // convert to *positive* integers
        BigInteger n = new BigInteger(1, nData);
        BigInteger e = new BigInteger(1, eData);

        // create RSA specification and convert to key
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(n, e);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPublicKey pk = (RSAPublicKey) kf.generatePublic(keySpec);
        System.out.println(pk.getAlgorithm());
    }
}

Java不知道这种格式,所以你必须自己解析它,或者找一个解码器。我懒得编程。