RSAEncrypter encrypter = new RSAEncrypter((RSAKey) jwk); // there should always at least be the public key encrypter.getJCAContext().setProvider(BouncyCastleProviderSingleton.getInstance()); encrypters.put(id, encrypter);
@Override protected JWEEncrypter buildEncrypter() { CommonHelper.assertNotNull("publicKey", publicKey); return new RSAEncrypter(this.publicKey); }
private JWEEncrypter createEncryptor() throws KrbException, JOSEException { if (RSAEncrypter.SUPPORTED_ALGORITHMS.contains(jweAlgorithm)) { if (!(encryptionKey instanceof RSAPublicKey)) { throw new KrbException("An RSAPublicKey key must be specified for encryption"); } return new RSAEncrypter((RSAPublicKey) encryptionKey); } else if (DirectEncrypter.SUPPORTED_ALGORITHMS.contains(jweAlgorithm)) { if (!(encryptionKey instanceof byte[])) { throw new KrbException("A byte[] key must be specified for encryption"); } return new DirectEncrypter((byte[]) encryptionKey); } throw new KrbException("An unknown encryption algorithm was specified"); }
private JWEEncrypter createEncryptor() throws KrbException, JOSEException { if (RSAEncrypter.SUPPORTED_ALGORITHMS.contains(jweAlgorithm)) { if (!(encryptionKey instanceof RSAPublicKey)) { throw new KrbException("An RSAPublicKey key must be specified for encryption"); } return new RSAEncrypter((RSAPublicKey) encryptionKey); } else if (DirectEncrypter.SUPPORTED_ALGORITHMS.contains(jweAlgorithm)) { if (!(encryptionKey instanceof byte[])) { throw new KrbException("A byte[] key must be specified for encryption"); } return new DirectEncrypter((byte[]) encryptionKey); } throw new KrbException("An unknown encryption algorithm was specified"); }
public static JWEEncrypter getEncrypter(Key key, JWEAlgorithm encAlgo, EncryptionMethod encMethod) throws UnsupportedEncAlgorithmException, UnsupportedKeyLengthException { if (key instanceof RSAPublicKey) return new RSAEncrypter((RSAPublicKey) key); if (key instanceof ECPublicKey) { try { return new ECDHEncrypter((ECPublicKey) key); } catch (JOSEException e) { throw new UnsupportedEncAlgorithmException(e.getMessage(), e); } } if (key instanceof SecretKey) { if (AESEncrypter.SUPPORTED_ALGORITHMS.contains(encAlgo) && AESEncrypter.SUPPORTED_ENCRYPTION_METHODS.contains(encMethod)) { try { return new AESEncrypter((SecretKey) key); } catch (KeyLengthException e) { throw new UnsupportedKeyLengthException(e.getMessage(), e); } } if (DirectEncrypter.SUPPORTED_ALGORITHMS.contains(encAlgo) && DirectEncrypter.SUPPORTED_ENCRYPTION_METHODS.contains(encMethod)) { try { return new DirectEncrypter((SecretKey) key); } catch (KeyLengthException e) { throw new UnsupportedKeyLengthException(e.getMessage(), e); } } } throw new UnsupportedEncAlgorithmException("Unknown Algorithm"); }
JWEEncrypter encrypter = new RSAEncrypter((RSAPublicKey) publicKey); encryptedJWT.encrypt(encrypter);
public static JWEEncrypter getEncrypter(JWK jwk, JWEAlgorithm encAlgo, EncryptionMethod encMethod) throws UnsupportedEncAlgorithmException, KeyExtractionException, UnsupportedKeyLengthException { if (jwk instanceof RSAKey) { try { return new RSAEncrypter((RSAKey) jwk); } catch (JOSEException e) { throw new KeyExtractionException(e.getMessage(), e);
JWEHeader header = new JWEHeader(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM); EncryptedJWT jwt = new EncryptedJWT(header, builder.build()); RSAEncrypter encrypter = new RSAEncrypter((RSAPublicKey) keyProvider.getDefaultPublicKey()); try { jwt.encrypt(encrypter);
RSAEncrypter encrypter = new RSAEncrypter(recipientPublicJWK); jweObject.encrypt(encrypter);
@Test public void nestedJWT() throws Exception { RSAKey senderJWK = (RSAKey) JWK.parse(senderJwkJson); RSAKey recipientPublicJWK = (RSAKey) (JWK.parse(recipientJwkJson)); // Create JWT SignedJWT signedJWT = new SignedJWT( new JWSHeader.Builder(JWSAlgorithm.RS256).keyID(senderJWK.getKeyID()).build(), new JWTClaimsSet.Builder() .subject("testi") .issuer("https:devgluu.saminet.local") .build()); signedJWT.sign(new RSASSASigner(senderJWK)); JWEObject jweObject = new JWEObject( new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP, EncryptionMethod.A128GCM) .contentType("JWT") // required to indicate nested JWT .build(), new Payload(signedJWT)); // Encrypt with the recipient's public key RSAEncrypter encrypter = new RSAEncrypter(recipientPublicJWK); jweObject.encrypt(encrypter); final String jweString = jweObject.serialize(); decryptAndValidateSignatureWithGluu(jweString); }