Search in sources :

Example 1 with EncryptionProviderException

use of com.bluenimble.platform.security.EncryptionProviderException in project serverless by bluenimble.

the class Json method store.

public static void store(JsonObject source, File file, String paraphrase, boolean base64) throws IOException {
    if (source == null) {
        source = new JsonObject();
    }
    OutputStream os = null;
    try {
        os = Lang.isNullOrEmpty(paraphrase) ? new FileOutputStream(file) : new ByteArrayOutputStream();
        IOUtils.copy(new ByteArrayInputStream(source.toString(2).getBytes()), os);
    } finally {
        if (Lang.isNullOrEmpty(paraphrase)) {
            IOUtils.closeQuietly(os);
        }
    }
    if (!Lang.isNullOrEmpty(paraphrase)) {
        OutputStream out = null;
        try {
            out = base64 ? new ByteArrayOutputStream() : new FileOutputStream(file);
            EncryptionProvider.Default.crypt(new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray()), out, pad(paraphrase), Mode.Encrypt);
            if (base64) {
                byte[] bytes = Base64.encodeBase64(((ByteArrayOutputStream) out).toByteArray());
                out = new FileOutputStream(file);
                out.write(bytes);
                out.flush();
            }
        } catch (EncryptionProviderException e) {
            throw new IOException(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(out);
        }
    }
}
Also used : EncryptionProviderException(com.bluenimble.platform.security.EncryptionProviderException) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) JsonObject(com.bluenimble.platform.json.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with EncryptionProviderException

use of com.bluenimble.platform.security.EncryptionProviderException in project serverless by bluenimble.

the class AESEncryptionProvider method crypt.

@Override
public void crypt(InputStream is, OutputStream os, String key, Mode mode) throws EncryptionProviderException {
    int m = Cipher.ENCRYPT_MODE;
    switch(mode) {
        case Encrypt:
            m = Cipher.ENCRYPT_MODE;
            break;
        case Decrypt:
            m = Cipher.DECRYPT_MODE;
            break;
        default:
            break;
    }
    CipherOutputStream cos = null;
    try {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES", provider);
        cipher.init(m, secretKey);
        if (m == Cipher.ENCRYPT_MODE) {
            CipherInputStream cis = new CipherInputStream(is, cipher);
            IOUtils.copy(cis, os);
        } else if (m == Cipher.DECRYPT_MODE) {
            cos = new CipherOutputStream(os, cipher);
            IOUtils.copy(is, cos);
        }
    } catch (Exception e) {
        throw new EncryptionProviderException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(cos);
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) EncryptionProviderException(com.bluenimble.platform.security.EncryptionProviderException) CipherInputStream(javax.crypto.CipherInputStream) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher) EncryptionProviderException(com.bluenimble.platform.security.EncryptionProviderException)

Aggregations

EncryptionProviderException (com.bluenimble.platform.security.EncryptionProviderException)2 JsonObject (com.bluenimble.platform.json.JsonObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Cipher (javax.crypto.Cipher)1 CipherInputStream (javax.crypto.CipherInputStream)1 CipherOutputStream (javax.crypto.CipherOutputStream)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1