Asymmetric encryption (RSA, ECC) in Cybersecurity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using asymmetric encryption like RSA or ECC, it is important to understand how the time to encrypt or decrypt changes as the size of the input or key grows.
We want to know how the work needed grows when we use bigger keys or encrypt larger messages.
Analyze the time complexity of the following simplified RSA encryption process.
// Simplified RSA encryption
function rsaEncrypt(message, publicKey, n) {
let encrypted = 1n;
for (let i = 0n; i < message; i++) {
encrypted = (encrypted * publicKey) % n;
}
return encrypted;
}
This code raises the public key to the power of the message modulo n, simulating RSA encryption.
- Primary operation: Multiplying and taking modulo repeatedly inside the loop.
- How many times: The loop runs once for each unit of the message size (the exponent).
As the message size (exponent) grows, the number of multiplications grows roughly the same amount.
| Input Size (message) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1000 | 1000 multiplications |
Pattern observation: Doubling the message size roughly doubles the work needed.
Time Complexity: O(n)
This means the time to encrypt grows directly in proportion to the size of the message or exponent.
[X] Wrong: "Encryption time stays the same no matter how big the message or key is."
[OK] Correct: Larger messages or keys require more calculations, so the time grows with their size.
Understanding how encryption time grows helps you explain performance trade-offs in security systems clearly and confidently.
"What if we use a faster method like exponentiation by squaring instead of simple repeated multiplication? How would the time complexity change?"
Practice
Solution
Step 1: Understand key usage in asymmetric encryption
Asymmetric encryption uses two keys: a public key to encrypt and a private key to decrypt messages.Step 2: Compare with symmetric encryption
Symmetric encryption uses the same key for both encrypting and decrypting, which is different from asymmetric encryption.Final Answer:
To use two different keys: one to encrypt and one to decrypt -> Option CQuick Check:
Asymmetric encryption = two keys (public/private) [OK]
- Confusing asymmetric with symmetric encryption
- Thinking one key is used for both encrypting and decrypting
- Believing encryption speeds up internet
- Assuming passwords are stored in plain text
Solution
Step 1: Recall RSA key generation method
RSA keys are created by multiplying two large prime numbers to form the public and private keys.Step 2: Check other options for correctness
RSA does not use a single key, keys are not fixed at 128 bits, and it heavily relies on math operations.Final Answer:
RSA keys are generated using large prime numbers -> Option DQuick Check:
RSA key generation = large primes [OK]
- Thinking RSA uses one key only
- Assuming RSA keys have fixed small size
- Ignoring the role of prime numbers
- Believing RSA avoids math operations
public_key = (e=3, n=33)
private_key = (d=7, n=33)
message = 4
encrypted = (message^e) mod n
decrypted = (encrypted^d) mod nWhat is the decrypted message?
Solution
Step 1: Calculate encrypted message
encrypted = (4^3) mod 33 = 64 mod 33 = 31Step 2: Calculate decrypted message
decrypted = (31^7) mod 33. Calculating 31^7 mod 33 gives 4 (using modular exponentiation).Final Answer:
4 -> Option AQuick Check:
Decrypted message matches original message [OK]
- Forgetting to apply modulo operation
- Mixing up public and private keys
- Incorrectly calculating powers
- Assuming encrypted equals decrypted
private_key = generate_ecc_key()
public_key = private_key * GWhat is the likely error?
Solution
Step 1: Understand ECC key generation
In ECC, the public key is generated by multiplying the base point G by the private key scalar.Step 2: Identify the error in code
The code multiplies private_key by G, but the correct order is G multiplied by private_key.Final Answer:
Public key is generated by multiplying G by private key, not the other way -> Option AQuick Check:
ECC public key = G * private key [OK]
- Reversing multiplication order
- Confusing addition with multiplication
- Thinking private key is a point
- Believing private key can't generate public key
Solution
Step 1: Compare key sizes and efficiency
ECC achieves strong security with smaller keys compared to RSA, which helps reduce processing power and battery use on mobiles.Step 2: Evaluate other options
ECC keys are not easier to guess; RSA is generally slower on mobiles; ECC requires private keys like RSA.Final Answer:
ECC uses smaller keys for similar security, saving battery and space -> Option BQuick Check:
ECC = smaller keys, better for mobiles [OK]
- Thinking ECC keys are weaker
- Assuming RSA is faster on mobiles
- Believing ECC doesn't use private keys
- Ignoring battery and space constraints
