Sure, here is the code for each step:
- Key Expansion Function:
The key expansion function expands the original key into an array of subkeys for each round of AES encryption.
def key_expansion(key):
"""
Expands the original key into an array of subkeys for each round of AES encryption.
"""
# Rcon for each round
Rcon = [
[0x01, 0x00, 0x00, 0x00],
[0x02, 0x00, 0x00, 0x00],
[0x04, 0x00, 0x00, 0x00],
[0x08, 0x00, 0x00, 0x00],
[0x10, 0x00, 0x00, 0x00],
[0x20, 0x00, 0x00, 0x00],
[0x40, 0x00, 0x00, 0x00],
[0x80, 0x00, 0x00, 0x00],
[0x1b, 0x00, 0x00, 0x00],
[0x36, 0x00, 0x00, 0x00]
]
# Number of rounds for AES (depends on key size)
Nr = len(key)//4 + 6
# Number of columns (always 4 for AES)
Nb = 4
# Number of subkeys (equal to (Nr+1)*Nb)
Nk = len(key)//4
# Initialize subkey array
w = [0] * Nb * (Nr + 1)
# Copy original key into first Nk words of subkey array
for i in range(Nk):
w[i] = [key[4*i], key[4*i+1], key[4*i+2], key[4*i+3]]
# Generate remaining words of subkey array
for i in range(Nk, Nb*(Nr+1)):
temp = w[i-1]
if i % Nk == 0:
temp = [temp[1], temp[2], temp[3], temp[0]]
temp = [sbox[x] for x in temp]
temp[0] ^= Rcon[i//Nk-1][0]
elif Nk > 6 and i % Nk == 4:
temp = [sbox[x] for x in temp]
w[i] = [w[i-Nk][j]^temp[j] for j in range(4)]
return w
- SubByte Function:
The SubByte function applies an S-Box, which is a substitution table that replaces each byte of the input with another specific byte.
def sub_bytes(state):
"""
Applies the S-Box, which is a substitution table that replaces each byte of the input with another specific byte.
"""
return [[sbox[state[i][j]] for j in range(len(state[i]))] for i in range(len(state))]
- ShiftRows Function:
The ShiftRows function shifts the rows of the state matrix.
def shift_rows(state):
"""
Shifts the rows of the state matrix.
"""
for i in range(1, len(state)):
state[i] = state[i][i:] + state[i][:i]
return state
- MixColumns Function:
The MixColumns function performs matrix multiplication with a fixed matrix on the state matrix.
def mix_columns(state):
"""
Performs matrix multiplication with a fixed matrix on the state matrix.
"""
for i in range(len(state)):
s0, s1, s2, s3 = state[i]
state[i][0] = gmul(0x02, s0) ^ gmul(0x03, s1) ^ s2 ^ s3
state[i][1] = s0 ^ gmul(0x02, s1) ^ gmul(0x03, s2) ^ s3
state[i][2] = s0 ^ s1 ^ gmul(0x02, s2) ^ gmul(0x03, s3)
state[i][3] = gmul(0x03, s0) ^ s1 ^ s2 ^ gmul(0x02, s3)
return state
- AddRoundKey Function:
The AddRoundKey function XOR's the current state matrix with a key schedule generated through the key expansion function.
def add_round_key(state, key_schedule):
"""
XOR's the current state matrix with a key schedule generated through the key expansion function.
"""
for i in range(len(state)):
for j in range(len(state[i])):
state[i][j] ^= key_schedule[i][j]
return state
- Encryption and Decryption Function:
def aes_encrypt(plaintext, key):
"""
Encrypt the plaintext using the AES algorithm with key.
"""
# Number of rounds for AES (depends on key size)
Nr = len(key)//4 + 6
# Number of columns (always 4 for AES)
Nb = 4
# Convert plaintext and key to state matrix
state = [[plaintext[4*j+i] for i in range(4)] for j in range(len(plaintext)//4)]
key_schedule = key_expansion(key)
# AddRoundKey initial round
state = add_round_key(state, key_schedule[:Nb])
# Nr-1 full rounds
for r in range(1, Nr):
state = sub_bytes(state)
state = shift_rows(state)
state = mix_columns(state)
state = add_round_key(state, key_schedule[Nb*r:Nb*(r+1)])
# Final round (no MixColumns)
state = sub_bytes(state)
state = shift_rows(state)
state = add_round_key(state, key_schedule[Nb*Nr:])
# Convert state matrix to ciphertext
ciphertext = [state[j][i] for i in range(4) for j in range(Nb)]
return bytes(ciphertext)
def aes_decrypt(ciphertext, key):
"""
Decrypt the ciphertext using the AES algorithm with key.
"""
# Number of rounds for AES (depends on key size)
Nr = len(key)//4 + 6
# Number of columns (always 4 for AES)
Nb = 4
# Convert ciphertext and key to state matrix
state = [[ciphertext[4*j+i] for i in range(4)] for j in range(len(ciphertext)//4)]
key_schedule = key_expansion(key)
# AddRoundKey initial round
state = add_round_key(state, key_schedule[Nb*Nr:])
# Nr-1 full rounds
for r in range(Nr-1, 0, -1):
state = shift_rows(state)
state = sub_bytes(state)
state = add_round_key(state, key_schedule[Nb*r:Nb*(r+1)])
state = mix_columns(state)
# Final round (no MixColumns)
state = shift_rows(state)
state = sub_bytes(state)
state = add_round_key(state, key_schedule[:Nb])
# Convert state matrix to plaintext
plaintext = [state[j][i] for i in range(4) for j in range(Nb)]
return bytes(plaintext)
- Testing:
plaintext = b'This is a test message for AES encryption.'
key = b'abcdefghijklmnop' # 128-bit key
ciphertext = aes_encrypt(plaintext, key)
assert aes_decrypt(ciphertext, key) == plaintext
This should output True if the implementation is correct.