Initialization vector

Also known as: IV

On this page 7

An initialization vector (IV) is a non-secret input value supplied to a cipher mode alongside the key, so that encrypting the same plaintext twice under the same key produces different ciphertexts. In cryptography, the IV is what turns a deterministic block cipher or stream cipher construction into a semantically secure encryption scheme.

The IV is usually transmitted in the clear next to the ciphertext. Its security depends not on secrecy but on the property each mode requires: some modes need an unpredictable random IV, others only need a value that is never repeated with the same key.

Why encryption needs an initialization vector

An initialization vector exists because a cipher with a fixed key is a deterministic function: the same input always gives the same output. Without an IV, an observer could tell when two messages are identical or share a common prefix, which leaks information even though no key is broken.

The IV adds per-message variation to the computation. In chained modes it is mixed into the first block, and in counter-based modes it forms the starting value of the keystream. Either way, the IV ensures that ciphertexts reveal nothing about repeated plaintexts, the property cryptographers call indistinguishability under chosen-plaintext attack (IND-CPA).

The receiver needs the same IV to decrypt, so protocols typically prepend the IV to the ciphertext or derive it from a shared sequence number. Treating the IV as a secret adds no meaningful security and is not required by any standard mode.

IV requirements in common cipher modes

Each mode of operation places different requirements on the IV, and violating them causes failures of very different severity. The table below lists the requirements for widely used modes with AES and ChaCha20.

ModeTypical IV sizeRequirementEffect of reuse
CBC128 bits (one AES block)Unpredictable and randomReveals equal plaintext prefixes
CTR128 bits including counterUnique per keyKeystream reuse, plaintext XOR leaks
GCM96 bits recommendedUnique per keyConfidentiality and authentication lost
ChaCha20-Poly130596 bitsUnique per keyKeystream reuse and forgery
XChaCha20-Poly1305192 bitsUnique, random is safeSame as ChaCha20 if repeated
AES-GCM-SIV96 bitsUnique preferredOnly reveals identical messages
IV requirements by mode of operation

CBC mode is the classic case where predictability matters. If an attacker can predict the next IV and choose plaintext, the attacker can test guesses about earlier blocks, the weakness exploited by the BEAST attack against TLS 1.0, which reused the last ciphertext block of the previous record as the next IV.

Counter-based modes fail far more severely. When a CTR or GCM nonce repeats under one key, the two messages are encrypted with the same keystream, so XORing the ciphertexts cancels the keystream and yields the XOR of the plaintexts. With any known or guessable content, an attacker can then recover both messages without touching the key.

Initialization vector, nonce and salt

An IV, a cryptographic nonce and a salt are all public, non-secret values that add uniqueness, but they serve different purposes and carry different requirements. In modern authenticated modes, the terms IV and nonce are often used interchangeably, although the word nonce stresses uniqueness rather than randomness.

ValueUsed inCore requirementSecret
Initialization vectorCipher modes such as CBC, CTR, GCMUnique, and random for CBCNo
NonceAEAD ciphers and protocolsNever repeated with one keyNo
SaltPassword hashing and key derivationUnique per stored credentialNo
KeyAll encryptionUnpredictable and protectedYes
How the initialization vector differs from related values

The practical distinction matters during design reviews: a salt protects against precomputed attacks on password hashes, whereas an IV protects the confidentiality of encrypted messages. Using one value for both roles is a common source of subtle errors.

Generating and managing IVs correctly

Correct IV generation depends on the mode and on how many messages a single key will protect. For CBC, the IV must come from a cryptographically secure random number generator; for GCM and ChaCha20-Poly1305, a deterministic counter is often safer than random values because it guarantees uniqueness.

Random 96-bit nonces carry a birthday-bound collision risk. Guidance for AES-GCM in NIST SP 800-38D limits random-IV use to about 2 to the power of 32 messages per key, after which the key must be rotated. Extended-nonce designs such as XChaCha20 make random nonces safe for practically unlimited message counts.

Well-designed libraries reduce these risks by generating the nonce internally and returning it with the ciphertext. Where an API requires the caller to supply an IV, the code review should confirm its source, its uniqueness guarantee and the key rotation limit that applies.

SituationRecommended IV strategyReason
Single sender, persistent stateMonotonic counter per keyGuaranteed uniqueness
Many stateless senders, one keyXChaCha20 with random 192-bit nonceNegligible collision risk
Legacy CBC file encryptionFresh random 128-bit IV per fileMeets unpredictability requirement
Risk of accidental reuseMisuse-resistant AES-GCM-SIVLimits damage if repetition occurs
Choosing an IV strategy

Typical IV mistakes and their consequences

IV errors are among the most frequent real-world cryptographic failures because the code still runs and decrypts correctly, so nothing signals the problem. The following mistakes appear repeatedly in audits.

  • Hard-coded or all-zero IVs make every message with the same key start identically and destroy semantic security.
  • Nonce reuse in GCM lets an attacker recover the authentication subkey and forge messages, as shown in research on TLS servers that repeated nonces.
  • Counters reset after restart silently repeat IVs when state is not stored durably, a typical flaw in embedded devices.
  • Short IVs in legacy protocols such as the 24-bit IV in WEP wrap around quickly, which enabled practical key recovery.

Explained without jargon

An initialization vector is a fresh starting value added each time something is encrypted. It makes sure that the same message never looks the same twice, even when the same key is used.

Imagine writing secret notes with one code book. If you always started the same way, anyone could notice when you repeat yourself. The IV is like a different random first line on every note: it is not secret, but it hides the patterns.

A typical use case

A team building a messaging backend encrypts stored messages with AES-256-GCM. An early prototype generated the 96-bit nonce from the current second, so two messages saved in the same second under the same key received identical nonces. A security review caught the issue before launch.

The team switched to a per-key 64-bit message counter combined with a 32-bit server identifier, stored the counter in the database transactionally and scheduled key rotation well before 2 to the power of 32 messages. Each record now stores its 12-byte nonce in front of the ciphertext, decryption works unchanged, and the design guarantees that no nonce repeats under any key.