Register now for Android Dev Summit 2019!

EncryptedFile

public final class EncryptedFile
extends Object

java.lang.Object
   ↳ 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

class EncryptedFile.Builder

Builder class to configure EncryptedFile  

enum EncryptedFile.FileEncryptionScheme

The encryption scheme to encrypt files. 

Public methods

FileInputStream openFileInput()

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

FileOutputStream openFileOutput()

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

Inherited methods

Public methods

openFileInput

public FileInputStream openFileInput ()

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.

Returns
FileInputStream The input stream to read previously encrypted data.

Throws
GeneralSecurityException when a bad master key or keyset has been used
IOException when the file was not found

openFileOutput

public FileOutputStream openFileOutput ()

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.

Returns
FileOutputStream The FileOutputStream that encrypts all data.

Throws
GeneralSecurityException when a bad master key or keyset has been used
IOException when the file already exists or is not available for writing