Skip to content
Visual explainer 3h to learn 4 steps

Symmetric vs asymmetric encryption

Choose symmetric or asymmetric encryption for a use case and explain key exchange.

By Invicly Tech, , in Cyber Security Fundamentals. About 11 minutes to read.

Topic 16 of 49

In short

Symmetric encryption uses one shared key and is fast; asymmetric uses a public and private key pair and solves key distribution. Real systems combine them: asymmetric crypto exchanges a symmetric key, which then encrypts the bulk of the data.

Explained simply

Encryption turns readable data (plaintext) into scrambled data (ciphertext) using a key. Without the right key, the ciphertext looks like random noise. The method itself is public; all the secrecy is in the key. This idea is so important it has a name: Kerckhoffs's principle.

Symmetric encryption uses one key for both locking and unlocking, like a house key you copy for a friend. It is very fast, so it protects almost all real data: disks, files and web traffic. The hard part is giving the key to the other person safely.

Asymmetric encryption uses two linked keys. The public key can be shared with the whole world; the private key never leaves its owner. What one key locks, only the other can unlock. It is much slower, so it is used for small but important jobs: sharing keys and making signatures.

Real systems mix both. When you open a website, your browser and the server use asymmetric maths to agree on a fresh symmetric key, then use that fast key for the actual data. This mix is called hybrid encryption.

Press Play on a diagram to walk through it one step at a time.

CompareSymmetric vs asymmetric
Symmetric
  • One shared key
  • Very fast
  • Key distribution is hard
  • Example: AES
Asymmetric
  • Public and private keys
  • Slower
  • Solves key sharing
  • Example: RSA, ECC
FlowHybrid (envelope) encryption of one file
  1. 1
    Make data key

    Random 256-bit AES key

  2. 2
    Encrypt file

    AES-GCM with the data key

  3. 3
    Wrap data key

    Encrypt it with the public or KMS key

  4. 4
    Store together

    Ciphertext plus wrapped key

  5. 5
    Read later

    Unwrap key with private key, decrypt file

In depth

AES and why the mode matters

AES (Advanced Encryption Standard) is the symmetric cipher used almost everywhere. It works on 128-bit blocks and supports 128, 192 or 256-bit keys. AES-128 is still considered secure; AES-256 gives extra margin. Most CPUs have built-in AES instructions, so it can encrypt gigabytes per second.

AES alone only encrypts one 16-byte block. A mode of operation says how to handle longer data. ECB mode encrypts each block separately, so identical blocks give identical output and patterns leak; never use it. CBC hides patterns but needs a random IV (initial value) and a separate integrity check. GCM is the modern default: it is authenticated encryption, meaning it also detects any change to the ciphertext. ChaCha20-Poly1305 is the main alternative, fast on phones without AES hardware.

The big GCM rule: never reuse a nonce (a number used once) with the same key. Reuse can expose data and let attackers forge messages.

  • Use AES-GCM or ChaCha20-Poly1305 (authenticated encryption)
  • Never use ECB mode
  • GCM nonce: 12 bytes, unique for every message with the same key

RSA, elliptic curves and key sizes

RSA is the classic asymmetric algorithm. Its security comes from how hard it is to factor a huge number into its two prime factors. Keys must be big: 2048 bits is the minimum today, and 3072 bits is advised for data that must stay safe for many years. RSA can only encrypt a small amount of data, less than its key size, so it encrypts a key, not a file.

Elliptic curve cryptography (ECC) gets the same security with much smaller keys: a 256-bit curve key is roughly as strong as 3072-bit RSA. Common choices are P-256, X25519 for key exchange and Ed25519 for signatures. Smaller keys mean faster handshakes and less data.

A large quantum computer could break both RSA and ECC. To prepare, NIST published ML-KEM in 2024 as a post-quantum key exchange, and major browsers already combine it with X25519 in TLS. Symmetric AES-256 is expected to stay safe.

Diffie-Hellman: agreeing a secret in public

Diffie-Hellman (DH) lets two sides create the same secret number while an eavesdropper watches every message. Both agree on public values: a large prime p and a base g. Alice picks a secret a and sends g^a mod p. Bob picks a secret b and sends g^b mod p. Alice raises Bob's value to a, Bob raises Alice's value to b, and both get g^(ab) mod p. The watcher sees g^a and g^b but cannot work out the secret without solving a very hard maths problem.

Modern systems use the elliptic curve version, ECDH, often with X25519. When fresh keys are made for every session (ECDHE, the E means ephemeral), stealing the server's long-term key later cannot decrypt old recordings. This property is called forward secrecy.

DH alone does not prove who you are talking to. An attacker in the middle could run DH with both sides. That is why TLS signs the exchange with the server's certificate key.

FlowDiffie-Hellman with tiny numbers (p = 23, g = 5)
  1. 1
    Agree in public

    p = 23, g = 5

  2. 2
    Alice sends 4

    Secret a = 4: 5^4 mod 23 = 4

  3. 3
    Bob sends 10

    Secret b = 3: 5^3 mod 23 = 10

  4. 4
    Alice computes

    10^4 mod 23 = 18

  5. 5
    Bob computes

    4^3 mod 23 = 18

  6. 6
    Eavesdropper

    Saw only 23, 5, 4 and 10

Key management is the real challenge

Strong algorithms rarely fail; key handling does. Keys get hard-coded in source, left in backups or shared by email. A good design answers: where is the key made, where is it stored, who can use it, how is it rotated, and how is it destroyed.

Cloud and enterprise systems use envelope encryption. Each file or record is encrypted with its own data key. That data key is then encrypted with a key-encryption key kept in a KMS (key management service) or an HSM (hardware security module), a device that never lets the master key out. You store the encrypted data key next to the data. To read it, you ask the KMS to unwrap the data key, which is logged and permission-checked.

This gives defenders control and visibility: revoking access to the master key makes all its data unreadable, and every use leaves an audit record.

  • Never commit keys to Git; scan repos for secrets
  • Generate keys with a secure random source (os.urandom, secrets, openssl rand)
  • Log and alert on unusual KMS decrypt activity

Learn it in 4 short steps

About 45m each. Track them with a timer in your plan.

  1. 1
    Explain symmetric ciphers like AES~45m
  2. 2
    Explain public and private key pairs~45m
  3. 3
    Explain hybrid encryption~45m
  4. 4
    Describe Diffie-Hellman key exchange~45m

Key points

  • Symmetric encryption uses the same key to encrypt and decrypt.
  • Asymmetric encryption uses a matched public and private key.
  • Hybrid schemes use asymmetric to share a symmetric key.
  • Diffie-Hellman agrees a shared secret over an open channel.

Key terms

Tap a card to flip it. 0/4 known

Examples

Authenticated encryption with AES-GCM in Python

Step 1: generate a random 256-bit key. Step 2: make a fresh 12-byte nonce for this message. Step 3: encrypt; the result includes a 16-byte tag that detects tampering. Optional associated data (here a header) is checked but not encrypted. Step 4: decrypt with the same nonce. Step 5: flip one byte and decrypt again: the library raises InvalidTag instead of returning garbage. Store the nonce with the ciphertext; it is not secret, it just must never repeat for this key.

python
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.exceptions import InvalidTag

key = AESGCM.generate_key(bit_length=256)
aes = AESGCM(key)
nonce = os.urandom(12)
ct = aes.encrypt(nonce, b"pay 100 to bob", b"header-v1")
print(aes.decrypt(nonce, ct, b"header-v1"))   # b'pay 100 to bob'

bad = ct[:-1] + bytes([ct[-1] ^ 1])
try:
    aes.decrypt(nonce, bad, b"header-v1")
except InvalidTag:
    print("tampering detected")

Diffie-Hellman by hand, then for real

Step 1: with tiny public numbers p = 23 and g = 5, Alice picks 4 and Bob picks 3. Step 2: each computes a public value and sends it. Step 3: each combines the other's public value with their own secret. Both reach 18, which never crossed the wire. Step 4: real systems use X25519, where the numbers are 256 bits long, so guessing the secret is impossible in practice. The second half of the code does the same exchange with the cryptography library.

python
p, g = 23, 5
a, b = 4, 3
A, B = pow(g, a, p), pow(g, b, p)        # 4 and 10 are sent
print(pow(B, a, p), pow(A, b, p))         # 18 18

from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
alice, bob = X25519PrivateKey.generate(), X25519PrivateKey.generate()
s1 = alice.exchange(bob.public_key())
s2 = bob.exchange(alice.public_key())
print(s1 == s2, len(s1))                  # True 32

Quick reference

  • Symmetric: one shared key, fast (AES, ChaCha20). Asymmetric: key pair, slow (RSA, ECC)
  • Prefer authenticated encryption: AES-GCM or ChaCha20-Poly1305
  • Never use ECB mode, and never reuse a GCM nonce with the same key
  • RSA: 2048 bits minimum, 3072 for long-term; 256-bit ECC is about as strong as RSA-3072
  • ECDHE gives forward secrecy: old sessions stay safe if the server key leaks later
  • Diffie-Hellman needs authentication (signatures, certificates) to stop a man in the middle
  • Envelope encryption: data key encrypts data, KMS key encrypts the data key
  • openssl rand -hex 32 makes a random 256-bit key in hex

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1You must encrypt a 4 GB backup for a colleague who has sent you their public key. What is the right approach?

Question 2

2Why is ECB mode unsafe?

Question 3

3What does forward secrecy protect against?

Question 4

4Which is a property of AES-GCM that AES-CBC does not provide by itself?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    For each case, say symmetric, asymmetric or both: full-disk encryption, sending your public key to a server for SSH login, HTTPS to a website.

  2. Exercise 2medium

    Using openssl, generate an RSA key pair, encrypt a file with a random AES key, and protect that AES key with the public key.

  3. Exercise 3hard

    Show in Python why reusing an AES-GCM nonce is dangerous: encrypt two different messages with the same key and nonce, and recover information about them without the key.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Is AES-128 still secure?
Yes. No practical attack breaks AES-128 today. AES-256 is chosen for extra margin, including against future quantum computers.
What is the difference between encryption and encoding?
Encoding, like Base64, just changes the format and anyone can reverse it without a key. Encryption needs a secret key to reverse, so only Base64 is never protection.
Will quantum computers break all encryption?
A large enough quantum computer would break RSA and elliptic curve crypto, but not AES with long keys. That is why new post-quantum algorithms like ML-KEM are being rolled out now, often combined with X25519.
Should I write my own encryption code?
No. Use well-reviewed libraries and high-level APIs such as AESGCM, libsodium or age. Small mistakes, like reusing a nonce, break security completely.

Go deeper

ShareXLinkedInWhatsApp