I'm using JJWT to attempt to create and later verify JWT keys. It doesn't work when I try and convert the keys to Strings and vice versa - for storage for later use.
This example work fine:
KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.PS256);
Key publicKey = keyPair.getPublic();
Key privateKey = keyPair.getPrivate();
Claims claims = Jwts.claims();
claims.setIssuedAt(new Date());
String jws = Jwts.builder().setSubject("Joe").claim("Hello", "World").signWith(privateKey).compact();
boolean result = Jwts.parserBuilder().setSigningKey(publicKey).build().parseClaimsJws(jws).getBody().getSubject().equals("Joe");
System.out.println("Verified:" + result);
return "";
However, when I convert the keys to Base64Encoded strings as such:
String base64Public = Encoders.BASE64.encode(publicKey.getEncoded());
String base64Private = Encoders.BASE64.encode(privateKey.getEncoded());
How do I load the public key (from a String) back into the algorithm and verify a JWS using the public key?
String base64Public = Encoders.BASE64.encode(publicKey.getEncoded());
boolean result2 = Jwts.parserBuilder().setSigningKey(base64Public).build().parseClaimsJws(jws).getBody().getSubject().equals("Joe");
fails with:
Key bytes can only be specified for HMAC signatures. Please specify a PublicKey or PrivateKey instance.