An Introduction to Symmetric Key Encryption
Shoeb Ilyas - October 10, 2026 - 1777 Words
Cryptography is all about secure communication in the presence of a bad actor. Modern cryptography exists at the crossroads of mathematics, computer science, digital processing, physics, and other subjects. Prior to the modern age, cryptography was effectively synonymous with encryption. Encryption can be defined as an act of converting a message (plaintext) to some gibberish (ciphertext), which can only be read by reversing the process. Usually, an encryption key is involved during the process of encryption.
Cryptography is extremely important in the modern computer science and software industry. Without it, there's no secure communication over the internet or through any other media. Banks cannot function securely. All your chats, voice messages, and calls can be easily intercepted by a third party adversary. There's no privacy. There can be no secure HTTPS, SMTP, FTP, or other secure communication protocols without it. In short, the entire digital infrastructure will collapse without cryptography because neither will the data be secure over any network nor will we be able to store it securely.
If you don't know cryptography or encryption well, you have zero chance of coming up with strong and secure software.
In this series of articles, we will look into different encryption techniques that are used in modern computing for secure communication over the wire.
There are three cryptography concepts that we will look into in this series of articles:
- Symmetric Key Encryption
- Asymmetric Key Encryption
- Hashing
Before proceeding, there are some common terms that we need to memorize. Whenever I use these terms, I use them very consciously and never interchangeably.
Encryption: The process of generating ciphertext from the plaintext. Plaintext: The text or data that needs to be encrypted. Key: A secret value that helps us generate cipher text. Ciphertext: A nonsense text generated from the plaintext using an encryption key. Cipher: A function that encrypts our plaintext.
We will be dealing with a lot of binary and hex encodings. Therefore, you need to be familiar with the digital logic and be comfortable working with hex and binary. Basics should suffice. You don't need to be an expert.
I have also prepared some fancy animations to walk you through the encryption algorithms discussed in these articles.
What does it mean to encrypt a message? When we talk about encrypting a message, we are referring to bit-level manipulation of data. All data formats, be it text, PNG, video files, or any other format, are binary in the end. So manipulating data at the binary level prevents us from corrupting a file, given that we properly encrypt or decrypt our plaintext.
Symmetric Key Encryption
In Symmetric key encryption, two clients that intend to share information possess a secret encryption key. The encryption key must be the same for both clients. One client encrypts the plaintext using the key while the other decrypts the cipher text using the same key to reproduce the original message. The main drawback of the symmetric key encryption is that both clients need to possess the exact same keys for secure communication.
There are two types of symmetric key encryption algorithms:
- Stream Ciphers: In stream ciphers, like ChaCha, the cipher encrypts one single digit at a time
- Block Ciphers: In block ciphers, like AES, the plaintext is divided into equal sized chunks and encryption is applied to the n-bit blocks of data at a time instead of going through each digit and encrypting it. AES uses 128 bit blocks for encryption
There are many symmetric key encryption algorithms out there but we will scope ourselves to two - OTP and AES.
OTP - The Perfect Encryption
OTP, short for one-time pad, is a symmetric key encryption algorithm that is fairly simple but extremely powerful. To the extent, that its impossible for any external bad actor to decrypt your ciphertext when its encrypted using OTP. This is the reason its also known as the perfect encryption. We'll look into details as to why exactly but first let's understand how OTP works. Heads up, don't confuse this with one-time password. Both are two different concepts.
There are three simple rules to OTP:
- The key length must be more than or equal to the message length
- The key must be used only once
- The key must be truly random
Note: Math.random() in JavaScript is not cryptographically secure and and therefore should not be used. If you want to generate a cryptographically secure key, then you should use
randomBytesfunction from thecryptomodule.
Let's now, understand the working of OTP in detail.
Every message is binary in the end. "HI" in ASCII is two bytes: 16 bits.
H = 0x48 = 01001000 I = 0x49 = 01001001
A secret key is generated using a random generator. The key length is more than or equal to the longest expected length of the plaintext. When sending this message, the sender, encrypts the plaintext by XOR-ing each bit with the each corresponding bit secret in the secret key. If the length of the plaintext is less than the length of the secret key, the plaintext is padded with 0fhex (1111 in binary) to make the plaintext length equal to the length of the secret. This secret key is present on the receiver's end as well. Once the cipher text lands at the receiver's end, they can decrypt the OTP using the same secret key.
The secret key is destroyed once it's used because using the same secret key to communicate multiple messages, can enable a middleman to potentially decipher our messages using some pattern recognition.
Perfect Encryption
OTP is regarded as the perfect encryption because it's impossible to decrypt it without having the required key. This is why it's perfectly secure. Let's understand this more in detail.
For any cipher text, the key once used is destroyed and never used to encrypt the following plain texts. Statistically, this means that every cipher text is unique in itself and there's no pattern to be studied here. Hence the name One-Time.
The deciphering of a cipher text is nothing but a plain guess without a secret key. This is because every possible combination of plaintext equally likely.
Let's take 000101010101 as our plaintext example. Let's say the random key is 110110111011.
If we XOR the plaintext with the random key we get - 110011101110. Now for the first bit in our cipher text, the following bit combinations are perfectly valid. I will use P for plaintext bit and C for the cipher text bit. Here, the first bit is 1. However, this proves nothing because the plaintext could be 1 or it could be 0 because we, as an external bad actor, don't have access to the secret key.
However, this bit is just one out of the many bits. In a practical world, this can have hundreds of bits and all possible combinations are equally likely. Therefore, if we have 100 bits, then we have 2100! combinations. All combinations are treated as equally valid. Theoretically, even an attacker generates all possible combinations, they can never know which decrypted message was the original one because a cipher text doesn't give any information about the plaintext and is independent of it,
Therefore, breaking One-Time Pad is effectively impossible.
AES Encryption
AES is short for Advanced Encryption Standard. It is the most common form of encryption method. AES is so common that a lot of devices have a hardware component dedicated specifically to performing AES related operations. For some devices that don't, AES operations is a little slow to execute on them.
In the normal AES algorithm, a plaintext is divided into chunks of 128 bit blocks. If the data cannot be properly divided by 128, then the last block is padded accordingly. Then we encrypt each individual block. Then the encrypted blocks are combined to form the final cipher text.
Now let's look into what exactly happens during the encryption process. Let's take one block for illustration purpose.
We have our block comprising exactly 128 bits. The block is arranged into a 4x4 matrix with each cell occupying 1 byte. We are now going to XOR this matrix with a matrix created from the round key of exactly 128 bit in size. A round key is derived from our secret key. However, XOR-ing our data is not enough.
Step 2 is byte substitution. We use a pre-computed lookup table called an S-box. We use this S-box to substitute each byte in each of the cipher text cells. To read more about byte-substitution please read more. Byte substitution adds confusion to our cipher text.
In step 3, we shift rows. We exclude the first row in our matrix. For the remaining rows, we cyclically shift bytes to th left. The second row is shifted 1 byte to the left. The third row - 2 bytes to the left. And the final row - 3 bytes to the left.
In step 4, we mix the columns. We perform some mathematical calculations to perform column mixing. Mixing columns is designed in such a way that changing a single bit in the input spreads across multiple bits in the output bits. This creates an avalanche effect, meaning that changing even a few bits can create a whole different set of cipher texts. If you want a more mathematical explanation of the column mixing algorithm, I recommend you to read through this - Rijndael Mix Columns.
Both shift rows and mix columns combined together add diffusion.
In the final step we XOR with the round key, and repeat the steps from step 2 to this final step N number of rounds. Following list specifies the number of rounds for each encryption based on the key size:
- AES-128 - 10 rounds
- AES-192 - 12 rounds
- AES-256 - 14 rounds
The key size is only used to determine the number of rounds. The block size always remains 128 bit.
Additionally, we skip mixing the columns in the last round.
Here is a diagram to walk you through the process of encryption to help you understand if you still have any doubts lingering in your mind.
The plaintext is split into 128-bit blocks, and the last block is padded. We follow one block: 16 bytes.
This was a short introduction to cryptography and an in-depth explanation of OPT and AES symmetric key encryption algorithms. There are other symmetric key encryption algorithms that we did not discuss. However, this article should give you enough context and basic mental model to explore other encryption algorithms. This is it for this article.
In the following article, we will discuss about the different modes of AES, why the ECB mode is an insecure mode of encryption compared to other better modes we have and why breaking AES is theoretically possible but practically not feasible.
Thank you for reading!