Search in sources :

Example 6 with EncryptionResult

use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.

the class SecureQueryDecryptProcessor method decode.

String decode(String string) throws UnsupportedEncodingException {
    byte[] bytes = Base64.decodeBase64(string);
    EncryptionResult result = EncryptionResult.fromByteArray(bytes);
    byte[] clear = null;
    try {
        clear = encryptor.decrypt(result.salt, result.iv, result.cipher);
    } catch (Exception e) {
        log.unableToDecryptValue(e);
    }
    if (clear != null) {
        return new String(clear, StandardCharsets.UTF_8);
    }
    return null;
}
Also used : EncryptionResult(org.apache.knox.gateway.services.security.EncryptionResult) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with EncryptionResult

use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.

the class KnoxSessionStore method uncompressDecryptBase64.

private Serializable uncompressDecryptBase64(final String v) {
    if (v != null && !v.isEmpty()) {
        byte[] bytes = Base64.decodeBase64(v);
        EncryptionResult result = EncryptionResult.fromByteArray(bytes);
        byte[] clear = cryptoService.decryptForCluster(this.clusterName, PAC4J_PASSWORD, result.cipher, result.iv, result.salt);
        if (clear != null) {
            try {
                return javaSerializationHelper.deserializeFromBytes(unCompress(clear));
            } catch (IOException e) {
                throw new TechnicalException(e);
            }
        }
    }
    return null;
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) EncryptionResult(org.apache.knox.gateway.services.security.EncryptionResult) IOException(java.io.IOException)

Example 8 with EncryptionResult

use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.

the class KnoxSessionStore method compressEncryptBase64.

private String compressEncryptBase64(final Object o) {
    if (o == null || o.equals("") || (o instanceof Map<?, ?> && ((Map<?, ?>) o).isEmpty())) {
        return null;
    } else {
        byte[] bytes = javaSerializationHelper.serializeToBytes((Serializable) o);
        /* compress the data  */
        try {
            bytes = compress(bytes);
            if (bytes.length > 3000) {
                logger.warn("Cookie too big, it might not be properly set");
            }
        } catch (final IOException e) {
            throw new TechnicalException(e);
        }
        EncryptionResult result = cryptoService.encryptForCluster(this.clusterName, PAC4J_PASSWORD, bytes);
        return Base64.encodeBase64String(result.toByteAray());
    }
}
Also used : TechnicalException(org.pac4j.core.exception.TechnicalException) EncryptionResult(org.apache.knox.gateway.services.security.EncryptionResult) IOException(java.io.IOException)

Example 9 with EncryptionResult

use of org.apache.knox.gateway.services.security.EncryptionResult in project knox by apache.

the class ConfigurableEncryptor method encrypt.

public EncryptionResult encrypt(byte[] plain) throws Exception {
    byte[] salt = new byte[saltSize];
    SecureRandom rnd = new SecureRandom();
    rnd.nextBytes(salt);
    SecretKey tmp = getKeyFromPassword(new String(passPhrase), salt);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), alg);
    Cipher ecipher = Cipher.getInstance(transformation);
    ecipher.init(Cipher.ENCRYPT_MODE, secret);
    return new EncryptionResult(salt, ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV(), ecipher.doFinal(plain));
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) EncryptionResult(org.apache.knox.gateway.services.security.EncryptionResult) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher)

Aggregations

EncryptionResult (org.apache.knox.gateway.services.security.EncryptionResult)9 IOException (java.io.IOException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 TechnicalException (org.pac4j.core.exception.TechnicalException)2 SecureRandom (java.security.SecureRandom)1 ArrayList (java.util.ArrayList)1 Cipher (javax.crypto.Cipher)1 SecretKey (javax.crypto.SecretKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1