Search in sources :

Example 1 with AesGcmJWEEncryptionProvider

use of org.keycloak.jose.jwe.enc.AesGcmJWEEncryptionProvider in project keycloak by keycloak.

the class JWETest method testKeyEncryption_ContentEncryptionAesGcm.

private void testKeyEncryption_ContentEncryptionAesGcm(String jweAlgorithmName, String jweEncryptionName) throws Exception {
    // generate key pair for KEK
    KeyPair keyPair = KeyUtils.generateRsaKeyPair(2048);
    JWEAlgorithmProvider jweAlgorithmProvider = new RsaKeyEncryptionJWEAlgorithmProvider(getJcaAlgorithmName(jweAlgorithmName));
    JWEEncryptionProvider jweEncryptionProvider = new AesGcmJWEEncryptionProvider(jweEncryptionName);
    JWEHeader jweHeader = new JWEHeader(jweAlgorithmName, jweEncryptionName, null);
    JWE jwe = new JWE().header(jweHeader).content(PAYLOAD.getBytes(StandardCharsets.UTF_8));
    jwe.getKeyStorage().setEncryptionKey(keyPair.getPublic());
    String encodedContent = jwe.encodeJwe(jweAlgorithmProvider, jweEncryptionProvider);
    System.out.println("Encoded content: " + encodedContent);
    System.out.println("Encoded content length: " + encodedContent.length());
    jwe = new JWE();
    jwe.getKeyStorage().setDecryptionKey(keyPair.getPrivate());
    jwe.verifyAndDecodeJwe(encodedContent, jweAlgorithmProvider, jweEncryptionProvider);
    String decodedContent = new String(jwe.getContent(), StandardCharsets.UTF_8);
    System.out.println("Decoded content: " + decodedContent);
    System.out.println("Decoded content length: " + decodedContent.length());
    Assert.assertEquals(PAYLOAD, decodedContent);
}
Also used : KeyPair(java.security.KeyPair) RsaKeyEncryptionJWEAlgorithmProvider(org.keycloak.jose.jwe.alg.RsaKeyEncryptionJWEAlgorithmProvider) JWEAlgorithmProvider(org.keycloak.jose.jwe.alg.JWEAlgorithmProvider) RsaKeyEncryptionJWEAlgorithmProvider(org.keycloak.jose.jwe.alg.RsaKeyEncryptionJWEAlgorithmProvider) AesGcmJWEEncryptionProvider(org.keycloak.jose.jwe.enc.AesGcmJWEEncryptionProvider) JWEEncryptionProvider(org.keycloak.jose.jwe.enc.JWEEncryptionProvider) AesCbcHmacShaJWEEncryptionProvider(org.keycloak.jose.jwe.enc.AesCbcHmacShaJWEEncryptionProvider) AesGcmJWEEncryptionProvider(org.keycloak.jose.jwe.enc.AesGcmJWEEncryptionProvider)

Aggregations

KeyPair (java.security.KeyPair)1 JWEAlgorithmProvider (org.keycloak.jose.jwe.alg.JWEAlgorithmProvider)1 RsaKeyEncryptionJWEAlgorithmProvider (org.keycloak.jose.jwe.alg.RsaKeyEncryptionJWEAlgorithmProvider)1 AesCbcHmacShaJWEEncryptionProvider (org.keycloak.jose.jwe.enc.AesCbcHmacShaJWEEncryptionProvider)1 AesGcmJWEEncryptionProvider (org.keycloak.jose.jwe.enc.AesGcmJWEEncryptionProvider)1 JWEEncryptionProvider (org.keycloak.jose.jwe.enc.JWEEncryptionProvider)1