এনক্রিপশন ও সিক্রেট
TLS, AES-GCM, KMS envelope, key rotation আর secret management।
Start with the production incident, then the design decision that fixes it.
শুক্রবার রাতে চলো-র GitHub repo-তে একটা .env commit হয়ে গেল—DB password, payment gateway secret, JWT signing key। মাহিন ১০ মিনিটে key rotate করল, কিন্তু তার আগে স্ক্যানার ওই key দিয়ে staging DB-তে ঢুকে গেছে। একই সপ্তাহে একটা S3 bucket public পাওয়া গেল, যেখানে rider-দের invoice PDF আছে।
দুই ঘটনা দুই শিক্ষা: secret (password/key) আর data (invoice/PII) আলাদা করে protect করতে হয়, আর key rotate না করলে একবার leak মানে চিরকাল leak। Encryption মানে শুধু https নয়—in transit, at rest, আর key management তিনটাই লাগে। Password-কে encrypt নয়, hash করতে হয়; আর hash-ও plain SHA-256 নয়।
In transit। TLS 1.2+ সব external/internal traffic-এ; certificate rotation; HSTS। Service-to-service-এ mTLS হলে identity verification-ও পাওয়া যায়।
At rest। Disk/DB/object storage-এ AES-256-GCM; envelope encryption—data key দিয়ে data encrypt, data key-কে KMS master key দিয়ে encrypt। KMS key কখনো app-এ hardcode নয়; IAM role দিয়ে access।
Hashing বনাম encryption। Password irreversible hash (Argon2id/bcrypt) + per-user salt; encrypt করলে key leak-এ সব password পড়া যায়। Searchable data দরকার হলে encryption নয়, tokenization/format-preserving ভাবো।
Key rotation। প্রতি ৯০ দিন বা leak suspicion-এ; old key দিয়ে decrypt, new key দিয়ে re-encrypt; key version ciphertext-এ রাখো। Rotate না করলে এক key leak পুরো history খুলে দেয়।
Secrets management। Vault/AWS Secrets Manager/Doppler; env file repo-তে নয়; CI secret masked; runtime inject; access audit। .env.example-এ নমুনা মান, real value নয়।
Threat model। কে attacker—DB backup চুরি, insider, misconfigured bucket, log leak? প্রতিটা threat-এর জন্য control আলাদা। Encryption backup-কেও Cover করবে।
| ডেটা | পদ্ধতি | key কোথায় |
|---|---|---|
| Password | Argon2id hash + salt | কোথাও নয় |
| Card/API key | KMS envelope | KMS/HSM |
| Invoice PDF | AES-GCM at rest | KMS data key |
| Session token | random + hash in DB | server secret |
| TLS | certificate | CA/ACME |
Envelope encryption-এর ধারণা: data key random, master key দিয়ে wrap:
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os
data_key = AESGCM.generate_key(bit_length=256)
nonce = os.urandom(12)
ciphertext = AESGCM(data_key).encrypt(nonce, plaintext, associated_data=b"invoice")
# store nonce + ciphertext; wrap data_key with KMS master key
wrapped_key = kms.encrypt(KeyId=master_key_id, Plaintext=data_key)
# password hashing (example: argon2 CLI)
echo -n 'correct horse battery staple' | argon2 somesalt -id -t 3 -m 16 -p 4
# never log or commit the output/secret
associated_data (AAD) invoice id হলে ciphertext অন্য invoice-এ copy করলে decrypt fail করবে—context binding।
১. Data classify করো। Public, internal, PII, secret—প্রতিটা class-এর storage/encryption/access আলাদা।
২. Transit lock করো। TLS everywhere, internal service mTLS, HSTS; certificate expiry alert।
৩. At-rest key hierarchy। KMS master → per-object data key; ciphertext-এ key version; backup-ও encrypted।
৪. Secrets সরাও। Repo history scan, env vault, CI masked, runtime injection; leaked key rotate + audit।
৫. Rotation drill। পুরোনো key দিয়ে decrypt, নতুন key দিয়ে re-encrypt, dual-read window; rollback plan।
.env repo-তে commit। Secret vault/CI masked inject; leak হলে সাথে সাথে rotate।echo -n "hello" | sha256sum দুইবার চালালে same hash—এটা integrity, confidentiality নয়। এখন openssl rand -hex 32 দিয়ে key বানিয়ে openssl enc -aes-256-gcm দিয়ে একটা ফাইল encrypt/decrypt করো; key না থাকলে পড়া যায় না দেখো। Password-এর জন্য argon2/bcrypt ব্যবহার করো, AES নয়—কেন?
Log/error message-এ secret বা PII নয়; request body log করলে redact করো। KMS access IAM-এ সীমিত রাখো; app role শুধু দরকারি key পড়ুক। Backup restore drill-এ key availability-ও test করো—key ছাড়া encrypted backup অর্থহীন।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
plaintext-এর সাথে keystream XOR করলেই cipher, আবার একই key দিয়ে XOR করলে ফেরা যায়।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
DB password, JWT key, invoice PDF; এক key leak।
One timed attempt. Solve every problem to earn the module badge.
Final checkpoint
Ready to prove this module?
Ask questions, share an approach or leave a study tip for this lesson.
Was this lesson helpful?
Your reaction helps other learners judge these notes.
0 comments
Sign in to ask a question or share an approach.
Sign in to commentNo comments yet
Be the first to ask a question or leave a study tip.
| প্রশ্ন | উত্তর |
|---|
| Password? | Argon2id/bcrypt hash |
| Data at rest? | AES-256-GCM |
| Key? | KMS envelope, rotate |
| Transit? | TLS 1.2+/mTLS |
| Secret? | vault, not repo |
| Leak? | rotate + audit |