EncryptedFile

class EncryptedFile
kotlin.Any
   ↳ androidx.security.crypto.EncryptedFile

Class used to create and read encrypted files.

String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
 
   File file = new File(context.getFilesDir(), "secret_data");
   EncryptedFile encryptedFile = EncryptedFile.Builder(
       file,
       context,
       masterKeyAlias,
       EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
   ).build();
 
   // write to the encrypted file
   FileOutputStream encryptedOutputStream = encryptedFile.openFileOutput();
 
   // read the encrypted file
   FileInputStream encryptedInputStream = encryptedFile.openFileInput();
  

Summary

Nested classes

Builder class to configure EncryptedFile

The encryption scheme to encrypt files.

Public methods

FileInputStream

Opens a FileInputStream that reads encrypted files based on the previous settings.

FileOutputStream

Opens a FileOutputStream for writing that automatically encrypts the data based on the provided settings.

Public methods

openFileInput

@NonNull fun openFileInput(): FileInputStream

Opens a FileInputStream that reads encrypted files based on the previous settings. Please ensure that the same master key and keyset are used to decrypt or it will cause failures.

Return
FileInputStream: The input stream to read previously encrypted data.
Exceptions
GeneralSecurityException when a bad master key or keyset has been used
IOException when the file was not found

openFileOutput

@NonNull fun openFileOutput(): FileOutputStream

Opens a FileOutputStream for writing that automatically encrypts the data based on the provided settings. Please ensure that the same master key and keyset are used to decrypt or it will cause failures.

Return
FileOutputStream: The FileOutputStream that encrypts all data.
Exceptions
GeneralSecurityException when a bad master key or keyset has been used
IOException when the file already exists or is not available for writing