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
- 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.
- 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.
- 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
- 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.
- 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.
- 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
- 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.
- 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.
- 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
- 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.
- 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.
- 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.