Online AES encrypt & decrypt

AES uses a 128-bit block. The key is 128, 192, or 256 bits, written as 32, 48, or 64 hex characters, and the same key encrypts and decrypts. The mode decides how blocks connect. CBC, CFB, CTR, and OFB need an IV that is not secret and is not stored inside the Base64 or hex ciphertext. GCM uses a 12-byte nonce and appends a 16-byte authentication tag, with no block padding. ECB uses no IV, so identical plaintext blocks stay identical. A passphrase is not a raw key: EVP_BytesToKey with MD5, one iteration, and an empty salt turns it into a 128-bit key. A string of exactly 32, 48, or 64 hex characters is the raw key instead.

What the key, the mode, and the IV each change in AES

AES is a block cipher with a 128-bit block. The key is 128, 192, or 256 bits, which is 16, 24, or 32 bytes, written as 32, 48, or 64 hex characters. A longer key does not change the block size. The same key encrypts and decrypts. A string of any other length, including a shorter hex string or a sentence, is a passphrase. That passphrase is stretched with EVP_BytesToKey using MD5, one iteration, and an empty salt, and the result is a 128-bit key. It is not a random-salt password header sitting in front of the ciphertext.

The mode decides how successive blocks depend on each other. CBC, CFB, CTR, and OFB need an IV. GCM needs a 12-byte nonce. ECB needs neither. The IV is not a second key and does not have to stay secret, but it must be new for each message under the same key. It is also not written into the Base64 or hex ciphertext. Losing it, or using a different one, does not yield the plaintext.

The ciphertext bytes have two spellings. Base64 and hex encode and decode name the same bytes. Switching the spelling is not a second encryption. CBC with PKCS7 grows the plaintext out to a multiple of 16 bytes. GCM does not pad. It appends a 16-byte authentication tag, so a wrong key, a wrong nonce, or a changed ciphertext byte all fail the same way.

How key length, IV, padding, and the tag line up

Why the key is 16, 24, or 32 bytes and a passphrase is not that key

  1. AES-128, AES-192, and AES-256 name the key length. The block stays 128 bits in all three. 32 hex characters are 16 bytes. 48 are 24. 64 are 32.
  2. A hex string of any other length is not a short key. It is read as a passphrase, then derived to 128 bits with MD5, one iteration, and an empty salt.
  3. The derived key is not the characters of the passphrase, and it is not PBKDF2. Two systems match only when both use this derivation, or both use the same raw key bytes.

Why the IV is required and why it is absent from the ciphertext

  1. CBC chains each block to the previous ciphertext block. The IV is the starting block. CTR and GCM turn the IV into a counter. Reusing it with the same key repeats or cancels the keystream.
  2. The IV for CBC, CFB, CTR, and OFB is 16 bytes, 32 hex characters. The GCM nonce is 12 bytes, 24 hex characters. ECB has no IV.
  3. The Base64 or hex result is only the ciphertext. For GCM that includes the 16-byte tag at the end. The IV stays beside it. An IV typed as hex is read as hex. Other text is read as UTF-8 bytes. Either form is truncated or zero-padded to the length the mode needs.

What PKCS7 adds, and what the GCM tag adds instead

  1. PKCS7 fills the last block. If the padding length is N, each of those N bytes holds the value N. A full extra block is added when the plaintext already ends on a block boundary.
  2. No padding means the plaintext byte length is already a multiple of 16. Zero padding fills with zero bytes and cannot tell a real trailing zero from the fill.
  3. GCM does not use block padding. The ciphertext is the plaintext length plus a 16-byte tag. CFB, CTR, and OFB are stream modes, but a selected padding is still applied on those modes. Only GCM ignores it.

Why one mismatched parameter makes a valid-looking ciphertext fail

  1. Mode, padding, key, IV, and the byte spelling all have to match the encryption. A CBC ciphertext read as GCM, or Base64 read as hex, is a different byte string.
  2. PKCS7 that does not end with a legal pad, or a GCM tag that does not verify, both reject the message. The failure does not say which of the key, the IV, or the ciphertext was wrong.
  3. ECB needs no IV, and identical 16-byte plaintext blocks stay identical in the ciphertext. Repeated structure in the plaintext remains visible. The other modes hide that only when the IV is unique per message.

Where a matching AES message still fails to open

A ciphertext copied without the IV that was used beside it

The Base64 or hex string is the ciphertext alone. For GCM it also carries the tag. The 16-byte IV, or the 12-byte GCM nonce, was generated or typed separately and is not prefixed to those bytes. Decrypting with a new random IV uses a different starting state. CBC then yields the wrong first block or a padding error. GCM fails the tag. The bytes of the message did not change. The missing IV did.

A passphrase and a 64-character hex string that look equally like a key

Sixty-four hex characters are a raw AES-256 key. A sentence, or a hex string of 16 or 40 characters, is a passphrase and becomes a 128-bit key through MD5, one iteration, and an empty salt. Encrypting with one reading and decrypting with the other uses two different keys. The ciphertext can be well formed and still not open. The derivation is not a Salted header and not PBKDF2.

The same ciphertext bytes written once as Base64 and once as hex

Base64 and hex are two writings of one byte string. A CBC message in Base64 is not a different encryption from the hex of those bytes. Base64 groups six bits. Hex encode and decode writes two digits per byte. Reading a Base64 string as hex, or the reverse, parses different bytes, so the padding or the GCM tag cannot verify.

Common questions

What is AES encryption?
AES is a symmetric block cipher. The same secret key encrypts and decrypts, and each block is 128 bits no matter which key length is used. The mode, the IV, and the padding decide how those blocks are chained and how the final partial block is stored. The key is not inside the ciphertext. A person who does not have the key and the IV cannot read the plaintext back from the Base64 or hex spelling alone.
What is the difference between AES-128, AES-192, and AES-256?
The numbers are the key length in bits: 16, 24, and 32 bytes. In hex that is 32, 48, and 64 characters. The block size stays 128 bits for all three, so the ciphertext does not grow because the key grew. A 256-bit key is not a 256-bit block. A hex string that is not one of those three lengths is not a smaller AES key. It is treated as a passphrase and derived down to 128 bits.
What is the difference between CBC and GCM?
CBC chains each plaintext block with the previous ciphertext block, starting from a 16-byte IV, and usually finishes with PKCS7 padding. It does not check that the ciphertext is intact. GCM uses a 12-byte nonce as a counter and appends a 16-byte authentication tag. A changed bit, a wrong key, or a wrong nonce fails the tag. GCM does not use PKCS7. The tag is the extra length, not padding.
Does the IV have to be secret?
No. The IV, or the GCM nonce, can travel next to the ciphertext. It must not be reused with the same key. A repeated CTR or GCM nonce under one key repeats the keystream, and a repeated CBC IV leaks the equality of the first blocks. Secret material is the key. The IV is a per-message starting value, which is why it is stored beside the ciphertext instead of being mixed into the key.
Why is the IV not inside the Base64 ciphertext?
The Base64 or hex string is the ciphertext bytes. CBC, CFB, CTR, and OFB leave the 16-byte IV outside that string. GCM leaves the 12-byte nonce outside and puts only the tag at the end of the ciphertext. An empty IV at encryption time is replaced by a new random value, and that value has to be kept. Decrypting without it, or with a different one, does not recover the plaintext.
What is PKCS7 padding?
AES works on 16-byte blocks. PKCS7 extends the last block so the length becomes a multiple of 16. If four bytes are missing, it appends four bytes each equal to 4. If the plaintext already fills a block, it appends a whole extra block of bytes equal to 16. On decrypt, those bytes are removed. A final byte that is not a legal pad length means the key, the IV, the mode, or the ciphertext does not match.
Why does AES decryption fail when the ciphertext looks intact?
A readable Base64 or hex string only means the bytes could be parsed. Opening them still needs the same mode, the same padding, the same key, and the same IV. GCM also checks the tag, so corruption and a wrong key look the same. A passphrase derived with MD5 and an empty salt is a different key from the raw hex of those same characters. ECB has no IV, but every other mode fails closed when the IV is missing or different.
Why does ECB show patterns from the plaintext?
ECB encrypts each 16-byte block on its own. There is no IV and no chaining, so two equal plaintext blocks become two equal ciphertext blocks. Repeated headers, aligned zeros, or images with flat areas stay visible as repetition. CBC and the stream modes hide that equality only when each message has its own IV. ECB does not have that input, so the pattern is a property of the mode.