21BIT238
21BIT238
Submitted to
Department of ICT Engineering
School of Technology, Pandit Deendayal Energy University
ADVANCED ENCRYPTION ALGORITHM (AES)
Encryption
The AES encryption process involves several distinct steps performed in multiple rounds:
• SubBytes: During this step, each byte of the block undergoes a substitution process
using a predefined substitution box known as the S-box. This box is a fixed table where
each byte value (8 bits) is replaced by another value. The substitution is non-linear and
designed to add confusion, making it challenging for an observer to discern any patterns
between the plaintext and the resulting ciphertext. The S-box ensures a complex and
nonlinear relationship between the input and output bytes.
• ShiftRows: In this step, the rows of the block are shifted cyclically. Each row in the
block is shifted to the left by a certain number of bytes. The first row remains
unchanged, the second row shifts one position to the left, the third row shifts two
positions, and the fourth row shifts three positions. This transformation ensures that data
within each row is spread across different columns after encryption, further enhancing
diffusion and making patterns less evident.
• MixColumns: Here, the columns of the block undergo a mixing operation. Each column
is treated as a polynomial, and matrix multiplication in a finite field is performed. The
transformation involves multiplying each column by a fixed matrix (called the
MixColumns matrix) to create a new column. This operation introduces diffusion and
ensures that each byte in the block depends on multiple bytes from the previous round,
increasing the complexity of the encryption.
• AddRoundKey: For this step, a round key derived from the main encryption key is
XORed with each byte of the block. The round key is generated from the main key using
a process called the key schedule. Each round key is exclusive to a specific round of
encryption and is combined with the state of the block using bitwise XOR. This step
introduces confusion and ensures that each round of encryption is unique, contributing to
the security of the algorithm.
Decryption
AES decryption is the process of reversing the encryption steps to transform the ciphertext back
into the original plaintext. It involves using the same encryption key but applying the inverse
operations of AES encryption. Let's break down the decryption process step by step:
• Key Expansion (Inverse): Generate round keys from the main encryption key using the
key schedule in reverse order. Recreate the set of round keys used in each encryption
round.
• Initial Round (Inverse): Add the last round key (which was added in the final encryption
round) to the ciphertext block.
• Rounds (Iterative Process in Reverse): For each round (in reverse order compared to
encryption rounds):
o Inverse AddRoundKey: XOR each byte of the block with the corresponding
during encryption. This involves using the inverse of the MixColumns matrix. o
Inverse ShiftRows: Reverse the row shifting operation by shifting rows in the
opposite direction. o Inverse SubBytes: Reverse the substitution process by using
the inverse S-box to replace each byte with its original value.
• Final Round (No MixColumns, Inverse AddRoundKey): Perform the inverse SubBytes,
inverse ShiftRows, and inverse AddRoundKey operations, similar to the final round in
encryption. No MixColumns operation is performed in the final decryption round.
• Output: The final output after completing all rounds is the original plaintext.
• Round Keys: The round keys are used in reverse order compared to encryption.
• Final Round: Similar to encryption, the final round in decryption doesn't include the
MixColumns operation.
AES decryption, when performed correctly with the matching key, reverses the encryption
process and retrieves the original plaintext from the ciphertext. It's crucial to use the correct
decryption key, as using the wrong key will not yield the correct plaintext.
Code:
%% Input
input_arr=['64' '17' '6F' '28';'4F' '6E' '65' '10';'4E' '69' '6E' '85';'20' '54' '77'
'6E'];
disp("Input message:")
disp(input_arr) %%
Initializations
dec_key=randi([0,255],4,44);
s_table=randi([0,255],16,16);
mc_matrix=[2 3 1 1;1 2 3 1;1 1 2 3;3 1 1 2];
for i=1:4 for j=1:4
double_bin_mc_matrix(i,2*j-1:2*j)=dec2bin(mc_matrix(i,j),2);
bin_mc_matrix=char(double_bin_mc_matrix); end end
key_count=0;
%% Initial Add Round Key
for i=1:4 for j=1:4
state(i,j)=bitxor(dec_key(i,j+key_count),hex2dec(input_arr(i,2*j-
1:2*j)));
end end
key_count=key_count+4;
%% 9 Rounds for
rounds=1:9 %
Substitute Bytes for
i=1:4 for j=1:4
temp=dec2hex(state(i,j),2);
state(i,j)=s_table(hex2dec(temp(1))+1,hex2dec(temp(2))+1);
end
end
% Shift Rows
for i=1:4
for j=1:4
k=mod(j+(4-(i-1)),4);
if(k==0) k=4;
end
temp_state(i,k)=state(i,j);
end end
state=temp_state;
% Mix Columns
for i=1:4
for j=1:4
double_bin_state(i,8*j-7:8*j)=dec2bin(state(i,j),8);
bin_state=char(double_bin_state); end end for
i=1:4 for j=1:4
rem_reg=[0 0 0 0 0 0 0 0];
for k=1:4
temp=conv(reshape(str2num(char(num2cell(bin_state(k,8*j7:8*j)))),1,[]),res
hape(str2num(char(num2cell(bin_mc_matrix(i,2*k1:2*k)))),1,[]));
for n=1:length(temp)
temp(n)=mod(temp(n),2); end
[q,r]=deconv(temp,[1 0 0 0 1 1 0 1 1]);
m=1;
for p=length(r)-7:length(r)
r2(m)=mod(r(p),2);
m=m+1; end
rem_reg=bitxor(r2,rem_reg);
end
state(i,j)=bin2dec(num2str(rem_reg));
end
end
% Add Round Key
for i=1:4
for j=1:4
state(i,j)=bitxor(dec_key(i,j+key_count),hex2dec(input_arr(i,2*j-
1:2*j)));
end
end
key_count=key_count+4; end
%% 10th round
% Substitute Bytes
for i=1:4 for
j=1:4
temp=dec2hex(state(i,j),2);
state(i,j)=s_table(hex2dec(temp(1))+1,hex2dec(temp(2))+1);
end end
% Shift Rows
for i=1:4
for j=1:4
k=mod(j+(4-(i-1)),4);
if(k==0) k=4;
end
temp_state(i,k)=state(i,j);
end end
state=temp_state; %
Add Round Key
for i=1:4
for j=1:4
state(i,j)=bitxor(dec_key(i,j+key_count),hex2dec(input_arr(i,2*j-
1:2*j)));
end end
%% Output
disp(dec_key) disp(s_table)
ciphertext=dec2hex(state); disp("Encrypted
message:")
disp(reshape(ciphertext,4,8))
OUTPUT: