Search in sources :

Example 1 with JweDecrypterImpl

use of io.jans.as.model.jwe.JweDecrypterImpl in project jans by JanssenProject.

the class CrossEncryptionTest method testDecryptWithGluuDecrypter.

public boolean testDecryptWithGluuDecrypter(String jwe) {
    try {
        JWK jwk = JWK.parse(recipientJwkJson);
        RSAPrivateKey rsaPrivateKey = ((RSAKey) jwk).toRSAPrivateKey();
        JweDecrypterImpl decrypter = new JweDecrypterImpl(rsaPrivateKey);
        decrypter.setKeyEncryptionAlgorithm(KeyEncryptionAlgorithm.RSA_OAEP);
        decrypter.setBlockEncryptionAlgorithm(BlockEncryptionAlgorithm.A128GCM);
        final String decryptedPayload = decrypter.decrypt(jwe).getClaims().toJsonString();
        System.out.println("Gluu decrypt succeed: " + decryptedPayload);
        if (isJsonEqual(decryptedPayload, PAYLOAD)) {
            return true;
        }
    } catch (Exception e) {
        System.out.println("Gluu decrypt failed: " + e.getMessage());
        e.printStackTrace();
    }
    return false;
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) JweDecrypterImpl(io.jans.as.model.jwe.JweDecrypterImpl) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) JOSEException(com.nimbusds.jose.JOSEException) JSONException(org.json.JSONException) InvalidJwtException(io.jans.as.model.exception.InvalidJwtException) ParseException(java.text.ParseException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) CryptoProviderException(io.jans.as.model.exception.CryptoProviderException) IOException(java.io.IOException) InvalidJweException(io.jans.as.model.exception.InvalidJweException) NoSuchProviderException(java.security.NoSuchProviderException) JWK(com.nimbusds.jose.jwk.JWK)

Example 2 with JweDecrypterImpl

use of io.jans.as.model.jwe.JweDecrypterImpl in project jans by JanssenProject.

the class CrossEncryptionTest method decryptAndValidateSignatureWithGluu.

private void decryptAndValidateSignatureWithGluu(String jweString) throws ParseException, JOSEException, InvalidJweException, JSONException, InvalidJwtException {
    JWK jwk = JWK.parse(recipientJwkJson);
    RSAPrivateKey rsaPrivateKey = ((RSAKey) jwk).toRSAPrivateKey();
    JweDecrypterImpl decrypter = new JweDecrypterImpl(rsaPrivateKey);
    decrypter.setKeyEncryptionAlgorithm(KeyEncryptionAlgorithm.RSA_OAEP);
    decrypter.setBlockEncryptionAlgorithm(BlockEncryptionAlgorithm.A128GCM);
    final Jwe jwe = decrypter.decrypt(jweString);
    assertEquals(jwe.getHeader().getContentType(), JwtType.JWT);
    final Jwt jwt = jwe.getSignedJWTPayload();
    final RSAPublicKey senderPublicKey = RSAKeyFactory.valueOf(getSenderWebKey()).getPublicKey();
    Assert.assertTrue(new RSASigner(SignatureAlgorithm.RS256, senderPublicKey).validate(jwt));
    System.out.println("Gluu decrypt and nested jwt signature verification succeed: " + jwt.getClaims().toJsonString());
}
Also used : RSAKey(com.nimbusds.jose.jwk.RSAKey) RSAPublicKey(io.jans.as.model.crypto.signature.RSAPublicKey) JweDecrypterImpl(io.jans.as.model.jwe.JweDecrypterImpl) Jwt(io.jans.as.model.jwt.Jwt) RSASigner(io.jans.as.model.jws.RSASigner) Jwe(io.jans.as.model.jwe.Jwe) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) JWK(com.nimbusds.jose.jwk.JWK)

Aggregations

JWK (com.nimbusds.jose.jwk.JWK)2 RSAKey (com.nimbusds.jose.jwk.RSAKey)2 JweDecrypterImpl (io.jans.as.model.jwe.JweDecrypterImpl)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2 JOSEException (com.nimbusds.jose.JOSEException)1 RSAPublicKey (io.jans.as.model.crypto.signature.RSAPublicKey)1 CryptoProviderException (io.jans.as.model.exception.CryptoProviderException)1 InvalidJweException (io.jans.as.model.exception.InvalidJweException)1 InvalidJwtException (io.jans.as.model.exception.InvalidJwtException)1 Jwe (io.jans.as.model.jwe.Jwe)1 RSASigner (io.jans.as.model.jws.RSASigner)1 Jwt (io.jans.as.model.jwt.Jwt)1 IOException (java.io.IOException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 SignatureException (java.security.SignatureException)1 ParseException (java.text.ParseException)1 JSONException (org.json.JSONException)1