Symmetric Encryption (AES basics)

Simple explanation

Francesco De Rosa
2 min readApr 15, 2024

The Problem to solve

Let’s answer the most basic question on cryptography out there : What if I have a key and I want to encrypt / decrypt my messages with it ? I Want a simple, fast and reliable method to do that

AES ( Advanced Encryption Standard )

The Strategy

From previous posts , we know how to convert normal human string to bytes ( number ) and viceversa . Now we need to build some kind of encryption that works with this converted bytes and can hide a message based on our key .

AES ( Advanced Encryption Standard ) solves this problem for us in a clever but not too trivial way

How AES Works

Assume we have a fixed length key of 16 bytes and a fixed length message of a n bytes where n is a multiples of 16 ( we discuss later why ) . We can now divide the message ( plaintext ) in m blocks of 16 bytes and put every block into a 4x4 matrix where in every cell there is one byte ( we can do the same with the key )

Now we have to scramble the plaintext based on key, to do so we can take every block of plaintext and start to make some operation that you study in algebra class like : XOR , Permutation and matrix product with every block of plaintext and the key . After that we obtain a ciphertext based on our key and decryptable just with our key !

The Role of MODE

When we encrypt something, we also need to specify not just the encryption method ( AES or DES ) but also the mode, this is needed in order to know how our encryption has to manage our block of ciphertext : if we want no interaction with blocks , we use ECB, if we want to scramble more the cipher and xor something , we use CBC and so on

Python Implementation

Output

( hex encoding )

plaintext 
4d692063686965646f20636f7361207369676e69666963686920696c206e756d65726f206e656c206e6f6d652064692071756573746f20616c676f7269746d6f2e
cipher
c619abfa265701f0a5cdf33949efc12c54a0a449479e662c2740dc63bf67d22f7e2f7adf119676140f59d17c24d25c69adccdc7354e14112cdd267185c54e7e2932d8f066254c10b90b5dab6617998f8

WHY cipher and plaintext does not have the same length, and we i need pad function ? We’ll discuss about that in the next post !

--

--