AES Encryption

Advanced Encryption Standard - Interactive Learning Experience

šŸ” AES Structure Overview

The Advanced Encryption Standard (AES) is a symmetric encryption algorithm that operates on 128-bit blocks of data. It uses a substitution-permutation network structure with multiple rounds of transformation.

Block Size

AES always works with 128-bit blocks (16 bytes), arranged in a 4Ɨ4 matrix called the State.

Sā‚€,ā‚€
Sā‚€,₁
Sā‚€,ā‚‚
Sā‚€,ā‚ƒ
S₁,ā‚€
S₁,₁
S₁,ā‚‚
S₁,ā‚ƒ
Sā‚‚,ā‚€
Sā‚‚,₁
Sā‚‚,ā‚‚
Sā‚‚,ā‚ƒ
Sā‚ƒ,ā‚€
Sā‚ƒ,₁
Sā‚ƒ,ā‚‚
Sā‚ƒ,ā‚ƒ

Key Sizes

AES supports three key sizes:

  • AES-128: 128-bit key, 10 rounds
  • AES-192: 192-bit key, 12 rounds
  • AES-256: 256-bit key, 14 rounds

āš™ļø Transformation Functions

AES uses four main transformation functions in each round:

1. SubBytes Transformation

Each byte in the state is replaced with a corresponding byte from the S-box (substitution box). This provides non-linearity to the cipher.

for i in range(4):
    for j in range(4):
        state[i][j] = S_BOX[state[i][j]]

2. ShiftRows Transformation

The rows of the state are cyclically shifted. Row 0 is not shifted, row 1 is shifted left by 1, row 2 by 2, and row 3 by 3.

# Row 0: no shift
# Row 1: shift left by 1
# Row 2: shift left by 2
# Row 3: shift left by 3
temp = state[1][0] state[1] = [state[1][1], state[1][2], state[1][3], temp]

3. MixColumns Transformation

Each column is multiplied by a fixed polynomial in GF(2⁸). This provides diffusion by mixing the bytes within each column.

# Matrix multiplication in GF(2^8)
# Each column is multiplied by:
    # [2, 3, 1, 1]
    # [1, 2, 3, 1]
    # [1, 1, 2, 3]
    # [3, 1, 1, 2]

4. AddRoundKey Transformation

The state is XORed with the round key derived from the original key through key expansion.

for i in range(4):
     for j in range(4):
        state[i][j] ^= round_key[i][j]

šŸ”‘ Key Expansion

The key expansion algorithm generates round keys from the original cipher key. For AES-128, it produces 11 round keys (including the initial key).

Interactive Key Expansion Demo

šŸ’” Interactive AES Example

Encrypt/Decrypt Demo

šŸ’» Implementation

Here's a simplified implementation of AES core functions:

// S-Box for SubBytes transformation
const S_BOX = [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
     0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
     // ... (full S-box would be 256 bytes)];
// SubBytes transformation
function subBytes(state) {
     for (let i = 0; i < 4; i++) {
         for (let j = 0; j < 4; j++) {
             state[i][j] = S_BOX[state[i][j]];
         }
    }
}
// ShiftRows transformation
function shiftRows(state) {
     let temp;
     // Row 1: shift left by 1
     temp = state[1][0];
     state[1][0] = state[1][1];
     state[1][1] = state[1][2];
     state[1][2] = state[1][3];
     state[1][3] = temp;
     // Row 2: shift left by 2
     temp = state[2][0];
     state[2][0] = state[2][2];
     state[2][2] = temp;
     temp = state[2][1];
     state[2][1] = state[2][3];
     state[2][3] = temp;
     // Row 3: shift left by 3
     temp = state[3][0];
     state[3][0] = state[3][3];
     state[3][3] = state[3][2];
     state[3][2] = state[3][1];
     state[3][1] = temp;
}
// AddRoundKey transformation
function addRoundKey(state, roundKey) {
     for (let i = 0; i < 4; i++) {
         for (let j = 0; j < 4; j++) {
             state[i][j] ^= roundKey[i][j];
         }
     }
}