ALICEEVEBOB1msg = b"pay 50"2key = os.urandom(32)3aes = AESGCM(key)4nonce = os.urandom(12)5ct = aes.encrypt(nonce, msg, None)6pt = aes.decrypt(nonce, ct, None)msgkeynoncectptpay 50!
Back row: Alice, Bob, their copies of the shared key, the network and EveFront row: the Python program and its memory boxesTokens: plaintext, key, nonce, locked ciphertext, a tampered ciphertextAt the back, Alice and Bob each hold a copy of the same secret key; the network runs between them and Eve listens beside it. In front, a six-line Python program that encrypts "pay 50" with AES-GCM, and the memory boxes it fills. The key turns the plaintext into a locked 22-byte ciphertext, Eve copies only noise, Bob's copy of the key turns it back, and a changed byte is rejected. GCM uses AES to make a stream of key-dependent bytes and combines it with the message, which is why the ciphertext has the same length as the plaintext. "Scrambles" is the plain-words version of that.

One shared key

  1. Encryption scrambles data so only someone with the right key can read it. In symmetric encryption the same secret key does both jobs: it locks (encrypts) and unlocks (decrypts). Alice and Bob each hold a copy of one key.

Lock it in Python

Send and unlock

Mistakes and fixes

What happens

;; step 1/10 · shared-key
encryption   plaintext + key  →  ciphertext
decryption   ciphertext + key →  plaintext
symmetric    the SAME key does both
Alice        holds a copy of the key
Bob          holds a copy of the same key
goal         confidentiality (only key holders can read)

The example runs in one Python program so you can try it. In real life Alice and Bob run separate programs; Bob builds his own AESGCM(key) from his copy of the key. AESGCM comes from the widely used cryptography package (pip install cryptography), not the standard library. os.urandom is in the standard library and is meant for cryptographic use.