Feel kind of out of my depth here.
I have a message that I'm trying to encrypt on a react front end using a public key and the jose library. Then that message will be sent to the java back end and needs to be decrypted by the same public key so that the message can be read.
On the front end, this is my code:
const secret = jose.base64url.decode('zH4NRP1HMALxxCFnRZABFA7GOJtzUAgIj02alfL1lvI');
const jwt = await new jose.EncryptJWT({ foo: 'bar' })
.setProtectedHeader({ alg: 'dir', enc: 'A128CBC-HS256' })
.setIssuedAt()
.setIssuer('urn:example:issuer')
.setAudience('urn:example:audience')
.setExpirationTime('2h')
.encrypt(secret);
And then this is my attempt at decrypting on the backend
byte[] encoded = Base64.getUrlDecoder().decode("zH4NRP1HMALxxCFnRZABFA7GOJtzUAgIj02alfL1lvI");
PublicKey pk = null;
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
pk = keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
.setRequireExpirationTime()
.setVerificationKey(pk)
.build();
try {
JwtClaims jwtDecoded = jwtConsumer.processToClaims(forwardKey);
System.out.println(jwtDecoded.getStringClaimValue("foo"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
However Java doesn't seem to have the HS256 algorithm available when creating the PublicKey object using the keyfactory, but then I can't find any information on what algorithms to use for the encryption on the front end.
Decrypting on the front end using the same secret works fine. But when trying to do the same thing in JAVA its like 20 more lines and I'm not sure what those lines are supposed to be at all