TLS/SSL for encrypted communication in IOT Protocols - Time & Space Complexity
When devices use TLS/SSL to encrypt messages, the time it takes depends on how much data is sent and how the encryption works.
We want to know how the time to encrypt and decrypt grows as the message size grows.
Analyze the time complexity of the following code snippet.
// Simplified TLS encryption process
function encryptData(data) {
let encrypted = '';
for (let i = 0; i < data.length; i++) {
encrypted += String.fromCharCode(encryptByte(data.charCodeAt(i)));
}
return encrypted;
}
function encryptByte(byte) {
// Simulate encryption of one byte
return byte ^ 0xAA; // simple XOR for example
}
This code encrypts data byte by byte using a simple operation for each byte.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each byte of the data to encrypt it.
- How many times: Once for every byte in the input data.
As the data size grows, the encryption time grows in a straight line because each byte is handled separately.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 encryptByte calls |
| 100 | 100 encryptByte calls |
| 1000 | 1000 encryptByte calls |
Pattern observation: Doubling the input doubles the work because each byte is encrypted one by one.
Time Complexity: O(n)
This means the time to encrypt grows directly with the size of the data.
[X] Wrong: "Encrypting a small message takes the same time as a large one because encryption is instant."
[OK] Correct: Each byte must be processed, so bigger messages take more time, not the same.
Understanding how encryption time grows helps you explain performance in secure communication, a key skill in IoT and network security roles.
"What if the encryption function processed data in fixed-size blocks instead of byte-by-byte? How would the time complexity change?"