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);
}
}
}
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);
}
}
Aggregations