← All articlesMobile applications

How to Store an IV with an Android Record Without Reusing It on Updates

An IV can be stored alongside an encrypted record: this parameter does not need to be secret. The main task is to manage its lifecycle correctly. Reading requires the stored IV, while a new encryption operation, including a record update, requires a new one. We will explore these requirements using a wholly fictional Android notebook app called Leaf and synthetic notes. The result is a storage format and verifiable acceptance criteria, not a promise of absolute protection.

The Boundary Between Public Parameters and the Secret Key

In our example notebook, a note contains the synthetic text ‘Buy three blue pencils.’ For its encrypted representation, we will define separate fields for the format version, mode designation, key identifier, IV, and encryption output. The IV is stored with the ciphertext; the secret key is excluded from this structure. The key identifier is only a reference used to select the key, not the key material itself.

This defines the division of responsibilities: record storage holds the parameters needed for reading, while the application manages the key separately and performs cryptographic operations. The fact that the IV is public does not permit its reuse on the next save. In GCM, it must not repeat with the same key, and this rule applies to all operations using that key, not just a single note.

Choosing a Mode Without Mixing Examples

When compatibility with an external system is not required, Android recommends AES in CBC or GCM mode with a 256-bit key. For a new implementation, OWASP recommends an authenticated mode, such as AES/GCM/NoPadding. Unauthenticated CBC should not be treated as an equivalent replacement. For our fictional notebook, we will choose GCM and document that decision in the requirements.

The Java example in the Android documentation retrieves the IV through cipher.getIV(), but the example itself uses AES/CBC/PKCS5PADDING. It cannot be copied wholesale into a GCM implementation without accounting for the differences. GCM parameters on Android use GCMParameterSpec rather than the older IvParameterSpec.

Creating and Saving a Record Again

We will define the save rule as follows: every new encryption operation receives an IV generated through a cryptographically secure method that ensures uniqueness. Constants, zeros, and reuse of the previous IV are excluded. Updating an existing note also counts as a new encryption operation; the previous IV does not become acceptable merely because the record identifier remains the same.

For Leaf, we will specify this sequence: prepare a new encrypted representation, retrieve the IV for that operation, and save the matching pair of IV and encryption output. The storage update must ensure that subsequent reads use exactly this pair once the save completes. If an update is interrupted, the application must not treat a mixture of old ciphertext and a new IV as successfully saved. This design requirement needs a separate check.

Reading Uses the Stored IV

When reading, the application retrieves the IV from the selected version of the record and passes it as a decryption parameter. There is no need to generate a new IV for reading: a new parameter belongs to a new encryption operation. It helps to distinguish these two paths explicitly in the requirements so that shared save and load code does not conceal IV reuse.

The read criterion in this example is an exact match between the recovered text and the expected synthetic note. After an edit, we expect the updated text, such as ‘Buy five green pencils.’ The absence of an exception or the successful completion of a call does not, by itself, confirm that the application has read the current version.

A Test Plan Using Your Own Synthetic Data

Run these checks only on a test notebook owned by the team. For each save, record the test record identifier, key reference, IV, and expected text without logging the secret key. Compare observed results against predefined acceptance criteria.

  • Creation: the IV from the completed operation is stored, and reading the record again returns the original synthetic text.
  • Editing: the IV differs from the previous one for the same key; reopening the note shows the new text.
  • Saving the same text again: if a new encryption operation occurs, the previous IV is not reused.
  • Multiple records sharing one key: no IV repeats among the operations checked.
  • Interrupted update: reopening provides a consistent version of the record; an incomplete change is not marked as accepted.

Keys, Errors, and the Limits of Testing

Android Keystore allows operations with keys while keeping the key material non-exportable. Key usage restrictions cannot be changed after generation or import, so the required permissions should be defined in advance. However, a compromised application process may still be able to use its Keystore keys: non-exportability does not mean complete protection of the process.

Do not expose decryption error details that could serve as an oracle. Include a clear, general message that the record cannot be read in the interface acceptance criteria. Also account for the limits of testing: the absence of repeated IVs in the checked sample does not prove uniqueness across all future operations. The generation implementation and every save path also need review. Recovering a lost key remains a separate task.

Fix Priorities and the Next Step

First, eliminate constant or reused IVs from every path that performs new encryption. Then formalize the record format and consistent saving of its fields, and add creation, editing, and reading checks. Next, review Keystore settings and error messages. Acceptance must confirm the change in stored state and the exact read result, rather than just a successful operation response.

If your product has a website or web component, you can start with a free website/web-product audit. It does not replace a separate review of the Android implementation, IV lifecycle, and key management.

Sources and documentation

  1. Cryptography
  2. Android Keystore system
  3. OWASP MASWE-0007: Improper Encryption
  4. OWASP Mobile App Cryptography
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free