I am encrypting a file created on the android app in a Bluetooth service. In another class later on I want to decrypt this file and upload it to a server.
For encryption I am using the AndroidX androidx.security:security-crypto:1.0.0-alpha02 library which is a wrapper around Tink. I have read all the developer docs and tutorials I could find for EncryptedFile, EncryptedFile.Builder, and so on.
I encrypt the file as follows:
String keySetAlias = "BilboBaggins";
String keySetPref = "Hobbits";
EncryptedFile m_StudyChannelEncryptedFile = new EncryptedFile.Builder(
filePath,
getApplicationContext(),
masterKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB).setKeysetAlias(keySetAlias).setKeysetPrefName(keySetPref).build();
m_output = m_StudyChannelEncryptedFile.openFileOutput();
From here I can write to a file like with a normal FileOutputStream, and from looking at the data that is written in the phone's storage I can confirm that it is encrypted.
Prior to uploading, I attempt to do the same thing in another class and then decrypt it.
String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
String keySetAlias = "BilboBaggins";
String keySetPref = "Hobbits";
EncryptedFile encryptedFile = new EncryptedFile.Builder(
filePath,
getApplicationContext(),
masterKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB).setKeysetAlias(keySetAlias).setKeysetPrefName(keySetPref).build();
// Read channel data file
FileInputStream fChannel = encryptedFile.openFileInput();
m_Dat1Size = fChannel.available();
From here the issue is that the available size of the file I'm getting is zero - like it doesn't exist. I can confirm that the original data written is not overwritten though as the file on the phone storage still has encrypted data.
I believe that by providing it with a location keySetAlias, keySetPref the EncryptedFile builder should be able to initialise an EncryptedFile instance which will have the correct keys.
I would appreciate any help or insight!
Thank you, Michael