Symmetric Encryption
Chapters and transcript
Symmetric Encryption Look around this office. The laptop disk is encrypted. The Wi-Fi traffic is encrypted. And almost every HTTPS connection to a server is encrypted. All of them use symmetric encryption, the workhorse of cryptography. In this lesson, you will learn how it works, what AES is, and the mistakes that make strong encryption weak.
One shared key In symmetric encryption, the same key is used to encrypt and to decrypt. Think of a normal door lock. The key that locks the door also unlocks it. Anyone who has a copy of the key can open the door.
One shared key Here, Ayesha has key K. She encrypts Meeting at three, and sends the ciphertext to Bilal. An attacker captures it, but without K he sees only random-looking data. Bilal has a copy of the same key, so he decrypts it and reads Meeting at three.
Why it is used Symmetric encryption has one big advantage. It is fast. Many chips even have special built-in instructions just for AES. That is why it protects bulk data, such as whole disks, video calls and large file transfers.
AES The most widely used symmetric algorithm is AES, the Advanced Encryption Standard. It was chosen by NIST in two thousand and one, after an open, international competition. AES is a block cipher. It encrypts data in blocks of one hundred twenty-eight bits. It supports three key sizes: one hundred twenty-eight, one ninety-two and two hundred fifty-six bits. With a properly chosen random key, no practical attack is known.
AES You may also see ChaCha20. It is a stream cipher. It is fast on phones and small devices without AES hardware, and it is an option in modern TLS. But older ciphers such as DES and RC4 should no longer be used. DES has a key far too short for today, and RC4 has known weaknesses. If you see them in settings, treat that as a warning sign.
The key sharing problem Now the big weakness. Both sides need the same secret key, so how do they get it safely? Suppose Ayesha sends the key to Bilal by email, in plain form. An attacker who can read that channel gets the key too. Later, the ciphertext arrives, and the attacker reads it easily. This is the key sharing problem.
The key sharing problem It gets worse as more people join. If every pair needs its own key, two people need one key. Ten people need forty-five keys. And one hundred people need four thousand, nine hundred and fifty keys. The formula is n times n minus one, divided by two. Managing and replacing thousands of keys is very difficult.
Hybrid encryption For a long time, the only answer was to meet in person or use a trusted courier. Today, systems combine two types. Public-key methods agree on a fresh symmetric key safely. Then fast symmetric encryption protects the actual data. This is called hybrid encryption, and HTTPS works this way. You will learn public-key cryptography in the next lesson.
Modes of operation AES on its own encrypts only one sixteen-byte block. A mode of operation is the set of rules for longer data, and the mode matters as much as the algorithm. ECB encrypts each block on its own. Do not use it. CBC is older and still found, but needs extra integrity protection. CTR is fine for secrecy, with no integrity on its own. GCM adds an integrity check, and is recommended.
Why ECB is dangerous Why is ECB dangerous? In ECB, the same plaintext block always gives the same ciphertext block. So patterns survive. Encrypt a simple picture in ECB mode, and the outline is still visible. In a salary database, two employees with the same salary get identical encrypted fields. A strong algorithm in a weak mode is weak.
Authenticated encryption Authenticated encryption, such as AES-GCM, gives secrecy and integrity. The sender attaches a short authentication tag to the ciphertext. An attacker cannot read it, but he changes one bit on the way. The receiver checks the tag, it fails, and the data is rejected. One more rule: each message uses a nonce, a value used once. Never reuse a nonce with the same key.
Quick check Quick check. You must send a scanned CNIC to your manager in an encrypted archive. How should you share the password? Think, then choose.
Using it safely Here is how it looks in practice. A colleague asks for the salary sheet. You send it as an AES-256 encrypted archive by email. When they ask for the password, you call them instead of typing it. Use a long password, because the password is turned into the key. A weak password means a weak key.
Using it safely Developers must create keys with a cryptographically secure random number generator, never with a normal random function or a timestamp. Try this OpenSSL command. It creates a random two hundred fifty-six bit key, shown as sixty-four hexadecimal characters. Run it twice. The output is completely different each time. That unpredictability is exactly what a good key needs.
Key takeaways Let us put it together. Symmetric encryption uses one key to lock and unlock. Use AES or ChaCha20, not DES or RC4. Prefer authenticated modes such as AES-GCM. Never use ECB, and never reuse a nonce. Turn on full-disk encryption, such as BitLocker or FileVault. And never hardcode keys. Keep them in a secrets manager.
Symmetric encryption is the workhorse of cryptography. It protects your phone storage, your Wi-Fi traffic, your laptop disk and almost every HTTPS connection. In this lesson you will learn how it works, what AES is, and the mistakes that make strong encryption weak.
One key for locking and unlocking
In symmetric encryption, the same key is used to encrypt and to decrypt. Think of a normal door lock: the key that locks the door also unlocks it. Anyone who has a copy of the key can open the door.
Ayesha: "Meeting at 3" + key K --[ encrypt ]--> 8f3a91c0d2...
Bilal: 8f3a91c0d2... + key K --[ decrypt ]--> "Meeting at 3"
Both Ayesha and Bilal must have the same key K. An attacker who captures the ciphertext but does not have K sees only random-looking data.
Symmetric encryption has one big advantage: it is fast. Modern processors can encrypt very large amounts of data per second, and many chips have special built-in instructions just for AES. That is why symmetric encryption is used for bulk data, such as whole disks, video calls and large file transfers.
AES: the world's standard cipher
The most widely used symmetric algorithm is the Advanced Encryption Standard (AES). It was selected by the United States National Institute of Standards and Technology (NIST) in 2001, after an open, international competition. The winning design was created by two Belgian cryptographers and was originally called Rijndael. Today AES is used all over the world, by governments, banks and ordinary apps.
Key facts about AES:
- It is a block cipher. It encrypts data in fixed-size chunks, called blocks, of 128 bits (16 bytes) at a time.
- It supports three key sizes: 128, 192 and 256 bits. You will see names like AES-128 and AES-256.
- With a properly chosen random key, no practical attack is known that can recover the key or read the data. Both AES-128 and AES-256 are considered strong.
Another modern symmetric cipher you may see is ChaCha20. It is a stream cipher, which means it produces a long stream of random-looking bits that is mixed with the data. It is fast on phones and small devices that do not have AES hardware, and it is one of the options in modern TLS.
Older ciphers such as DES and RC4 should no longer be used. DES has a key that is far too short by today's standards, and RC4 has known weaknesses. If you see them in a system's settings, treat that as a warning sign.
The key sharing problem
Symmetric encryption has one big weakness. Both sides need the same secret key, so how do they get it safely?
If Ayesha sends the key to Bilal by email or WhatsApp in plain form, an attacker who can read that channel gets the key too. Then the encryption is useless. This is called the key distribution problem or key sharing problem.
The problem gets worse as more people join. If every pair of people needs its own private key, the number of keys grows quickly:
| People in the group | Keys needed |
|---|---|
| 2 | 1 |
| 10 | 45 |
| 100 | 4,950 |
The formula is n x (n - 1) / 2, where n is the number of people. Managing thousands of keys, and replacing them when staff leave, becomes very difficult.
For a long time, the only answer was to meet in person or use a trusted courier. Today, the problem is mostly solved by public-key cryptography, which you will learn about in the next lesson. In practice, systems combine both: public-key methods are used to agree on a fresh symmetric key safely, and then fast symmetric encryption protects the actual data. This combination is called hybrid encryption, and HTTPS works this way.
Modes of operation
AES on its own only knows how to encrypt one 16-byte block. Real messages are longer. A mode of operation is the set of rules for using a block cipher on data of any length. The mode matters as much as the algorithm.
You do not need the mathematics. Here is the idea behind the common modes:
| Mode | Basic idea | Verdict |
|---|---|---|
| ECB | Encrypts each block on its own | Do not use |
| CBC | Mixes each block with the previous ciphertext block; needs a random starting value | Older, still found; needs extra integrity protection |
| CTR | Turns AES into a stream cipher using a counter | Fine for secrecy, but no integrity on its own |
| GCM | Like CTR, plus an integrity check | Recommended for most uses |
Two more words you should know:
- IV (initialisation vector) or nonce: a value used once per message so that encrypting the same message twice gives different ciphertext. It does not need to be secret, but it must follow the mode's rules. In GCM, reusing a nonce with the same key is a serious mistake that can expose data and allow forgery.
- Authenticated encryption: a mode that gives both confidentiality and integrity. AES-GCM and ChaCha20-Poly1305 are common examples. They attach a short authentication tag to the ciphertext. If an attacker changes even one bit, the tag check fails and the data is rejected.
Without authentication, an attacker who cannot read the data may still be able to change it in useful ways. This is why modern systems prefer authenticated encryption.
Why ECB is dangerous
ECB (Electronic Codebook) mode is the simplest mode. It splits the data into blocks and encrypts each block separately with the same key. The problem is simple: the same plaintext block always produces the same ciphertext block.
That means patterns in the data survive encryption. A famous demonstration takes a simple picture, such as a cartoon with large areas of one colour, and encrypts it with AES in ECB mode. The colours change, but the outline of the picture is still clearly visible. The data is "encrypted", yet anyone can see what it shows.
The same problem affects records and files. Imagine a database of salaries encrypted in ECB mode. If two employees have exactly the same salary field, their encrypted fields are identical too. An attacker learns who earns the same amount without decrypting anything.
The lesson: a strong algorithm in a weak mode is weak. When you see "AES-256" in a product description, it is fair to ask, "Which mode?"
Using symmetric encryption safely
Most of the risk in symmetric encryption is not in AES itself. It is in how keys are created, stored and used.
For everyone:
- Turn on full-disk encryption on laptops (BitLocker on Windows, FileVault on Mac). A stolen laptop without it gives away every file.
- When you send a sensitive file, such as a scanned CNIC or a salary sheet, use an encrypted archive. For example, 7-Zip can create AES-256 encrypted archives. Send the password through a different channel, such as a phone call, not in the same email.
- Use a long password for encrypted files. The password is turned into the key, so a weak password means a weak key.
For developers:
- Use authenticated encryption (AES-GCM or ChaCha20-Poly1305) from a trusted library.
- Never use ECB. Never reuse a nonce with the same key.
- Generate keys with a cryptographically secure random number generator, never with a normal random function or a timestamp.
- Never hardcode keys in source code. Store them in a secrets manager or key management service.
Try it yourself. If you have OpenSSL installed, this command creates a random 256-bit key, shown as 64 hexadecimal characters:
openssl rand -hex 32
Run it twice and notice that the output is completely different each time. That unpredictability is exactly what a good key needs.
HANDS-ON LAB
Fix the Encryption Settings
A developer wants to ship weak encryption settings. Make the right call on each one.
Check your understanding
Answer every question correctly to complete this lesson. If you miss one, try the quiz again.
1. Ten people each need a private, separate symmetric key with every other person in the group. Why does this become a problem as the group grows?
2. An image is encrypted with AES in ECB mode, and the outline of the picture is still visible in the encrypted file. Why?
3. What extra protection does an authenticated mode such as AES-GCM give compared with older modes like CBC?
Locked: answer every quiz question above correctly to complete this lesson.
Spotted a mistake or something unclear? Report a mistake