A nonce in cryptography is a value that is used only once within a given context — a random number, timestamp, counter or combination of these that has at most a negligible chance of repeating. It makes each encryption, signature or authentication unique so that identical messages produce different outputs and old messages cannot be replayed. A nonce does not need to be secret, but it must never repeat under the same key.
Key takeaways
- A nonce is a value used only once per cryptographic operation under a given key; its job is uniqueness, not secrecy.
- Nonces stop replay attacks, make identical plaintexts encrypt differently, and keep signatures from leaking private keys.
- Reusing a nonce is catastrophic: in AES-GCM it can expose plaintext, and in ECDSA it can reveal the private key.
Every time you log in, send an encrypted message or sign a transaction, a small number you never see does a quiet but critical job: it makes sure that exact operation has never happened before, and will never happen again.
What is a nonce in cryptography?
A nonce is a value that is used only once within a given cryptographic context. The word is short for "number used once", and NIST defines it as a time-varying value that has at most a negligible chance of repeating.
That value can be a random number generated fresh for each use, a timestamp, a sequence number, or a combination of these. NIST's glossary lists all of these as valid constructions, and the key point is the property, not the form: whatever you use, it must not repeat while the same key is in use.
A nonce is not a key, and it does not need to be secret. In most designs it is transmitted in the clear alongside the data it protects. Its entire security contribution comes from being fresh. NIST's glossary defines it as a time-varying value with at most a negligible chance of repeating — see the NIST definition of a cryptographic nonce.
Why do cryptography systems need a nonce?
Without a nonce, cryptography would be dangerously repetitive. The same plaintext encrypted with the same key would always produce the same ciphertext, which tells an attacker that two messages are identical — and lets them replay an old message as if it were new.
Nonces solve three problems at once. They give each operation freshness, so a receiver can tell a live message from a recorded one. They give uniqueness, so identical inputs produce different outputs. And they give replay protection: an attacker who records a valid message and sends it again will be caught, because the nonce has already been used.
How does a nonce work in practice?
The simplest picture is a challenge and response. A server generates a random nonce and sends it to the client as a challenge. The client combines the nonce with its credentials, computes a cryptographic hash or signature, and sends the result back.
Because the server chose the nonce at random, an attacker who recorded an earlier login cannot reuse it: the next login will use a different nonce, and the old response will not match. The server keeps a record of nonces it has seen, or checks that each one is fresh, and rejects anything it has already accepted.
The same pattern appears in encryption. A stream cipher combines the key with a nonce to produce a unique keystream for that one message. Change the nonce and the keystream changes completely, even though the key is the same. That is why a nonce is sometimes called an initialization vector: in many modes, the IV is the nonce.
What are the types of nonce?
Nonces come in three main forms, and the right one depends on what you are protecting. Each trades randomness against the need to track state.
| Type | How it is made | Strengths | Risks |
|---|---|---|---|
| Random nonce | Fresh random value per use | No state to track | Needs a good random generator; collisions possible if too short |
| Counter nonce | Sequence number that increases | Guaranteed unique if state survives | Breaks if the counter resets, e.g. after a restore |
| Timestamp nonce | Current time | Simple, no storage needed | Clock skew and low resolution can cause repeats |
| Combined nonce | Fixed field plus counter or random part | Best of both; used in AES-GCM | More complex to implement correctly |
NIST SP 800-38D, which defines AES-GCM, allows three approaches: a deterministic counter, a randomly generated value with a collision-probability calculation, or a fixed field combined with an invocation field. The standard recommends a 96-bit nonce, and it limits AES-GCM to 2^32 invocations per key when random 96-bit nonces are used, which keeps the collision probability below 2^-32.
Where are nonces used?
Nonces appear in almost every cryptographic system you use daily, often under a different name. Once you know what to look for, you will see them everywhere.
- Authenticated encryption: in AES-GCM, the IV is the nonce, and it must be unique for every encryption under a given key.
- Digital signatures: ECDSA and DSA use a per-signature nonce, usually called k, that must be unique and unpredictable for each signature.
- Authentication protocols: challenge-response logins, Kerberos tickets and TLS handshakes all use nonces to prove freshness.
- Content Security Policy: a web server puts a fresh random nonce in a response header and on trusted script tags; the browser runs only scripts whose nonce matches.
- Blockchain: in proof-of-work mining, the nonce is a 32-bit number in the block header that miners vary while hashing, searching for a hash below the target.
- Password hashing: the related idea of a salt gives each stored password hash a unique value, though a salt is stored, not used once and discarded.
In TLS, the handshake uses random values from both sides that act as nonces, binding the session keys to that one connection. If you want to see how a modern protocol carries these values, our free JWT decoder shows the structure of the tokens that carry similar claims.
What happens if you reuse a nonce?
Reusing a nonce is one of the most damaging mistakes in cryptography. The consequences depend on the algorithm, but they are always serious — and in some cases they hand the attacker the private key.
In AES-GCM, nonce reuse under the same key destroys both confidentiality and authentication. An attacker who sees two ciphertexts encrypted with the same nonce can XOR them together to recover the plaintexts, and can forge valid-looking messages. That is why NIST SP 800-38D is so strict about uniqueness.
In ECDSA, the damage is worse. Each signature uses a secret per-signature value, k, as its nonce. If the same k is used for two different signatures, an attacker can solve for the private key with simple algebra.
This is not theoretical: attackers have observed transactions with repeated or predictable nonces and extracted private keys, stealing funds. The fix, standardized in RFC 6979, is to derive k deterministically from the private key and the message hash using HMAC.
Nonce vs IV vs salt: what's the difference?
These three terms are often confused because they all add uniqueness to a cryptographic operation. The difference is what they protect and how long they live.
| Value | Job | Secret? | Reused? |
|---|---|---|---|
| Nonce | Make one operation unique | No | Never, under the same key |
| IV | Starting value for an encryption mode | No | Never, under the same key |
| Salt | Make one password hash unique | No | One salt per password, stored with the hash |
| Key | The actual secret | Yes, always | Yes, across many operations |
The simplest way to remember it: the key is the secret, the nonce is the one-time serial number, the IV is where the nonce goes in an encryption mode, and the salt is the nonce's cousin for stored passwords. If you generate a password for an account, use our free password generator — and store it in a password manager, not in a file.
How do you use nonces safely?
Safe nonce use is mostly about discipline: pick the right construction for your algorithm, and never let the same value appear twice under the same key.
- Use a vetted library. Modern libraries generate nonces correctly and refuse to reuse them. Do not write your own.
- Use a cryptographically secure random source. Your platform's secure generator exists for this; a general-purpose random function does not qualify.
- Respect the limits. AES-GCM's 2^32 invocation limit per key is a hard ceiling, not a suggestion — rotate keys before you approach it.
- For ECDSA, use RFC 6979 or EdDSA. Deterministic nonces eliminate the whole class of random-generator failures. Ed25519 uses deterministic nonces by design.
- Protect counters across restores. If you use a counter nonce, make sure a backup restore cannot rewind it — a rewound counter is a reused nonce.
The bottom line
A nonce is a value used once: a small, non-secret number that makes each cryptographic operation unique, fresh and unreplayable. It is the reason the same message encrypts differently every time and the reason an old login cannot be replayed.
The rule is simple and absolute: never reuse a nonce under the same key. Get that right, use a vetted library, and the rest of the cryptography can do its job. You can explore how hashes and encodings work in practice with our free hash generator.
Frequently asked questions
What is a nonce in simple terms?
A nonce is a number used once. It is a unique value added to a cryptographic operation so that doing the same thing twice gives a different result. Think of it as a serial number for a single use: it stops attackers from copying an old message and sending it again.
Does a nonce need to be secret?
No. In most designs the nonce is sent in the clear alongside the data it protects. Its security comes from being fresh and unique, not from being hidden. What must stay secret is the key.
What is the difference between a nonce and an IV?
An initialization vector, or IV, is the starting value fed into an encryption mode, and in many modes it plays the role of a nonce. In AES-GCM, for example, the IV is the nonce. The difference is mostly one of naming: nonce describes the requirement that the value be used once, while IV describes where the value goes.
What is a nonce in blockchain?
In proof-of-work blockchains such as Bitcoin, the nonce is a 32-bit number in the block header that miners change repeatedly while hashing the block, looking for a hash below a target value. It is a different use of the same idea: a value varied until a condition is met.
What happens if you reuse a nonce?
It depends on the algorithm, but it is always bad. In AES-GCM, reusing a nonce under the same key can expose the plaintext and destroy authentication. In ECDSA signatures, reusing the nonce across two signatures can let an attacker recover the private key.
How long should a nonce be?
It depends on the algorithm. AES-GCM normally uses a 96-bit nonce, which NIST SP 800-38D recommends. Shorter nonces raise the chance of an accidental repeat, so follow the standard for the algorithm you use and never shorten a nonce to save space.
Is a nonce the same as a salt?
No, though both add uniqueness. A salt is stored with a password hash so that identical passwords hash differently. A nonce is used once per operation to make each encryption or signature unique. Salts protect stored passwords; nonces protect live operations.
What is a nonce in a Content Security Policy?
In a Content Security Policy, a nonce is a one-time token the server puts in a response header and on trusted script tags. The browser runs only scripts whose nonce matches the header, which blocks injected scripts. The server generates a fresh random nonce for every page load.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against the NIST CSRC glossary definition of a cryptographic nonce, NIST SP 800-38D and RFC 6979.
How we write and fact-check →