ENع
→Symmetric encryption
الدرس 07 · التشفير

Symmetric encryption

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 7 · تفاعلي
01 / 10

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)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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)
  2. 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 read
  3. Make 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 secret
  4. A 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 key
  5. Encrypt: 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 protect
  6. Send 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 kept
  7. Decrypt 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)
  8. 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)
  9. 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 key
  10. Mistake: 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

اختبار يتاح بعد الخطوة الأخيرة.