AES encryption and decryption
Runs in your browserYour key, your IV, any mode: encrypt and decrypt AES the way your code does. When decryption fails, the tool tries the usual mix-ups and tells you which one it was. Nothing leaves your browser.
More options
Type a text, then add a key and IV
Or click Generate for a random key and IV. The result updates as you type.
What happened, byte by byte
More options
Paste the encrypted text, the key and the IV
If it doesn’t decrypt, the tool tries the usual mix-ups (hex or text key, IV in front, padding, tag length …) and tells you which one it was.
What happened, byte by byte
Start from an example
Each example fills in the tool above. The ones under “Common mistakes” don’t decrypt at first: watch the tool find out why.
Encrypt
Decrypt
Common mistakes
Key, IV, nonce and tag: what goes where
AES itself only scrambles 16-byte blocks with a key. Everything else on this page is about the parts around it, and that’s where two programs end up with different results.
| Part | Size | What it is |
|---|---|---|
| Key | 16, 24 or 32 bytes | Decides AES-128, AES-192 or AES-256. Secret. As hex it’s 32, 48 or 64 characters, as Base64 24, 32 or 44. A password is not a key: turn it into one with PBKDF2, scrypt or Argon2. |
| IV | 16 bytes | Makes the same text encrypt differently each time (CBC, CTR, CFB, OFB). Not secret, but new for every message, so it’s usually sent along, in front of the ciphertext. |
| Nonce | 12 bytes (GCM) | GCM’s IV. It must never repeat with the same key: two messages with the same nonce leak their XOR and let an attacker forge tags. |
| Tag | 16 bytes (12 to 16) | GCM’s checksum. Decrypting fails if the key, nonce, data or AAD changed. Most libraries put it at the end of the ciphertext; Node.js and PHP hand it out separately. |
| AAD | any, optional | Additional authenticated data in GCM: a header or record ID that isn’t encrypted but is covered by the tag. Both sides must use the same. |
| Padding | 1 to 16 bytes | CBC and ECB work on whole blocks. PKCS#7 fills up with bytes that each hold the number of bytes added: 5 missing bytes become 05 05 05 05 05. A full block of 10 is added if the text already fits. |
| Salt | 8 bytes | Only with a passphrase (openssl enc, CryptoJS): stored after “Salted__” so the same passphrase gives a new key every time. |
The usual layout of a GCM message
This is how Java, Go, Web Crypto and Python’s AESGCM hand it over, nonce first. The example above (“GCM from Java or Web Crypto”) is exactly this. CBC messages are usually IV + ciphertext.
Which mode should I use?
| Mode | How it works | Padding | Use it? |
|---|---|---|---|
| GCM | Counter mode plus a tag that detects any change. | none | Yes, for anything new. Never reuse a nonce with the same key. |
| CBC | Each block is mixed with the one before. | PKCS#7 | For existing systems. Add an HMAC: without one, changed messages go unnoticed and padding oracles are possible. |
| CTR | Encrypts a counter and XORs it with the text. | none | With an HMAC, or use GCM, which is CTR plus a tag. |
| CFB, CFB8, OFB | Older ways to turn AES into a stream. | none | Only to read old data. |
| ECB | Every block on its own: same block in, same block out. | PKCS#7 | No. Patterns stay visible, as the ECB picture shows. Try the ECB example: both halves come out the same. |
Why your result doesn’t match
Two AES implementations with the same key and text give the same result. When they don’t, it’s one of these, roughly from most to least common. The Decrypt tab checks most of them for you.
a3f1c2… with 64 characters is 32 bytes in hex. Read as text it’s 64 bytes, which AES refuses, or a different 32 bytes if a library cuts it. Same for Base64.
A random IV gives a different result every time, which is correct. To compare, both sides need the same IV. In CBC a wrong IV only garbles the first 16 bytes; the rest decrypts fine.
Many programs put the IV (or GCM nonce) in front of the ciphertext and never show it separately. Tick “The IV is at the start of the encrypted data”.
The same bytes look completely different. Decrypting hex as Base64, or the other way round, gives noise or “not whole blocks”.
PKCS#7 is the standard. Old PHP mcrypt code and some C libraries pad with zero bytes, and some use none. In PHP, OPENSSL_ZERO_PADDING doesn’t add zeros: it turns padding off.
GCM output is ciphertext + tag in Java, Go, Web Crypto and Python’s AESGCM. Node.js and PHP give the tag separately, and you have to append it or pass it in.
96-bit (12-byte) tags exist. If the tag length differs, the tag check fails even with the right key.
Data starting with U2FsdGVkX1 comes from openssl enc or CryptoJS.AES.encrypt(text, "passphrase"). The key and IV are derived from the passphrase and a salt. Choose “Passphrase” as the key format.
PHP’s openssl_encrypt fills a short key with zero bytes and cuts a long one, without an error. The aes-256-cbc page on this site does the same, with a zero IV. Other languages refuse such keys.
echo "text" adds a newline. Use printf '%s' or echo -n, or the result differs in the last block.
The same in Python, Node.js, PHP, Java, Go and more
These examples use the key, IV and settings from the tool above and update as you change them. Each one prints exactly what the tool shows. 552 of them, in 8 languages and many combinations of mode, padding and tag, were run against the tool before this page went online.
# pip install cryptography
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import base64
key = bytes.fromhex("a3f1c2d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90")
nonce = bytes.fromhex("cafebabefacedbaddecaf888")
data = "Meet me at the station at 9.".encode()
enc = Cipher(algorithms.AES(key), modes.GCM(nonce)).encryptor()
ct = enc.update(data) + enc.finalize()
tag = enc.tag
print(base64.b64encode(nonce + ct + tag).decode())import crypto from "node:crypto";
const key = Buffer.from("a3f1c2d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90", "hex");
const nonce = Buffer.from("cafebabefacedbaddecaf888", "hex");
const data = Buffer.from("Meet me at the station at 9.", "utf8");
const cipher = crypto.createCipheriv("aes-256-gcm", key, nonce);
const ct = Buffer.concat([cipher.update(data), cipher.final()]);
console.log(Buffer.concat([nonce, ct, cipher.getAuthTag()]).toString("base64"));$key = hex2bin('a3f1c2d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90');
$nonce = hex2bin('cafebabefacedbaddecaf888');
$data = 'Meet me at the station at 9.';
$ct = openssl_encrypt($data, 'aes-256-gcm', $key, OPENSSL_RAW_DATA, $nonce, $tag, '');
echo base64_encode($nonce . $ct . $tag);import javax.crypto.Cipher;
import javax.crypto.spec.*;
import java.util.*;
import java.nio.charset.StandardCharsets;
public class Aes {
public static void main(String[] args) throws Exception {
byte[] key = HexFormat.of().parseHex("a3f1c2d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90");
byte[] nonce = HexFormat.of().parseHex("cafebabefacedbaddecaf888");
byte[] data = "Meet me at the station at 9.".getBytes(StandardCharsets.UTF_8);
Cipher c = Cipher.getInstance("AES/GCM/NoPadding");
c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(128, nonce));
byte[] out = c.doFinal(data); // ciphertext + tag
byte[] all = new byte[nonce.length + out.length];
System.arraycopy(nonce, 0, all, 0, nonce.length);
System.arraycopy(out, 0, all, nonce.length, out.length);
out = all;
System.out.println(Base64.getEncoder().encodeToString(out));
}
}package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/hex"
"fmt"
)
func main() {
key, _ := hex.DecodeString("a3f1c2d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90")
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
nonce, _ := hex.DecodeString("cafebabefacedbaddecaf888")
data := []byte("Meet me at the station at 9.")
aead, err := cipher.NewGCM(block)
if err != nil {
panic(err)
}
out := aead.Seal(nil, nonce, data, nil) // ciphertext + tag
out = append(nonce, out...)
fmt.Println(base64.StdEncoding.EncodeToString(out))
}const hex = (s) => Uint8Array.from(s.match(/../g) ?? [], (b) => parseInt(b, 16));
const toB64 = (b) => btoa(String.fromCharCode(...b));
const key = await crypto.subtle.importKey("raw", hex("a3f1c2d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c6d7e8f90"), "AES-GCM", false, ["encrypt"]);
const nonce = hex("cafebabefacedbaddecaf888");
const data = new TextEncoder().encode("Meet me at the station at 9.");
const ct = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv: nonce }, key, data)); // ciphertext + tag
console.log(toB64(new Uint8Array([...nonce, ...ct])));Runs in the browser console or any module (top-level await).
CryptoJS has no GCM.
“openssl enc” doesn’t support GCM or other authenticated modes. Use one of the other examples.
What the error message means
The same mistake gives a different message in every language. These are the real messages, triggered on purpose in each library.
Wrong key, IV or padding (CBC, ECB)
- Python
ValueError: Invalid padding bytes. - Node.js, PHP
error:1C800064:Provider routines::bad decrypt - Java
javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption. - OpenSSL
bad decrypt - CryptoJS
Error: Malformed UTF-8 data (or an empty string)
The key or the IV is not the one used for encrypting, or the padding differs (PKCS#7, zero bytes, none). With a wrong key, the last block decrypts to noise, and noise is almost never valid padding. Paste it into Decrypt above: the tool tries the usual mix-ups.
Tag doesn’t match (GCM)
- Python
cryptography.exceptions.InvalidTag - Node.js
Error: Unsupported state or unable to authenticate data - Java
javax.crypto.AEADBadTagException: Tag mismatch - Go
cipher: message authentication failed - PHP
openssl_decrypt() returns false - Browser
OperationError: The operation failed for an operation-specific reason
Key, nonce, additional data (AAD), tag length or the data itself differ. GCM refuses to show anything then, which is the point. If the tool shows readable text but a wrong tag, key and nonce are right and the AAD or tag length is the problem.
Key has the wrong length
- Python
ValueError: Invalid key size (72) for AES. - Node.js
RangeError: Invalid key length - Java
java.security.InvalidKeyException: Invalid AES key length: 9 bytes - Go
crypto/aes: invalid key size 9 - OpenSSL
hex string is too short, padding with zero bytes to length
AES keys are exactly 16, 24 or 32 bytes. A 64-character key is usually 32 bytes in hex, not 64 bytes of text. PHP and OpenSSL fill short keys with zero bytes without failing, which is why a PHP result sometimes can’t be reproduced elsewhere.
IV has the wrong length
- Python
ValueError: Invalid IV size (8) for CBC. - Node.js
TypeError: Invalid initialization vector - Java
java.security.InvalidAlgorithmParameterException: Wrong IV length: must be 16 bytes long - PHP
openssl_encrypt(): IV passed is only 8 bytes long, cipher expects an IV of precisely 16 bytes, padding with \0
CBC, CTR, CFB and OFB need a 16-byte IV, GCM usually a 12-byte nonce. 32 hex characters are 16 bytes.
Data isn’t whole blocks
- Java
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher - Go
panic: crypto/cipher: input not full blocks
CBC and ECB always produce whole 16-byte blocks. If the data isn’t, it’s cut off, still has an IV or tag attached, or it’s Base64 read as hex (or the other way round).
Passphrase on raw data
- OpenSSL
bad magic number
openssl enc -pass expects data that starts with “Salted__” (Base64 “U2FsdGVkX1”). If yours doesn’t, it was encrypted with a raw key and IV: use -K and -iv instead.
Is it safe?
Tested against OpenSSL and NIST
The page uses the same AES code as the 174 cipher pages on this site, which match PHP’s openssl_encrypt byte for byte. GCM comes from noble-ciphers, a widely used JavaScript library without dependencies. Checked on 3 December 2026:
- 1404 automated checks on every change: NIST test vectors (SP 800-38A for ECB, CBC, CFB, CFB8, OFB and CTR; the GCM test cases), 261 random cases from Python’s cryptography library (all modes, key sizes, paddings, nonce lengths from 8 to 60 bytes, shortened tags, AAD), wrong keys, tags and AAD, and 55 passphrase messages made by
openssl encwith MD5, SHA-256 and PBKDF2. - 552 code examples in Python, Node.js, PHP, Java, Go, Web Crypto, CryptoJS and the OpenSSL command line, run for real, each printing exactly what the tool shows.
- The mix-up finder is tested on its own: hex key read as text, IV in front, Base64 vs hex, ECB vs CBC, zero padding, 96-bit tags, AAD, CryptoJS passphrases, data from this site’s cipher pages. On random data it must find nothing, and does.
- What it can’t do: GCM nonces shorter than 8 bytes (NIST advises against them), and CCM, OCB, XTS or SIV. For those, the cipher pages cover what PHP and OpenSSL do.
Questions about AES encryption and decryption
How do I decrypt AES without the key?
You can’t. AES has no known shortcut: without the exact key, the only way is guessing, and a random 128-bit key can’t be guessed. If the key came from a weak password, that password can sometimes be guessed, but that’s an attack on the password, not on AES.
Why does the same text give a different result each time?
Because the IV or nonce is new each time, and that’s how it should be. Encrypting the same text twice to the same result would tell an eavesdropper that the messages are equal. To get a fixed result for testing, use a fixed IV.
Do I need to keep the IV secret?
No. The IV and the GCM nonce can be public; they’re usually stored or sent in front of the ciphertext. They must be unpredictable (CBC) or never repeat with the same key (GCM, CTR). Only the key is secret.
What’s the difference between AES-128 and AES-256?
The key length: 16 or 32 bytes. Both are secure today. AES-256 takes 14 rounds instead of 10, is a little slower and has a larger safety margin, also against future quantum computers. Here, the key you enter decides which one is used.
Where is the GCM tag?
Usually the last 16 bytes of the ciphertext: Java, Go, Web Crypto and Python’s AESGCM put it there. Node.js (getAuthTag) and PHP ($tag) return it separately. Choose “Where is the tag?” under More options to match.
Can I decrypt CryptoJS or openssl enc output here?
Yes. Data that starts with U2FsdGVkX1 (“Salted__”) was encrypted with a passphrase. Choose “Passphrase” as the key format and the key derivation: MD5 for CryptoJS and older OpenSSL, SHA-256 for openssl enc since version 1.1, PBKDF2 for openssl enc -pbkdf2. If you don’t know, just try decrypting: the tool finds it.