AP Cybersecurity · Study guide

Key Length and Security

AP® Cybersecurity · Unit 5 · Applications, Data & Cryptography

Key Length and Security

Why a longer key means a larger keyspace, how many guesses a brute-force attack needs on average, the cost of longer keys, and why key lengths only compare within one algorithm.

1Keyspace grows by doubling

Longer keys produce larger keyspaces. For binary keys, an n-bit key has a keyspace of 2n: each bit has two choices, so every added bit doubles the number of possible keys.

Figure 1. Adding one bit gives every old key two versions, one starting with 0 and one with 1, so the keyspace doubles.

2How many guesses on average

An adversary using software to guess an n-bit key will find it, on average, after 2n ÷ 2 = 2n−1 guesses. The right key is equally likely to be anywhere in the search, so on average the search ends halfway through. The worst case is all 2n.

A real test: a program picked 2,000 random 12-bit keys and searched for each one from 0 upward.

MeasureResult
Keyspace212 = 4,096
Predicted average, 2n−12,048 guesses
Measured average2,060 guesses
Fastest and slowest searches2 and 4,095 guesses

At a trillion guesses per second, here is the average search time:

Key length (bits)KeyspaceAverage guessesAverage time
828 = 25627less than a second
16216 = 65,536215less than a second
56256 = 7.2 × 1016255about 10 hours
1282128 = 3.4 × 10382127about 5.4 × 1018 years
2562256 = 1.2 × 10772255about 1.8 × 1057 years

3Longer keys cost time

Longer keys are more secure, but they also require more time to encrypt and decrypt. AES-256 runs 14 rounds of scrambling per block instead of AES-128's 10. For RSA the cost grows much faster: doubling an RSA key from 2048 to 4096 bits makes each decryption several times slower, which matters on a busy server or a small device.

4Recommendations keep rising

Processing power and efficiency keep improving, so software guesses keys faster every year. Key-length recommendations for both symmetric and asymmetric algorithms are therefore periodically increased.

ThenWhat happenedNow
56-bit DES keys were the U.S. standard from 1977In 1998 a purpose-built machine found a DES key in 56 hoursAES with 128-bit or longer keys
512-bit RSA keys were common in the 1990sA 512-bit RSA key was broken in 19992048-bit RSA is the minimum; 3072 or more for long-term secrets

5Compare within one algorithm

Key-length comparison is only valid for keys of the same algorithm.

  • An AES 256-bit key is more secure than an AES 128-bit key.
  • An RSA 4096-bit key is more secure than an RSA 2048-bit key.
  • RSA and AES keys cannot be compared directly. RSA keys must be much longer because attackers do not search every possible RSA key; they use math shortcuts. A 2048-bit RSA key is not sixteen times stronger than a 128-bit AES key.
The key idea

An n-bit key has 2n possible values, and a brute-force search needs 2n−1 guesses on average. Longer keys are more secure but slower, recommended lengths rise as computers get faster, and lengths only compare within the same algorithm.

6Worked examples

Worked Example 1 Compute the keyspace

A 20-bit key

A practice cipher uses a 20-bit key. What is its keyspace, and how many guesses does brute force need on average?

  1. 1
    Keyspace: 220 = 1,048,576.
  2. 2
    Average: 219 = 524,288 guesses.
Answer: 1,048,576 keys; 524,288 guesses on average.
Worked Example 2 Compare lengths

How much bigger?

How many times larger is the keyspace of a 64-bit key than a 60-bit key?

  1. 1
    Each extra bit doubles the keyspace.
  2. 2
    Four extra bits: 24 = 16.
Answer: 16 times larger, and 16 times as long to search.
Worked Example 3 Valid comparison?

RSA versus AES

A student says, “RSA-2048 is far more secure than AES-256, because 2048 is bigger than 256.” Evaluate the claim.

  1. 1
    Key lengths only compare within the same algorithm.
  2. 2
    RSA and AES are attacked in completely different ways, so their bit counts do not measure the same thing.
Answer: Invalid. RSA and AES keys cannot be compared by length.
Worked Example 4 Choose a length

Long-term records

A hospital must keep encrypted records private for 30 years. Its policy allows AES-128 or AES-256. Which should it choose, and what is the cost?

  1. 1
    Computers will keep getting faster, and recommendations rise over time.
  2. 2
    A 30-year secret needs a large safety margin: AES-256.
  3. 3
    The cost is more time to encrypt and decrypt, which modern servers handle easily.
Answer: AES-256, accepting a small speed cost for a margin that should last decades.

7Common mistakes

Common Mistake 1 · Thinking doubling the bits doubles the keyspace

Adding one bit doubles it. Going from 128 to 256 bits multiplies the keyspace by 2128.

Common Mistake 2 · Using 2n for the average

The average brute-force search needs half of that: 2n−1 guesses.

Common Mistake 3 · Comparing lengths across algorithms

Only compare AES to AES or RSA to RSA.

Common Mistake 4 · Treating today's length as permanent

Faster computers push recommendations up. A key length that was fine years ago may not be now.

Explain key length like this

“A 256-bit AES key has 2256 possible values, so a brute-force attack needs 2255 guesses on average, far more than any computer can try. It is more secure than a 128-bit AES key but takes somewhat longer to encrypt and decrypt.”

Give the keyspace, the average number of guesses, and the trade-off, and compare only within one algorithm.