public static byte[] readFromEncryptedFile(EncryptionKey encryptionKey, File file) throws IOException, NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, EncryptionException { final byte[] bytes = Util.readBytes(file); return decrypt(encryptionKey, bytes); }
private byte[] readFromFile(File file, boolean encrypted) throws NoSuchPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException, EncryptionException { return encrypted ? Encryptor.readFromEncryptedFile(encryptionKey, file) : Util.readBytes(file); }
public static <T extends SerializableObject> T deserialize(File file, Class<T> cls, EncryptionKey encryptionKey) throws IOException { try { final byte[] encryptedBytes = Util.readBytes(file); final byte[] unencryptedBytes = Encryptor.decrypt(encryptionKey, encryptedBytes); ByteArrayInputStream stream = null; try { stream = new ByteArrayInputStream(unencryptedBytes); DataInputStream in = new DataInputStream(stream); Constructor<T> constructor = cls.getDeclaredConstructor(DataInput.class); constructor.setAccessible(true); return constructor.newInstance(in); } finally { Util.ensureClosed(stream); } } catch (Exception e) { throw new IOException("Unable to instantiate class: " + cls, e); } } }
writeToFile(payloadFile, Util.readBytes(payloadFile), shouldWriteEncrypted);
@Override protected Object deserialize(File file) throws SerializerException { try { final byte[] encryptedBytes = Util.readBytes(file); final byte[] unencryptedBytes = Encryptor.decrypt(encryptionKey, encryptedBytes); ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { bis = new ByteArrayInputStream(unencryptedBytes); ois = new ObjectInputStream(bis); return ois.readObject(); } catch (Exception e) { throw new SerializerException(e); } finally { Util.ensureClosed(bis); Util.ensureClosed(ois); } } catch (Exception e) { throw new SerializerException(e); } } }