Beginner · Systems and cryptography · topic 14 of 43 · 35 min
Symmetric and asymmetric encryption
Explanation
Why encryption
Data travels over networks you do not control and sits on disks that can be stolen. Encryption turns readable data (plaintext) into scrambled data (ciphertext) using a key. Without the key, the ciphertext is useless. This protects confidentiality, both in transit (moving over a network) and at rest (stored on a disk).
An old idea: the Caesar cipher
The Caesar cipher shifts every letter by a fixed number. With a shift of 3, A becomes D and B becomes E. The key is the number 3. It is easy to understand and trivially easy to break: there are only 25 possible shifts, so an attacker tries them all. It is useful only to learn the words key, plaintext and ciphertext.
def caesar(text, shift):
out = ""
for ch in text:
if ch.isupper():
out += chr((ord(ch) - 65 + shift) % 26 + 65)
elif ch.islower():
out += chr((ord(ch) - 97 + shift) % 26 + 97)
else:
out += ch
return out
secret = caesar("Meet at Gate 4", 3)
print(secret)
print(caesar(secret, -3))Symmetric encryption: one shared key
In symmetric encryption, the same key locks and unlocks. Modern symmetric ciphers like AES (Advanced Encryption Standard) with 128- or 256-bit keys are very fast and, used correctly, unbreakable in practice. AES protects BitLocker disks, Wi-Fi (WPA2/WPA3), VPN tunnels and the data part of every HTTPS connection.
A simple building block inside many ciphers is XOR (exclusive or). XOR-ing data with a key scrambles it; XOR-ing again with the same key gives the data back. On its own, a short repeating XOR key is weak and easy to break, but it shows the "same key both ways" idea.
The big problem with symmetric encryption is key sharing: how do two people who have never met agree on a secret key over an open network, without anyone listening learning it?
Asymmetric encryption: a key pair
Asymmetric (public-key) cryptography uses two keys that belong together. The public key can be shared with anyone. The private key is kept secret. What one key locks, only the other can unlock. Common algorithms are RSA and elliptic-curve cryptography (ECC), such as ECDSA and Ed25519.
- Encrypting to someone: use their public key; only their private key decrypts.
- Digital signature: the sender signs with their own private key; anyone can verify with the sender's public key. This proves who sent it (authenticity) and that it was not changed (integrity), and the sender cannot deny it (non-repudiation).
- Key exchange: methods like Diffie-Hellman (and its elliptic-curve form, ECDHE) let two sides agree on a shared secret over an open network.
Hybrid encryption: using both
Asymmetric cryptography is slow, so real systems combine both. In HTTPS, asymmetric cryptography proves the server's identity (certificate and signature) and agrees on a fresh symmetric key (ECDHE). Then fast AES or ChaCha20 encrypts the actual data. Signal, WhatsApp, VPNs and SSH work in a similar way.
| Symmetric | Asymmetric | |
|---|---|---|
| Keys | One shared secret key | Public key + private key |
| Speed | Fast | Slow |
| Examples | AES, ChaCha20 | RSA, ECC (ECDSA, Ed25519), ECDHE |
| Used for | Bulk data: disks, Wi-Fi, HTTPS data | Key exchange, signatures, certificates |
Common mistakes
- Inventing your own encryption. Always use well-known, tested libraries and algorithms.
- Hard-coding keys in source code or pushing them to GitHub. Use a secrets manager.
- Sharing a private key, or emailing it. A private key should never leave its owner.
- Confusing encoding with encryption. Base64 only changes the format; anyone can decode it.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Symmetric and asymmetric encryption · 1/7
Encryption turns plaintext into ciphertext with a key. Without the key, it is useless.
Prefer a video by a teacher? Find free videos on “symmetric vs asymmetric encryption explained public key private key” on YouTube ↗
In real life: Signing GST and company filings
In India, company directors and professionals sign filings on government portals with a Digital Signature Certificate (DSC), often on a USB token. The private key stays inside the token. When they sign, the portal checks the signature using the public key in the certificate. That proves who signed and that the document was not changed after signing, which is why a signed filing cannot be easily denied later.
Practice (0/4 done)
Write caesar(text, shift) that shifts upper-case and lower-case letters by shift places (wrapping Z to A) and leaves every other character unchanged. A negative shift decrypts.
Write xor_bytes(data, key) that XORs each byte of data with the key, repeating the key as needed (byte i uses key[i % len(key)]). Return bytes. XOR-ing twice with the same key must give the original back.
Asha wants to send Ravi a secret file using asymmetric encryption. Which key should she encrypt it with?
Why is the Caesar cipher insecure?
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Certificates and PKISystems and cryptography · same module
- Encryption, keys and secretsCloud Computing (AWS, Azure, Google Cloud) · Cloud security and compliance
- Wi-Fi security: WPA2, WPA3 and guest networksComputer Networking (CCNA level) · Wireless networking
- Public, private, hybrid and multi-cloudCloud Computing (AWS, Azure, Google Cloud) · What cloud computing is
- Characters and <ctype.h>C Programming (from zero to systems) · Strings in C
- Wireless security conceptsEthical Hacking and Penetration Testing (legal labs) · Deeper lab skills: passwords, privilege, Metasploit, wireless
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which is a symmetric algorithm?
Answer: AES
AES uses one shared key for both encryption and decryption.
A digital signature is created with…
Answer: The sender's private key
Sign with your private key; others verify with your public key.
Why does HTTPS use both kinds of encryption?
Answer: Asymmetric for identity and key agreement, symmetric for fast bulk encryption
Hybrid encryption combines the strengths of both.
Base64-encoded text is…
Answer: Just encoded: anyone can decode it
Encoding changes format only; there is no key.
What should you never share?
Answer: Your private key
Whoever has your private key can sign and decrypt as you.
In envelope encryption, what does the master key in KMS encrypt?Related
Answer: The data key
The data key encrypts the data; the master key encrypts the data key and never leaves KMS.
Which encryption does WPA2 use?Related
What is a hybrid cloud?Related
Answer: On-premises or private systems working together with a public cloud
Hybrid joins your own systems with a public cloud.
A certificate's private key was leaked. What should happen?Related
Answer: Revoke the certificate and issue a new one with a new key
Revocation tells clients not to trust the old certificate.
From Certificates and PKI
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Systems and cryptography” before its exam: Certificates and PKI.
- Module 3 exam: Systems and cryptography · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Identity and network defence. 43 topics left in the course before the final exam.