Asymmetric encryption (RSA) in Computer Networks - Time & Space Complexity
When using RSA encryption, it is important to understand how the time to encrypt or decrypt data changes as the size of the input grows.
We want to know how the number of steps needed grows when the message or key size increases.
Analyze the time complexity of the RSA encryption process shown below.
function rsaEncrypt(message, publicKey) {
let encrypted = 1n;
let base = BigInt(message);
let exponent = BigInt(publicKey.e);
let modulus = BigInt(publicKey.n);
while (exponent > 0n) {
if (exponent % 2n === 1n) {
encrypted = (encrypted * base) % modulus;
}
base = (base * base) % modulus;
exponent = exponent / 2n;
}
return encrypted;
}
This code encrypts a message by repeatedly squaring and multiplying modulo a large number, using the public key exponent.
Look at what repeats in the code:
- Primary operation: The while loop that does repeated squaring and multiplication.
- How many times: The loop runs roughly as many times as the number of bits in the exponent.
The number of loop steps grows with the size of the exponent, which depends on the key size.
| Input Size (bits in exponent) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The steps increase roughly in a logarithmic scale as the exponent size grows.
Time Complexity: O(k)
This means the time to encrypt grows linearly with the number of bits in the key exponent.
[X] Wrong: "RSA encryption time depends only on the message size."
[OK] Correct: The main factor is the key size (exponent length), not the message length, because the math works on big numbers related to the key.
Understanding how RSA's encryption time grows helps you explain real-world security trade-offs clearly and confidently.
"What if we changed the exponent to a fixed small number? How would the time complexity change?"
Practice
Solution
Step 1: Understand RSA key roles
In RSA, the public key encrypts data, and the private key decrypts it.Step 2: Identify private key function
The private key is kept secret and used to decrypt messages encrypted by the public key.Final Answer:
To decrypt messages encrypted with the public key -> Option CQuick Check:
Private key = decrypt [OK]
- Confusing encryption and decryption roles
- Thinking private key is shared publicly
- Assuming private key encrypts messages
Solution
Step 1: Recall RSA key relationship
RSA uses a pair of keys where the public key encrypts and the private key decrypts.Step 2: Evaluate options
Only Public key encrypts, private key decrypts correctly states the roles of the keys in RSA encryption.Final Answer:
Public key encrypts, private key decrypts -> Option BQuick Check:
Public encrypt, private decrypt [OK]
- Thinking keys are the same
- Mixing up public and private key roles
- Believing keys are unrelated
Public key: (e=3, n=33)
Private key: (d=7, n=33)
Message m = 4What is the encrypted message c using
c = m^e mod n?Solution
Step 1: Calculate encryption formula
Compute c = 4^3 mod 33 = 64 mod 33.Step 2: Find remainder
64 divided by 33 leaves remainder 64 - 33*1 = 31, so 64 mod 33 = 31.Step 3: Re-check calculation
Actually, 4^3 = 64, 64 mod 33 = 31, so c = 31.Final Answer:
31 -> Option AQuick Check:
4^3 mod 33 = 31 [OK]
- Forgetting to apply modulo
- Calculating power incorrectly
- Mixing up encryption and decryption formulas
c = (m ** d) % nto encrypt a message. Why is this incorrect?
Solution
Step 1: Understand RSA key usage
Encryption uses the public exponente, decryption uses private exponentd.Step 2: Identify error in code
The code usesdto encrypt, which is incorrect; it should usee.Final Answer:
Becausedis the private key exponent used for decryption, not encryption -> Option AQuick Check:
Encrypt with e, decrypt with d [OK]
- Using private key exponent for encryption
- Confusing exponentiation operator validity
- Ignoring modulo operation necessity
Solution
Step 1: Understand RSA limitations
RSA is slow and inefficient for large data encryption like 128-bit messages directly.Step 2: Identify best practice
Common practice is to encrypt a symmetric key with RSA, then use that key for fast message encryption.Final Answer:
Use RSA to encrypt a symmetric key, then use that key to encrypt the message -> Option DQuick Check:
Hybrid encryption = RSA + symmetric key [OK]
- Encrypting large data directly with RSA
- Sharing private key publicly
- Reversing encryption/decryption key roles
Ready to prove it?
Pass the test and this pattern moves unknown
