@Test public void testChangePassword() throws IOException, GeneralSecurityException, PasswordException { File encryptedFile = new File(testDir, "encrypted"); OutputStream outputStream = new EncryptedOutputStream(new FileOutputStream(encryptedFile), FileEncryptionTestUtil.deriveKey(password)); outputStream.write(plainText); outputStream.close(); byte[] newPassword = "new password".getBytes(); FileEncryptionUtil.changePassword(encryptedFile, password, FileEncryptionTestUtil.deriveKey(newPassword)); InputStream inputStream = new EncryptedInputStream(new FileInputStream(encryptedFile), newPassword); byte[] decryptedText = Util.readBytes(inputStream); assertArrayEquals(plainText, decryptedText); encryptedFile.delete(); }
public static void changePassword(File file, byte[] oldPassword, DerivedKey newKey) throws IOException, GeneralSecurityException, PasswordException { InputStream inputStream = null; byte[] decryptedData = null; try { inputStream = new EncryptedInputStream(new FileInputStream(file), oldPassword); decryptedData = Util.readBytes(inputStream); } finally { if (inputStream != null) inputStream.close(); } OutputStream outputStream = null; try { outputStream = new EncryptedOutputStream(new FileOutputStream(file), newKey); outputStream.write(decryptedData); } finally { if (outputStream != null) outputStream.close(); } }
FileEncryptionUtil { public static void changePassword(File file, byte[] oldPassword, DerivedKey newKey) throws IOException, GeneralSecurityException, PasswordException { InputStream inputStream = null; byte[] decryptedData = null; try { inputStream = new EncryptedInputStream(new FileInputStream(file), oldPassword); decryptedData = Util.readBytes(inputStream); } finally { if (inputStream != null) inputStream.close(); } OutputStream outputStream = null; try { outputStream = new EncryptedOutputStream(new FileOutputStream(file), newKey); outputStream.write(decryptedData); } finally { if (outputStream != null) outputStream.close(); } } }
FileEncryptionUtil { public static void changePassword(File file, byte[] oldPassword, DerivedKey newKey) throws IOException, GeneralSecurityException, PasswordException { InputStream inputStream = null; byte[] decryptedData = null; try { inputStream = new EncryptedInputStream(new FileInputStream(file), oldPassword); decryptedData = Util.readBytes(inputStream); } finally { if (inputStream != null) inputStream.close(); } OutputStream outputStream = null; try { outputStream = new EncryptedOutputStream(new FileOutputStream(file), newKey); outputStream.write(decryptedData); } finally { if (outputStream != null) outputStream.close(); } }  }
FileEncryptionUtil { public static void changePassword(File file, byte[] oldPassword, DerivedKey newKey) throws IOException, GeneralSecurityException, PasswordException { InputStream inputStream = null; byte[] decryptedData = null; try { inputStream = new EncryptedInputStream(new FileInputStream(file), oldPassword); decryptedData = Util.readBytes(inputStream); } finally { if (inputStream != null) inputStream.close(); } OutputStream outputStream = null; try { outputStream = new EncryptedOutputStream(new FileOutputStream(file), newKey); outputStream.write(decryptedData); } finally { if (outputStream != null) outputStream.close(); } }  static boolean isPasswordCorrect(byte[] password, File passwordFile); static void writePasswordFile(File passwordFile, byte[] password, DerivedKey newKey); static void changePassword(File file, byte[] oldPassword, DerivedKey newKey); }
FileEncryptionUtil { public static void changePassword(File file, byte[] oldPassword, DerivedKey newKey) throws IOException, GeneralSecurityException, PasswordException { InputStream inputStream = null; byte[] decryptedData = null; try { inputStream = new EncryptedInputStream(new FileInputStream(file), oldPassword); decryptedData = Util.readBytes(inputStream); } finally { if (inputStream != null) inputStream.close(); } OutputStream outputStream = null; try { outputStream = new EncryptedOutputStream(new FileOutputStream(file), newKey); outputStream.write(decryptedData); } finally { if (outputStream != null) outputStream.close(); } }  static boolean isPasswordCorrect(byte[] password, File passwordFile); static void writePasswordFile(File passwordFile, byte[] password, DerivedKey newKey); static void changePassword(File file, byte[] oldPassword, DerivedKey newKey);  }