Symmetric encryption
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Symmetric encryption uses one secret key to both lock and unlock data. Follow a six-line Python program that encrypts "pay 50" with AES-GCM: a random 256-bit key, a 96-bit nonce, and a 22-byte ciphertext that carries a 16-byte tag. Watch Eve copy only noise, see Bob's copy of the key turn it back, and see a single changed byte rejected. Finish with the two mistakes that break it most often, reused nonces and keys written into code, and how to avoid them.
One key locks and unlocks
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.
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)
اقرأ الدرس خطوة بخطوة
One key locks and unlocks
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.
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)Plaintext: the readable message
Alice wants to send Bob "pay 50". Readable data is called plaintext. The b in b"pay 50" makes it bytes, because ciphers work on bytes, not letters. Line 1 stores those six bytes in msg.
the whole program starts with two imports: import os from cryptography.hazmat.primitives.ciphers.aead import AESGCM line 1 msg = b"pay 50" msg 6 bytes: p a y (space) 5 0 plaintext data anyone could readMake a random 256-bit key
os.urandom(32) asks the operating system for 32 random bytes: a 256-bit key. AES, the standard cipher, accepts 128, 192 or 256-bit keys. The method is public; only the key is secret. AESGCM(key) builds a cipher that uses it.
line 2 key = os.urandom(32) 32 bytes × 8 = 256 bits, from the OS random source made for cryptography line 3 aes = AESGCM(key) AES FIPS 197: 128-bit blocks, keys of 128, 192 or 256 bits rule the method is public; the key is the secretA nonce: a number used once
Each message also needs a nonce, a number used once: 12 random bytes (96 bits), the size NIST recommends for this mode. The nonce is not secret and travels with the message, but it must never repeat under the same key.
line 4 nonce = os.urandom(12) nonce "number used once", 12 bytes = 96 bits NIST SP 800-38D: 96-bit IVs recommended for GCM secret? no, it is sent along with ct repeat? never, with the same keyEncrypt: lock it with the key
encrypt() runs AES in GCM mode. AES scrambles the bytes with the key; GCM adds a 16-byte tag, a check value only the key can make. So ct is the ciphertext: 6 scrambled bytes plus the tag, 22 bytes in all.
line 5 ct = aes.encrypt(nonce, msg, None) AES-GCM authenticated encryption (SP 800-38D) ct 6 bytes ciphertext (same length as msg) + 16 bytes tag (128-bit check value) = 22 bytes None no extra unencrypted data to protectSend it: Eve sees only noise
Alice sends the nonce and ct over the network. Eve, an eavesdropper, can copy everything that passes. Without the key the 22 bytes look random, so the message stays confidential.
sent nonce (12 B) + ct (22 B) Eve copies both Eve has no key Eve sees 22 bytes that look random result confidentiality keptDecrypt with the same key
Bob uses his copy of the same key and the nonce. decrypt() first checks the tag, then reverses the scrambling. Out comes b"pay 50": the original plaintext.
line 6 pt = aes.decrypt(nonce, ct, None) needs the same key + the same nonce step 1 check the 16-byte tag step 2 undo the scrambling pt b"pay 50" (6 bytes, equal to msg)Tampering is caught
Suppose Eve changes one byte of ct on the way, hoping to alter the amount. The tag no longer matches, so decrypt() raises InvalidTag and returns nothing. Authenticated encryption protects integrity as well as secrecy.
Eve changes 1 byte of ct in transit Bob pt = aes.decrypt(nonce, ct, None) tag does not match result cryptography.exceptions.InvalidTag pt is never set: no fake "pay 90" goal integrity (changes are detected)Mistake: reusing a nonce
A common mistake is reusing a nonce with the same key, for example a fixed value in the code. In GCM that can leak plaintext and lets attackers forge tags. The fix: a fresh random nonce for every message, as line 4 does.
mistake nonce = b"000000000000" (same every time) harm two messages under one key + nonce leak how they differ, and tags can be forged fix nonce = os.urandom(12) for EVERY message limit NIST: at most 2^32 random nonces per key, then change the keyMistake: a key in the code
Anyone with the key can read and forge messages. Never write keys into code or a repository; keep them in a key manager, and use a vetted library, not a homemade cipher. One problem remains: how does Bob get the key safely?
mistake key = b"..." written in the source code or committed to git fix load it from a key manager or vault fix vetted library + authenticated mode (GCM) never a homemade cipher, never ECB open how is the key shared safely? public-key cryptography answers that
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What makes an encryption scheme symmetric?
Symmetric means one shared secret key does both jobs. Alice and Bob each need a copy of it, and it must stay secret.
02AES-GCM encrypts the 6-byte message b"pay 50". How long is the result ct?
GCM keeps the ciphertext the same length as the plaintext (6 bytes) and appends a 16-byte tag: 22 bytes in all.
03Which statement about the 12-byte nonce is true?
The nonce is not secret and travels with the ciphertext. Reusing one with the same key breaks GCM's secrecy and its tag protection, so use a fresh random nonce for every message.
04Eve changes one byte of ct on its way to Bob. What happens when Bob calls aes.decrypt(nonce, ct, None)?
The tag no longer matches, so decryption fails with InvalidTag. Authenticated encryption detects changes: it protects integrity, not only confidentiality.
05Where should an application's encryption key live?
OWASP: do not hard-code keys in source code or check them into version control. Anyone who gets the key can read and forge every message.
المسرد
- Plaintext and ciphertext
- Plaintext is readable data. Ciphertext is the scrambled form that encryption produces; without the key it looks random.
- Symmetric key
- One secret key that both encrypts and decrypts. Everyone who should read the data needs a copy, and it must be kept secret.
- AES
- The Advanced Encryption Standard (NIST FIPS 197): a cipher that works on 128-bit blocks with 128, 192 or 256-bit keys.
- Nonce
- A "number used once": an extra value given to each encryption. It is not secret, but it must never repeat with the same key. AES-GCM uses 12 bytes (96 bits).
- Authenticated encryption (AES-GCM)
- Encryption that also adds a tag, a check value only the key can make. Decryption checks the tag first and refuses data that was changed.
- Tag
- A short value (16 bytes in AES-GCM) added to the ciphertext. If even one byte of the ciphertext changes, the tag no longer matches.
- Key manager
- A service or vault that stores keys away from code and data, controls who may use them, and supports changing (rotating) them.
الملاحظات والمصادر
- 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.
- 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.
- NIST SP 800-38D requires that a key and IV (nonce) pair is never used twice, and, with random 96-bit nonces, at most 2^32 encryptions per key. Busy systems change keys well before that.
- Symmetric encryption assumes both sides already share the key. Sharing it safely over an open network is a separate problem, solved with public-key cryptography.
- This course is about defence. Weaknesses are explained only at the level of the mistake and the fix, never as steps to attack a real system.
- NIST FIPS 197 (updated 2023): Advanced Encryption Standard (AES)
- NIST SP 800-38D: Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC
- NIST SP 800-57 Part 1 Rev. 5: Recommendation for Key Management
- NIST CSRC Glossary: symmetric key
- OWASP Cryptographic Storage Cheat Sheet
- OWASP Key Management Cheat Sheet
- OWASP Top 10:2021 A02 Cryptographic Failures
- CISA: Mobile Communications Best Practice Guidance
- Python documentation: os.urandom
- cryptography documentation: Authenticated encryption (AESGCM)
اختبار يتاح بعد الخطوة الأخيرة.