I am working on an app where I need to send the encrypted JWT token using the public key available in (.der) format but I am facing InvalidKeyException error. Sharing the code and the exception below, any insight on this would be highly appreciated. The library used is nimbus-jose-jwt
private static RSAPublicKey getPublicKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
byte[] keyBytes = Files.readAllBytes(Paths.get("/emp/apps/emp.der"));
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
return (RSAPublicKey) kf.generatePublic(spec);
}
The Exception
Caused by: java.security.InvalidKeyException: IOException: algid parse error, not a sequence at java.base/sun.security.x509.X509Key.decode(X509Key.java:397)