use of org.jose4j.jwe.JsonWebEncryption in project cas by apereo.
the class EncodingUtils method encryptValueAsJwt.
/**
* Encrypt the value based on the seed array whose length was given,
* and the key and content encryption ids.
*
* @param secretKeyEncryptionKey the secret key encryption key
* @param value the value
* @param algorithmHeaderValue the algorithm header value
* @param encryptionMethodHeaderParameter the content encryption algorithm identifier
* @param keyIdHeaderValue the key id header value
* @param customHeaders the custom headers
* @return the encoded value
*/
public static String encryptValueAsJwt(final Key secretKeyEncryptionKey, final Serializable value, final String algorithmHeaderValue, final String encryptionMethodHeaderParameter, final String keyIdHeaderValue, final Map<String, Object> customHeaders) {
try {
val jwe = new JsonWebEncryption();
jwe.setPayload(value.toString());
jwe.enableDefaultCompression();
jwe.setAlgorithmHeaderValue(algorithmHeaderValue);
jwe.setEncryptionMethodHeaderParameter(encryptionMethodHeaderParameter);
jwe.setKey(secretKeyEncryptionKey);
jwe.setContentTypeHeaderValue("JWT");
jwe.setHeader("typ", "JWT");
customHeaders.forEach((k, v) -> jwe.setHeader(k, v.toString()));
if (StringUtils.isNotBlank(keyIdHeaderValue)) {
jwe.setKeyIdHeaderValue(keyIdHeaderValue);
}
LOGGER.trace("Encrypting via [{}]", encryptionMethodHeaderParameter);
return jwe.getCompactSerialization();
} catch (final Exception e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
use of org.jose4j.jwe.JsonWebEncryption in project cas by apereo.
the class EncodingUtils method decryptJwtValue.
/**
* Decrypt value based on the key created.
*
* @param secretKeyEncryptionKey the secret key encryption key
* @param value the value
* @return the decrypted value
*/
public static String decryptJwtValue(final Key secretKeyEncryptionKey, final String value) {
try {
val jwe = new JsonWebEncryption();
jwe.setKey(secretKeyEncryptionKey);
jwe.setCompactSerialization(value);
LOGGER.trace("Decrypting value...");
return jwe.getPayload();
} catch (final Exception e) {
if (LOGGER.isTraceEnabled()) {
throw new DecryptionException(e);
}
throw new DecryptionException();
}
}
use of org.jose4j.jwe.JsonWebEncryption in project oxAuth by GluuFederation.
the class CrossEncryptionTest method testDecryptWithJose4J.
public boolean testDecryptWithJose4J(String jwe) {
try {
PublicJsonWebKey jwk = PublicJsonWebKey.Factory.newPublicJwk(recipientJwkJson);
JsonWebEncryption receiverJwe = new JsonWebEncryption();
AlgorithmConstraints algConstraints = new AlgorithmConstraints(ConstraintType.WHITELIST, KeyManagementAlgorithmIdentifiers.RSA_OAEP);
receiverJwe.setAlgorithmConstraints(algConstraints);
AlgorithmConstraints encConstraints = new AlgorithmConstraints(ConstraintType.WHITELIST, ContentEncryptionAlgorithmIdentifiers.AES_128_GCM);
receiverJwe.setContentEncryptionAlgorithmConstraints(encConstraints);
receiverJwe.setKey(jwk.getPrivateKey());
receiverJwe.setCompactSerialization(jwe);
final String decryptedPayload = new String(Base64Util.base64urldecode(receiverJwe.getPlaintextString()));
System.out.println("Jose4j decrypt succeed: " + decryptedPayload);
if (isJsonEqual(decryptedPayload, PAYLOAD)) {
return true;
}
} catch (Exception e) {
System.out.println("Jose4j decrypt failed: " + e.getMessage());
e.printStackTrace();
}
return false;
}
Aggregations