protected void writeToFile(File file, byte[] data, boolean encrypted) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, EncryptionException { if (encrypted) { Encryptor.writeToEncryptedFile(encryptionKey, file, data); } else { Util.writeAtomically(file, data); } }
private void writeToFile(File file, byte[] data, boolean encrypted) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, EncryptionException { if (encrypted) { Encryptor.writeToEncryptedFile(encryptionKey, file, data); } else { Util.writeAtomically(file, data); } }
/** * Writes an object ot an encrypted file */ public static void serialize(File file, SerializableObject object, @NonNull EncryptionKey encryptionKey) throws IOException { ByteArrayOutputStream bos = null; DataOutputStream dos = null; try { bos = new ByteArrayOutputStream(); dos = new DataOutputStream(bos); object.writeExternal(dos); final byte[] unencryptedBytes = bos.toByteArray(); final byte[] encryptedBytes = Encryptor.encrypt(encryptionKey, unencryptedBytes); Util.writeAtomically(file, encryptedBytes); } catch (Exception e) { throw new IOException(e); } finally { Util.ensureClosed(bos); Util.ensureClosed(dos); } }