1

I have a public key returned like

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

And, I want to use java to load it, and my code is like

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();
        }

    }
}

and I got exception thrown, says java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format

How to load it correctly?

Bomin
  • 1,619
  • 5
  • 24
  • 39
  • 1
    SO Answer to similar question: http://stackoverflow.com/questions/26867840/java-security-spec-invalidkeyspecexception-and-inappropriate-key-specification-e – boxed__l Jul 20 '16 at 09:52
  • I'm not sure I get your comment. That question is about encrypting a key and store it into a file, and then load it from the file. Why is it similar to my question? Well, I don't really understand "AQAB" in my question either... – Bomin Jul 20 '16 at 13:33
  • AQAB is just base 64 for the public exponent, which is generally `010001` in hexadecimals (the fourth prime of Fermat). If you look at it bitwise in 6 bit groups then the number is `000000 010000 000000 000001`, in numbers `0 16 0 1` of, als je dat vertaald naar base 64 characters, `AQAB` (`A` is the first character in the base64 alphabet). – Maarten Bodewes Jul 20 '16 at 23:49
  • Hmm some Dutch in above comment: if you translate that to base 64... – Maarten Bodewes Oct 27 '16 at 20:36

1 Answers1

1

Java only approach (look ma, no libraries):

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 doesn't know this kind of format so you have to parse it yourself, or find a decoder. I was lazy and programmed it.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263