Simmetrik Shifrlash
AES, DES, 3DES — bitta kalit bilan ham shifrlash, ham deshifrlash. Blok va oqim shifrlar, ECB/CBC/CTR/GCM rejimlari, va AES ning ichki ishlash mexanizmi.
1 Simmetrik Shifrlash Nima?
2 Blok vs Oqim Shifrlar
Blok Shifrlar (Block Ciphers)
Ma'lumotni qat'iy o'lchamdagi bloklarga bo'lib shifrlaydi (64-bit, 128-bit).
Oqim Shifrlar (Stream Ciphers)
Ma'lumotni bit-bit yoki bayt-bayt shifrlaydi — XOR bilan pseudorandom kalit oqimi.
// Stream cipher asosi:
ciphertext[i] = plaintext[i] XOR keystream[i]
3 DES va Feistel Tarmog'i
DES Xususiyatlari
- 64-bit blok o'lchami
- 56-bit samarali kalit uzunligi (8 bit paritet)
- 16 ta Feistel turi
- 1999-yilda EFF "Deep Crack" 22 soatda buzdi
- Bugun ishlatish mumkin emas — brute force imkoni bor
3DES (Triple DES)
C = DES_K3( DES⁻¹_K2( DES_K1(P) ) )
- DES ni 3 marta qo'llaydi (Encrypt-Decrypt-Encrypt)
- 112-bit samarali xavfsizlik
- AES dan 3x sekin
- 2023-yildan NIST tomonidan deprecated
4 AES (Advanced Encryption Standard)
AES — 2001-yilda NIST tomonidan standart sifatida qabul qilingan, Rijndael algoritmi asosida. Bugun dunyodagi eng ko'p ishlatiladigan simmetrik shifrlash algoritmi: HTTPS, WiFi, disk shifrlash, VPN, va boshqa.
AES Turlar Soni
| Versiya | Kalit | Turlar | Xavfsizlik |
|---|---|---|---|
| AES-128 | 128 bit | 10 | Yetarli |
| AES-192 | 192 bit | 12 | Yaxshi |
| AES-256 | 256 bit | 14 | Eng yaxshi |
AES S-Box nima?
SubBytes bosqichida har bir bayt S-Box (Substitution Box) jadvalidan o'tkaziladi. Bu non-linear — kriptografik kuch beradi.
// S-Box misol (hex)
0x00 → 0x63
0x01 → 0x7c
0xff → 0x16
256 ta qiymat, GF(2⁸) inversiyasi + affine transform
5 Shifrlash Rejimlari (Modes of Operation)
| Rejim | IV kerak? | Parallel? | Xato tarqalish | Xavfsizlik | Ishlatish |
|---|---|---|---|---|---|
| ECB | Yo'q | Ha | Yo'q | ZAIF | HECH QACHON ishlatmang |
| CBC | Ha (tasodifiy) | Shifr emas | 2 blok | O'RTACHA | TLS 1.2 (eski) |
| CTR | Ha (nonce) | Ha | Tarqalmaydi | YAXSHI | Stream-like |
| GCM | Ha (nonce, 96-bit) | Ha | Auth tag | ENG YAXSHI | TLS 1.3, HTTPS |
6 Python bilan AES
Muhim: Hech qachon ECB rejimini ishlatmang. GCM rejimini ishlatganingizda nonce bir marta ishlatilsin — aks holda xavfsizlik buziladi.
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
import base64
# ============================================
# AES-CBC (to'g'ri ishlatish)
# ============================================
def aes_cbc_encrypt(plaintext: bytes, key: bytes) -> tuple:
iv = get_random_bytes(16) # Tasodifiy IV, har safar yangi!
cipher = AES.new(key, AES.MODE_CBC, iv)
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))
return iv, ciphertext
def aes_cbc_decrypt(iv: bytes, ciphertext: bytes, key: bytes) -> bytes:
cipher = AES.new(key, AES.MODE_CBC, iv)
return unpad(cipher.decrypt(ciphertext), AES.block_size)
# ============================================
# AES-GCM (eng yaxshi tanlov — autentifikatsiya bilan)
# ============================================
def aes_gcm_encrypt(plaintext: bytes, key: bytes, aad: bytes = None) -> dict:
nonce = get_random_bytes(12) # 96-bit nonce, faqat bir marta!
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce)
if aad:
cipher.update(aad) # Additional Authenticated Data
ciphertext, tag = cipher.encrypt_and_digest(plaintext)
return {'nonce': nonce, 'ciphertext': ciphertext, 'tag': tag}
def aes_gcm_decrypt(data: dict, key: bytes, aad: bytes = None) -> bytes:
cipher = AES.new(key, AES.MODE_GCM, nonce=data['nonce'])
if aad:
cipher.update(aad)
return cipher.decrypt_and_verify(data['ciphertext'], data['tag'])
# ValueError tashlaydi agar tag noto'g'ri bo'lsa (tampered!)
# Misol
key = get_random_bytes(32) # AES-256
msg = b"CTF{s3cr3t_m3ss4g3}"
# CBC
iv, enc = aes_cbc_encrypt(msg, key)
dec = aes_cbc_decrypt(iv, enc, key)
print(f"CBC: {dec}") # b"CTF{s3cr3t_m3ss4g3}"
# GCM
enc_data = aes_gcm_encrypt(msg, key, aad=b"header")
print(aes_gcm_decrypt(enc_data, key, aad=b"header"))
# ============================================
# CTF: ECB Oracle hujumi (Byte-at-a-time)
# ============================================
def detect_ecb(ciphertext: bytes, block_size: int = 16) -> bool:
blocks = [ciphertext[i:i+block_size] for i in range(0, len(ciphertext), block_size)]
return len(blocks) != len(set(blocks)) # Takrorlanuvchi blok = ECB
7 CTF Simmetrik Shifr Masalalari
Keng tarqalgan CTF naqshlari
ECB mode detection
Bir xil plaintext bloklar bir xil ciphertext bloklar hosil qiladi. Takrorlanuvchi blok topish = ECB.
CBC Bit-Flipping Attack
IV yoki oldingi ciphertext blokini o'zgartirish keyingi plaintext blokiga ta'sir qiladi.
Padding Oracle
Server padding xatosini ko'rsatsa — CBC deshifrlanadi. POODLE (2014) shu zaiflikdan foydalangan.
CTR Nonce Reuse
Bir nonce bilan ikki xil matn shifrlansa: C1⊕C2 = P1⊕P2. XOR cracking imkoni.
CBC Bit-Flipping Misol
# Maqsad: admin=true o'rnatish
# Plaintext: "role=user;admin=false"
def cbc_bitflip(ciphertext, pos, old, new):
# pos = o'zgartiriladigan bayt (oldingi blokda)
c = bytearray(ciphertext)
c[pos] ^= ord(old) ^ ord(new)
return bytes(c)
# "false" → "true;" qilish uchun
# 16-bayt blok hisoblab, oldingi blokda
# mos pozitsiyani o'zgartirish
# Himoya: CBC + MAC (Encrypt-then-MAC)
# Yoki AES-GCM (autentifikatsiya bor)
Bilimni Sinash
1. ECB rejimining asosiy zaifligini nima?
2. AES-256 uchun nechta shifrlash turi (round) mavjud?
3. Qaysi AES rejimi autentifikatsiya (integrity check) ham ta'minlaydi?