SECURITY ENGINEERING · PART 4 · CRYPTOGRAPHY IN PRACTICE · ENCRYPTION · 47 OF 229

RSA vs ECC: Public and Private Keys Explained

Asymmetric cryptography uses a key pair: a public key anyone may have and a private key only you hold. This episode explains what that solves, how RSA and elliptic curves differ, real numbers for key size and speed (RSA-2048 key generation 0.13 s, P-256 0.04 ms), why RSA can only encrypt 190 bytes, hybrid encryption, and how private keys are stolen and protected.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 14

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • A public and a private key
  • RSA 2048+ or elliptic curves
  • hybrid with AES for data
  • guard the private key

Notes

The problem

Here is the problem in full. AES needs the same key on both sides. If you send that key over the network, anyone sniffing copies it. Meeting in person does not scale to the internet. And the numbers get large: one thousand people who each want a private channel with every other need almost half a million shared keys. In the nineteen seventies, cryptographers found a way out, and it changed how security works.

Key distribution
  • AES needs the same key on both sides
  • send the key over the network? a sniffer copies it
  • meet in person first? does not scale to the internet
  • 1,000 people talking in pairs need 499,500 shared keys
  • solved in the 1970s with public key cryptography

The key pair

You generate two keys together, linked by mathematics. The public key can be given to anyone. The private key never leaves its owner. Whatever is encrypted with the public key can only be decrypted with the private key, and knowing the public key does not let you work out the private one. So the receiver publishes a public key, the sender encrypts with it, and only the receiver can read the result. A thousand people now need only a thousand key pairs.

Two keys, one pair
  • generated together, linked by mathematics
  • public key: give to anyone, put it on a website
  • private key: never shared, never leaves its owner
  • knowing the public key does not reveal the private key
  • 1,000 people now need only 1,000 key pairs

Two uses

A key pair has three uses. Encryption: anyone encrypts with your public key and only you decrypt. Signing, which runs the other way: you sign with your private key and anyone verifies with your public key, which proves the message came from you. And key exchange, where two sides combine their keys to agree on a shared secret. In modern systems, signing and key exchange are the common uses, and direct public key encryption is rare.

What a key pair can do
  • 1. Encrypt to someone public key encrypts, private key decrypts
  • 2. Sign as someone private key signs, public key verifies
  • 3. Agree on a shared key (key exchange, episode 6)
  • signing and key exchange are the common uses today

RSA

RSA is the classic algorithm, from nineteen seventy seven, and it is still everywhere. Its security rests on a simple fact: multiplying two large prime numbers is easy, but splitting the product back into them is not. Keys must be at least two thousand and forty eight bits; one thousand and twenty four is no longer safe. RSA must always be used with a padding scheme: OAEP for encryption and PSS for signatures. Raw RSA without padding is broken.

RSA
  • from 1977; still everywhere
  • based on: multiplying two big primes is easy, splitting the product is not key sizes: 2048 bits minimum, 3072 or 4096 for long life
  • 1024 bits: no longer safe
  • always use padding: OAEP to encrypt, PSS to sign
  • slow to generate, big keys

Elliptic curves

Elliptic curve cryptography is the modern choice. It rests on a different mathematical problem that is harder for the same size, so a two hundred and fifty six bit elliptic curve key is about as strong as a three thousand bit RSA key. The curves to know are P two fifty six, the widely supported standard, and Curve two five five one nine, used as X two five five one nine for key exchange and E D two five five one nine for signing. Keys are smaller and operations faster.

ECC
  • based on a harder problem on elliptic curves
  • 256-bit ECC is about as strong as 3072-bit RSA
  • curves to use: P-256 (NIST, widely used) Curve25519: X25519 for key exchange, Ed25519 to sign
  • smaller keys, faster, less to send on the wire

Real numbers

Here are real numbers from one machine. Generating an RSA two thousand and forty eight key pair took zero point one three seconds, and a four thousand and ninety six bit pair took zero point four four. A P two fifty six key pair took four hundredths of a millisecond, three thousand times faster. Public keys: two hundred and ninety four bytes for RSA, sixty five for P two fifty six, and just thirty two for E D two five five one nine. This is why new systems choose elliptic curves.

our measurements
generate a key pair
  RSA-2048        0.131 s
  RSA-4096        0.444 s
  P-256           0.00004 s

public key size
  RSA-2048        294 bytes
  P-256            65 bytes
  Ed25519          32 bytes

# RSA-2048: decrypt 0.36 ms

The size limit

There is a practical limit that surprises people. With a two thousand and forty eight bit key and OAEP padding, we encrypted one hundred and ninety bytes and got a two hundred and fifty six byte ciphertext. At one hundred and ninety one bytes the library refused. RSA cannot encrypt a file or a database record. It is also slow compared with AES. So nobody encrypts real data directly with a public key. They do something smarter.

RSA-2048 with OAEP
ct = pub.encrypt(b"x" * 190, oaep)
len(ct)            # 256 bytes

pub.encrypt(b"x" * 191, oaep)
# ValueError: Encryption failed

# the limit is 190 bytes, and it
# is slow compared with AES
# so nobody encrypts data
# directly with RSA

Hybrid encryption

The answer is hybrid encryption, and almost every real system uses it. Generate a random AES key. Encrypt the data with AES in GCM mode, which is fast and handles any size. Then use the public key method only to protect that small AES key. Send both together. TLS, PGP, SSH and cloud envelope encryption all work this way. You get the easy key sharing of asymmetric cryptography and the full speed of symmetric encryption.

How everything really works
  • 1. make a random AES key
  • 2. encrypt the data with AES-GCM (fast, any size)
  • 3. protect the AES key with the public key method
  • 4. send both together
  • used by: TLS, PGP, age, SSH, KMS envelope encryption
  • best of both: easy sharing and full speed

How attackers win

Attackers almost never break the mathematics. They take the private key. Keys are committed to git repositories, left unencrypted in home folders and backups, baked into container images and stored in pipeline variables. Occasionally a device generates keys with weak random numbers, and the keys can be guessed. Whoever holds the private key is the owner as far as any system can tell: they can decrypt, sign and log in, and nothing looks wrong.

Private key theft
  • keys committed to git repos
  • unencrypted keys in home folders and backups
  • keys baked into container images and CI variables
  • weak random numbers at key generation
  • a stolen key signs and decrypts as the owner, and nothing looks wrong

Protect the private key

So protect the private key above all. Generate it where it will be used instead of copying it around. Put a passphrase on key files. Better, keep it in hardware: a hardware security module, a TPM, a security key or a cloud key management service, where the key can be used but never exported. Run secret scanning on every commit. Prefer short-lived certificates to long-lived keys. And have a tested way to revoke a key when it leaks.

Defenses
  • generate keys where they will be used; do not copy them
  • passphrase on key files
  • hardware: HSM, TPM, security key, cloud KMS (the key cannot be exported)
  • secret scanning on every commit
  • short-lived certificates instead of long-lived keys
  • a tested way to revoke

Common mistakes

Mistakes. RSA keys shorter than two thousand and forty eight bits. RSA used without OAEP or PSS padding. Trying to encrypt bulk data directly with RSA. Private keys in a repository or a container image. One key pair used for encryption, signing and login together. No plan to rotate or revoke keys. And the most dangerous: inventing your own scheme instead of using a vetted library and a standard protocol.

Avoid these
  • RSA keys under 2048 bits
  • RSA without OAEP or PSS
  • encrypting bulk data with RSA
  • private keys in a repo or an image
  • one key pair for every purpose
  • no plan to rotate or revoke
  • inventing your own scheme

Say it like this

In the interview: symmetric encryption is fast but both sides need the same key. Asymmetric solves key sharing and proves identity, but it is slow and limited in size. So real systems are hybrid: asymmetric cryptography to agree a key and to sign, and AES in GCM mode for the data. I prefer elliptic curves for new systems because keys are smaller and faster, and I keep private keys in hardware or a key management service.

In the interview
  • "Symmetric is fast but needs
  • a shared key. Asymmetric
  • solves sharing and identity
  • but is slow and size-limited.
  • So real systems are hybrid: asymmetric to agree a key and sign, AES-GCM for the data.
  • I prefer elliptic curves and keep private keys in hardware."

Interview questions

Q1What is asymmetric encryption?

Cryptography that uses a mathematically linked key pair: a public key that can be shared freely and a private key kept secret. Data encrypted with the public key can only be decrypted with the private key, and signatures made with the private key can be verified with the public key.

Q2What is the difference between symmetric and asymmetric encryption?

Symmetric encryption uses one shared key and is very fast, but the key must be distributed securely. Asymmetric encryption uses a key pair, solving key distribution and enabling signatures, but is far slower and limited in message size. Real systems combine them.

Q3RSA or ECC: which should you use?

For new systems, elliptic curves (P-256, X25519, Ed25519): equivalent security with much smaller keys and faster operations. RSA at 2048 bits or more remains secure and is used where compatibility requires it.

Q4What RSA key size is secure?

At least 2048 bits, which is the current minimum in most standards; 3072 or 4096 bits for keys that must remain secure for many years. 1024-bit RSA is considered broken for practical purposes and must not be used.

Q5What is hybrid encryption?

A scheme that encrypts the data with a fast symmetric cipher under a random key, and then protects that key with asymmetric cryptography. TLS, PGP, SSH and KMS envelope encryption all work this way, combining easy key distribution with high speed.

Q6Why is RSA padding (OAEP, PSS) required?

Raw "textbook" RSA is deterministic and malleable, which allows several practical attacks. OAEP adds randomness and structure for encryption, and PSS does the same for signatures. The older PKCS#1 v1.5 padding has known attacks and should be avoided for new designs.

Q7How are private keys usually compromised?

By theft rather than cryptanalysis: keys committed to source control, left unencrypted on disks and in backups, embedded in images or CI variables, or generated with weak randomness. Hardware-backed storage and secret scanning are the main defenses.

Q8What is an HSM or a KMS used for?

A hardware security module or cloud key management service generates and stores private keys so that they can be used for operations but never exported. This limits the impact of a compromised server, because the attacker cannot copy the key.