android-storage icon indicating copy to clipboard operation
android-storage copied to clipboard

Fail to get the original file after encrypting it and decrypting

Open MRodriguess opened this issue 7 years ago • 0 comments

I am trying to read the database file from a specified app, encrypt it, and save it to external SD CARD. When I try to get it back, the file contents are not the same.

 //Get databaseFile uncrypted
 byte[] originalDbBytes = storage.readFile("database/path");
 
 //This file is ok
 storage.createFile("path/original/file", originalDbBytes);

 //Add encryption for testing purpose only
 String IVX = "abcdefghijklmnop"; // 16 lenght - not secret
 String SECRET_KEY = "secret1234567890"; // 16 lenght - secret
 byte[] SALT = "0000111100001111".getBytes(); // random 16 bytes array
 EncryptConfiguration configuration = new EncryptConfiguration.Builder()
 .setEncryptContent(IVX, SECRET_KEY, SALT)
 .build();

 //Configure the encription on storage instance
 storage.setEncryptConfiguration(configuration);

 //Create encrypted file.
 storage.createFile("path/encrypted/file", originalDbBytes);

 //Read encripted file.
 byte[] bytes = storage.readFile("path/encrypted/file");

 //Write decrypted file-
 storage.createFile("path/decrypted/file", bytes);

 decrypted file is invalid

Any hints on this? Should this work only with textfiles?

MRodriguess avatar Jan 16 '19 11:01 MRodriguess