CHƯƠNG 3: XÂY DỰNG CHƯƠNG TRÌNH THỰC THI MÃ KHỐI AES CẢI TIẾN VỚI MA TRẬN MDS KÍCH THƯỚC LỚN
Tải bản đầy đủ - 0trang
Sau khi tải thành cơng ta có file chạy như dưới đây:
Hình 3.2: File download hiển thị trong thư mục sau khi tải thành cơng
Giờ ta double click vào Vs_enterprise
Hình 3.3:Tiến hành chạy file
Tùy vào tốc độ của máy cũng như đường truyền mạng, sau khi tải xong
ta sẽ có màn hình như dưới đây:
40
Hình 3.4: Màn hình hiển thị sau khi chạy file thành công
Tùy vào nhu cầu lập trình mà ta sẽ chọn các gói khác nhau. Theo kinh nghiệm làm
Project thì Tui thấy đa phần chúng ta sẽ triển khai các dự án phổ biến dưới đây (nên tick
vào) tùy vào dung lượng ổ cứng, RAM, và các cấu hình khác nhau.
Sau đó ta bắt đầu bấm Install để cài đặt:
Hình 3.5: Tiến hành cài đặt
41
Sau khi cài đặt thành cơng, màn hình lúc khởi động Visual Studio 2019 sẽ hiển thị
như sau:
Hình 3.6: Giao diện Visual Studio sau khi cài đặt thành công
Khi khởi động xong, ta có màn hình dưới đây (nó khá khác biệt với các version cũ):
Hình 3.7: Màn hình hiển thị Visual Studio 2019
42
Ở màn hình trên ta có 3 lựa chọn chính:
+) Danh sách bên trái: chọn mở những Project cũ (cái này nếu bạn đã tạo trước đó
hoặc kể cả ở các Version cũ nó cũng tự động hiển thị lên đây cho bạn). Muốn mở lại thì
click vào nó là ok
+) Chọn Continue without code: Chọn mục này sẽ mở Visual Studio mà khơng có
Project nào cả
+) Chọn “Create a New Project” : Chương trình sẽ hiển thị màn hình tạo Project
mới.
Hình 3.8: Màn hình tạo Project mới
+) Trong mục Language chọn C#
+) Mục Platform chọn Windows
+) Mục Project Type chọn Desktop
Sau đó bấn next, đặt tên cho dự án sau đó bấm Create để tạo Project:
43
Hình 3.9: Màn hình Project sau khi được tạo mới
3.2. Thiết kế chương trình
Chương trình thực thi mã khối AES được thiết kế theo sơ đồ chức năng dưới đây:
Hình 3.10: Sơ đồ phân rã chức năng của chương trình
44
3.3. Cài đặt các Modul
Dưới đây là các modul của quy trình mã hóa, giải mã và tốc độ mã của thuật toán
AES.
+) Modul SubBytes() và InvSubBytes() giống ở cả AES gốc và AES cải tiến.
Modul SubBytes():
void SubBytes(unsigned char state[4][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[i][j] = sbox[state[i][j]];
}
}
}
Modul InvSubBytes():
void InvSubBytes(unsigned char state[4][4])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[i][j] = rsbox[state[i][j]];
}
}
}
+) Modul ShifRows() và InvSubBytes() giống ở cả AES gốc và AES cải tiến
Modul ShifRows():
void ShiftRows(unsigned char state[4][4])
{
unsigned char temp;
45
// Rotate first row 1 columns to left
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;
// Rotate second row 2 columns to left
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;
// Rotate third row 3 columns to left
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;
}
Modul InvShifRows()
void InvShiftRows(unsigned char state[4][4])
{
unsigned char temp;
// Rotate first row 1 columns to right
temp=state[1][3];
state[1][3]=state[1][2];
state[1][2]=state[1][1];
state[1][1]=state[1][0];
state[1][0]=temp;
// Rotate second row 2 columns to right
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;
// Rotate third row 3 columns to right
temp=state[3][0];
state[3][0]=state[3][1];
46
state[3][1]=state[3][2];
state[3][2]=state[3][3];
state[3][3]=temp;
}
+) Modul MixColumns() và InvMixColumns() AES cải tiến cũng giống với AES
gốc, chỉ khác là thay ma trận MDS thành ma trận MDS 8x8 và cần sửa đổi lại cấu trúc của
phép nhân ma trận với bảng trạng thái như trình bày trong chương 2.
Modul MixColumns() với phép tra bảng 256x256:
void MixColumns(unsigned char state[4][4], unsigned char M[8][8], int
poly)
{
//int i;
unsigned char a,b,c,d,e, f, g, h;
//for(i=0;i<4;i=i+2) {
a = state[0][0];
b = state[1][0];
c = state[2][0];
d = state[3][0];
e = state[0][1];
f = state[1][1];
g = state[2][1];
h = state[3][1];
state[0][0] = gf.mul_table[a][M[0][0]] ^ gf.mul_table[b][M[0][1]] ^
gf.mul_table[c][M[0][2]] ^ gf.mul_table[d][M[0][3]]^
gf.mul_table[e][M[0][4]] ^ gf.mul_table[f][M[0][5]] ^
gf.mul_table[g][M[0][6]] ^ gf.mul_table[h][M[0][7]];
state[1][0] = gf.mul_table[a][M[1][0]] ^ gf.mul_table[b][M[1][1]] ^
gf.mul_table[c][M[1][2]] ^ gf.mul_table[d][M[1][3]]^
gf.mul_table[e][M[1][4]] ^ gf.mul_table[f][M[1][5]] ^
gf.mul_table[g][M[1][6]] ^ gf.mul_table[h][M[1][7]];
state[2][0] = gf.mul_table[a][M[2][0]] ^ gf.mul_table[b][M[2][1]] ^
gf.mul_table[c][M[2][2]] ^ gf.mul_table[d][M[2][3]]^
gf.mul_table[e][M[2][4]] ^ gf.mul_table[f][M[2][5]] ^
gf.mul_table[g][M[2][6]] ^ gf.mul_table[h][M[2][7]];
47
state[3][0] = gf.mul_table[a][M[3][0]] ^ gf.mul_table[b][M[3][1]] ^
gf.mul_table[c][M[3][2]] ^ gf.mul_table[d][M[3][3]]^
gf.mul_table[e][M[3][4]] ^ gf.mul_table[f][M[3][5]] ^
gf.mul_table[g][M[3][6]] ^ gf.mul_table[h][M[3][7]];
state[0][1] = gf.mul_table[a][M[4][0]] ^ gf.mul_table[b][M[4][1]] ^
gf.mul_table[c][M[4][2]] ^ gf.mul_table[d][M[4][3]]^
gf.mul_table[e][M[4][4]] ^ gf.mul_table[f][M[4][5]] ^
gf.mul_table[g][M[4][6]] ^ gf.mul_table[h][M[4][7]];
state[1][1] = gf.mul_table[a][M[5][0]] ^ gf.mul_table[b][M[5][1]] ^
gf.mul_table[c][M[5][2]] ^ gf.mul_table[d][M[5][3]]^
gf.mul_table[e][M[5][4]] ^ gf.mul_table[f][M[5][5]] ^
gf.mul_table[g][M[5][6]] ^ gf.mul_table[h][M[5][7]];
state[2][1] = gf.mul_table[a][M[6][0]] ^ gf.mul_table[b][M[6][1]] ^
gf.mul_table[c][M[6][2]] ^ gf.mul_table[d][M[6][3]]^
gf.mul_table[e][M[6][4]] ^ gf.mul_table[f][M[6][5]] ^
gf.mul_table[g][M[6][6]] ^ gf.mul_table[h][M[6][7]];
state[3][1] = gf.mul_table[a][M[7][0]] ^ gf.mul_table[b][M[7][1]] ^
gf.mul_table[c][M[7][2]] ^ gf.mul_table[d][M[7][3]]^
gf.mul_table[e][M[7][4]] ^ gf.mul_table[f][M[7][5]] ^
gf.mul_table[g][M[7][6]] ^ gf.mul_table[h][M[7][7]];
a = state[0][2];
b = state[1][2];
c = state[2][2];
d = state[3][2];
e = state[0][3];
f = state[1][3];
g = state[2][3];
h = state[3][3];
state[0][2] = gf.mul_table[a][M[0][0]] ^ gf.mul_table[b][M[0][1]] ^
gf.mul_table[c][M[0][2]] ^ gf.mul_table[d][M[0][3]]^
gf.mul_table[e][M[0][4]] ^ gf.mul_table[f][M[0][5]] ^
gf.mul_table[g][M[0][6]] ^ gf.mul_table[h][M[0][7]];
state[1][2] = gf.mul_table[a][M[1][0]] ^ gf.mul_table[b][M[1][1]] ^
gf.mul_table[c][M[1][2]] ^ gf.mul_table[d][M[1][3]]^
gf.mul_table[e][M[1][4]] ^ gf.mul_table[f][M[1][5]] ^
48
gf.mul_table[g][M[1][6]] ^ gf.mul_table[h][M[1][7]];
state[2][2] = gf.mul_table[a][M[2][0]] ^ gf.mul_table[b][M[2][1]] ^
gf.mul_table[c][M[2][2]] ^ gf.mul_table[d][M[2][3]]^
gf.mul_table[e][M[2][4]] ^ gf.mul_table[f][M[2][5]] ^
gf.mul_table[g][M[2][6]] ^ gf.mul_table[h][M[2][7]];
state[3][2] = gf.mul_table[a][M[3][0]] ^ gf.mul_table[b][M[3][1]] ^
gf.mul_table[c][M[3][2]] ^ gf.mul_table[d][M[3][3]]^
gf.mul_table[e][M[3][4]] ^ gf.mul_table[f][M[3][5]] ^
gf.mul_table[g][M[3][6]] ^ gf.mul_table[h][M[3][7]];
state[0][3] = gf.mul_table[a][M[4][0]] ^ gf.mul_table[b][M[4][1]] ^
gf.mul_table[c][M[4][2]] ^ gf.mul_table[d][M[4][3]]^
gf.mul_table[e][M[4][4]] ^ gf.mul_table[f][M[4][5]] ^
gf.mul_table[g][M[4][6]] ^ gf.mul_table[h][M[4][7]];
state[1][3] = gf.mul_table[a][M[5][0]] ^ gf.mul_table[b][M[5][1]] ^
gf.mul_table[c][M[5][2]] ^ gf.mul_table[d][M[5][3]]^
gf.mul_table[e][M[5][4]] ^ gf.mul_table[f][M[5][5]] ^
gf.mul_table[g][M[5][6]] ^ gf.mul_table[h][M[5][7]];
state[2][3] = gf.mul_table[a][M[6][0]] ^ gf.mul_table[b][M[6][1]] ^
gf.mul_table[c][M[6][2]] ^ gf.mul_table[d][M[6][3]]^
gf.mul_table[e][M[6][4]] ^ gf.mul_table[f][M[6][5]] ^
gf.mul_table[g][M[6][6]] ^ gf.mul_table[h][M[6][7]];
state[3][3] = gf.mul_table[a][M[7][0]] ^ gf.mul_table[b][M[7][1]] ^
gf.mul_table[c][M[7][2]] ^ gf.mul_table[d][M[7][3]]^
gf.mul_table[e][M[7][4]] ^ gf.mul_table[f][M[7][5]] ^
gf.mul_table[g][M[7][6]] ^ gf.mul_table[h][M[7][7]];
}
Modul MixColumns() với phép nhân thông thường, phép nhân Xtime, tạo bảng
64x256 chỉ thay hàm gf.mul_table[a][M[i][j]] lần lượt thành MultAB_P(a, M[i][j], poly),
Multiply(a, M[i][j], poly), tm[0][a]
Modul InvMixColumns(): tương tự như modun MixColumns chỉ thay ma trận
MDS thành ma trận nghịch đảo InvMDS
49
Modul AddRoundKey():
void AddRoundKey(unsigned char state[4][4], int round)
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] ^= RoundKey[round * Nb * 4 + i * Nb + j];
}
}
}
Modun KeyExpansion():
void KeyExpansion()
{
int i,j;
unsigned char temp[4],k;
for(i=0;i
{
RoundKey[i*4]=Key[i*4];
RoundKey[i*4+1]=Key[i*4+1];
RoundKey[i*4+2]=Key[i*4+2];
RoundKey[i*4+3]=Key[i*4+3];
}
while (i < (Nb * (Nr+1)))
{
for(j=0;j<4;j++)
{
temp[j]=RoundKey[(i-1) * 4 + j];
}
if (i % Nk == 0)
{
{
k = temp[0];
temp[0] = temp[1];
temp[1] = temp[2];
temp[2] = temp[3];
temp[3] = k;
}
50