I currently have a JWK public key in the following json format(in a file called public_key.json
, that public key is a sample):
{
"productId": "some-product",
"kid": "abc123...",
"exp": 1669574140,
"y": "ABC....",
"kty": "EC",
"use": "enc",
"alg": "ECDH-ES",
"crv": "P-521",
"x": "DEF..."
}
I am loading the JWK with the following code:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
class Sample {
public static void main(String[] args) {
try {
byte[] payload = Files.readAllBytes(Paths.get("public_key.json"));
KeyFactory otherkf = KeyFactory.getInstance("EC");
X509EncodedKeySpec pkSpec = new X509EncodedKeySpec(payload);
PublicKey publicKey = otherkf.generatePublic(pkSpec);
} catch (IOException|InvalidKeySpecException|NoSuchAlgorithmException e) {
e.printStackTrace();
System.out.println(e);
}
}
}
The above code is raising the following exception:
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
at jdk.crypto.ec/sun.security.ec.ECKeyFactory.engineGeneratePublic(ECKeyFactory.java:158)
at java.base/java.security.KeyFactory.generatePublic(KeyFactory.java:351)
at Sample.main(Sample.java:16)
Caused by: java.security.InvalidKeyException: invalid key format
at java.base/sun.security.x509.X509Key.decode(X509Key.java:387)
at java.base/sun.security.x509.X509Key.decode(X509Key.java:402)
at jdk.crypto.ec/sun.security.ec.ECPublicKeyImpl.<init>(ECPublicKeyImpl.java:71)
at jdk.crypto.ec/sun.security.ec.ECKeyFactory.implGeneratePublic(ECKeyFactory.java:225)
at jdk.crypto.ec/sun.security.ec.ECKeyFactory.engineGeneratePublic(ECKeyFactory.java:154)
... 2 more
Am I loading the public key properly or do I need to transform the json into another format?