# EncryptedFile

Artifact: [androidx.security:security-crypto](https://developer.android.com/jetpack/androidx/releases/security) [View Source](https://cs.android.com/search?q=file:androidx/security/crypto/EncryptedFile.java+class:androidx.security.crypto.EncryptedFile) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

*** ** * ** ***


```
public final class EncryptedFile
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> Use `https://developer.android.com/reference/java/io/File.html` instead.

Class used to create and read encrypted files. **WARNING** : The encrypted file should not be backed up with Auto Backup. When restoring the file it is likely the key used to encrypt it will no longer be present. You should exclude all `EncryptedFile`s from backup using [backup rules](https://developer.android.com/guide/topics/data/autobackup#IncludingFiles). Be aware that if you are not explicitly calling `setKeysetPrefName()` there is also a silently-created default preferences file created at

```
    ApplicationProvider
         .getApplicationContext()
         .getFilesDir()
         .getParent() + "/shared_prefs/__androidx_security_crypto_encrypted_file_pref__"
```
This preferences file (or any others created with a custom specified location) also should be excluded from backups. Basic use of the class:

```
 MasterKey masterKey = new MasterKey.Builder(context)
     .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
     .build();

 File file = new File(context.getFilesDir(), "secret_data");
 EncryptedFile encryptedFile = EncryptedFile.Builder(
     context,
     file,
     masterKey,
     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 types |
|---|
| `public final class https://developer.android.com/reference/androidx/security/crypto/EncryptedFile.Builder` **This class is deprecated.** Use `https://developer.android.com/reference/java/io/File.html` instead. <br /> |
| `public enum https://developer.android.com/reference/androidx/security/crypto/EncryptedFile.FileEncryptionScheme` **This enum is deprecated.** Use `https://developer.android.com/reference/java/io/File.html` instead. <br /> |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/FileInputStream.html` | `[openFileInput](https://developer.android.com/reference/androidx/security/crypto/EncryptedFile#openFileInput())()` **This method is deprecated.** Deprecated in Java |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/FileOutputStream.html` | `[openFileOutput](https://developer.android.com/reference/androidx/security/crypto/EncryptedFile#openFileOutput())()` **This method is deprecated.** Deprecated in Java |

## Public methods

### openFileInput

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull FileInputStream [openFileInput](https://developer.android.com/reference/androidx/security/crypto/EncryptedFile#openFileInput())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/FileInputStream.html` | The input stream to read previously encrypted data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/security/GeneralSecurityException.html` | when a bad master key or keyset has been used |
| `https://developer.android.com/reference/java/io/FileNotFoundException.html` | when the file was not found |
| `https://developer.android.com/reference/java/io/IOException.html` | when other I/O errors occur |

### openFileOutput

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/security#1.0.0) Deprecated in [1.1.0](https://developer.android.com/jetpack/androidx/releases/security#1.1.0)

```
public @NonNull FileOutputStream [openFileOutput](https://developer.android.com/reference/androidx/security/crypto/EncryptedFile#openFileOutput())()
```

> [!CAUTION]
> **This method is deprecated.**   
> Deprecated in Java

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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/FileOutputStream.html` | The FileOutputStream that encrypts all data. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/security/GeneralSecurityException.html` | when a bad master key or keyset has been used |
| `https://developer.android.com/reference/java/io/IOException.html` | when the file already exists or is not available for writing |