将输入的4 X 4的矩阵左乘一个给定的4 X 4矩阵
以下为2乘以一个数和3乘以一个数的计算公式(这里的乘法并不是我们所认知的乘法)
#include <iostream>
/*
矩阵转换
1.old_data[]:将待转换的数据传入
2. new_data[4][4]:转换后的矩阵
*/
void matrix_exchange(unsigned char old_data[16], unsigned char new_data[4][4])
{
unsigned char i = 0;
for(i = 0; i < 16; i++)
{
new_data[i & 3][i >> 2] = old_data[i];//为了提高程序运行速度,使用位操作的方式
//new_data[i % 4][i / 4] = old_data[i];
}
//验证转换后的矩阵
#if 1
for(i = 0; i < 4; i++)
{
unsigned char j = 0;
for(j = 0; j < 4; j++)
{
printf("%d ", new_data[i][j]);
}
printf("\n");
}
#endif
}
/*
初始变换
将明文和秘钥进行异或,得到最终初始变换的数据
*/
void origin_change(unsigned char old_data[16], unsigned char old_key[16], unsigned char change_data[4][4])
{
unsigned char new_key[4][4] = {0};
unsigned char new_data[4][4] = {0};
unsigned char i = 0, j = 0;
printf("明文矩阵变换:\n");
matrix_exchange(old_data, new_data);
printf("\n");
printf("秘钥矩阵变换:\n");
matrix_exchange(old_key, new_key);
printf("\n");
printf("明文矩阵和秘钥矩阵异或之后的结果:\n");
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
change_data[i][j] = new_key[i][j] ^ new_data[i][j];
printf("%d ", change_data[i][j]);
}
printf("\n");
}
}
/*
字节替换
*/
unsigned char arr[16][16] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
};
void byte_change(unsigned char change_data[4][4], unsigned char byte_change_data[4][4])
{
unsigned char i = 0, j = 0;
printf("字节替换后的结果:\n");
for(i = 0; i < 4; i++)
{
for(j = 0; j <4; j++)
{
byte_change_data[i][j] = arr[change_data[i][j] >> 4][change_data[i][j] & 0x0f];
printf("%d ", byte_change_data[i][j]);
}
printf("\n");
}
}
/*
行移位
*/
void shitf_rows(unsigned char byte_change_data[4][4], unsigned char shitf_rows_data[4][4])
{
unsigned char i = 0, j = 0;
unsigned char temp = 0;
for(i = 0; i < 4; i++)
{
switch(i)
{
case 0:
shitf_rows_data[0][0] = byte_change_data[0][0];
shitf_rows_data[0][1] = byte_change_data[0][1];
shitf_rows_data[0][2] = byte_change_data[0][2];
shitf_rows_data[0][3] = byte_change_data[0][3];
break;
case 1:
shitf_rows_data[1][0] = byte_change_data[1][1];
shitf_rows_data[1][1] = byte_change_data[1][2];
shitf_rows_data[1][2] = byte_change_data[1][3];
shitf_rows_data[1][3] = byte_change_data[1][0];
break;
case 2:
shitf_rows_data[2][0] = byte_change_data[2][2];
shitf_rows_data[2][1] = byte_change_data[2][3];
shitf_rows_data[2][2] = byte_change_data[2][0];
shitf_rows_data[2][3] = byte_change_data[2][1];
break;
case 3:
shitf_rows_data[3][0] = byte_change_data[3][3];
shitf_rows_data[3][1] = byte_change_data[3][0];
shitf_rows_data[3][2] = byte_change_data[3][1];
shitf_rows_data[3][3] = byte_change_data[3][2];
break;
default:
break;
}
}
printf("行移位后的结果:\n");
for(i = 0; i < 4; i++)
{
for(j = 0; j <4; j++)
{
printf("%d ", shitf_rows_data[i][j]);
}
printf("\n");
}
}
/*
列混合
*/
const unsigned char mix_col_arr[4][4] =
{
2, 3, 1, 1,
1, 2, 3, 1,
1, 1, 2, 3,
3, 1, 1, 2
};
void mix_columns(unsigned char shitf_rows_data[4][4], unsigned char mix_columns_data[4][4])
{
unsigned char j = 0, i = 0;
//计算s'0,j
for(j = 0; j < 4; j++)
{
unsigned char item1 = 0;
unsigned char item2 = 0;
if(shitf_rows_data[0][j] >> 7)//2 * s0,j
{
item1 = (shitf_rows_data[0][j] << 1) ^ 0x1B; //a7 = 1, (a6a5a4a3a2a1a0)^ (00011011)
}
else
{
item1 = (shitf_rows_data[0][j] << 1); //a7 = 0, (a6a5a4a3a2a1a0)
}
if((shitf_rows_data[1][j] >> 7) == 1)//3* s1,j
{
item2 = ((shitf_rows_data[1][j] << 1) ^ 0x1B) ^ (shitf_rows_data[1][j]);
}
else
{
item2 = (shitf_rows_data[1][j] << 1) ^ (shitf_rows_data[1][j]);
}
mix_columns_data[0][j] = item1 ^ item2 ^ shitf_rows_data[2][j] ^ shitf_rows_data[3][j];
}
//计算s'1,j
for(j = 0; j < 4; j++)
{
unsigned char item2 = 0;
unsigned char item3 = 0;
if(shitf_rows_data[1][j] >> 7)//2 * s1,j
{
item2 = (shitf_rows_data[1][j] << 1) ^ 0x1B; //a7 = 1, (a6a5a4a3a2a1a0)^ (00011011)
}
else
{
item2 = (shitf_rows_data[1][j] << 1); //a7 = 0, (a6a5a4a3a2a1a0)
}
if((shitf_rows_data[2][j] >> 7) == 1)//3* s2,j
{
item3 = ((shitf_rows_data[2][j] << 1) ^ 0x1B) ^ (shitf_rows_data[2][j]);
}
else
{
item3 = (shitf_rows_data[2][j] << 1) ^ (shitf_rows_data[2][j]);
}
mix_columns_data[1][j] = shitf_rows_data[0][j] ^ item2 ^ item3 ^ shitf_rows_data[3][j];
}
//计算s'2,j
for(j = 0; j < 4; j++)
{
unsigned char item3 = 0;
unsigned char item4 = 0;
if(shitf_rows_data[2][j] >> 7)//2 * s2,j
{
item3 = (shitf_rows_data[2][j] << 1) ^ 0x1B; //a7 = 1, (a6a5a4a3a2a1a0)^ (00011011)
}
else
{
item3 = (shitf_rows_data[2][j] << 1); //a7 = 0, (a6a5a4a3a2a1a0)
}
if(shitf_rows_data[3][j] >> 7)//3* s3,j
{
item4 = ((shitf_rows_data[3][j] << 1) ^ 0x1B) ^ (shitf_rows_data[3][j]);
}
else
{
item4 = (shitf_rows_data[3][j] << 1) ^ (shitf_rows_data[3][j]);
}
mix_columns_data[2][j] = shitf_rows_data[0][j] ^ shitf_rows_data[1][j] ^ item3 ^ item4;
}
//计算s'3,j
for(j = 0; j < 4; j++)
{
unsigned char item1 = 0;
unsigned char item4 = 0;
if(shitf_rows_data[3][j] >> 7)//2 * s3,j
{
item4 = (shitf_rows_data[3][j] << 1) ^ 0x1B; //a7 = 1, (a6a5a4a3a2a1a0)^ (00011011)
}
else
{
item4 = (shitf_rows_data[3][j] << 1); //a7 = 0, (a6a5a4a3a2a1a0)
}
if(shitf_rows_data[0][j] >> 7)//3* s0,j
{
item1 = ((shitf_rows_data[0][j] << 1) ^ 0x1B) ^ (shitf_rows_data[0][j]);
}
else
{
item1 = (shitf_rows_data[0][j] << 1) ^ (shitf_rows_data[0][j]);
}
mix_columns_data[3][j] = item1 ^ shitf_rows_data[1][j] ^ shitf_rows_data[2][j] ^ item4;
}
printf("列混合:\n");
for(i = 0; i < 4; i++)
{
for(j = 0; j <4; j++)
{
printf("0x%x ", mix_columns_data[i][j]);
}
printf("\n");
}
}
int main()
{
unsigned char old_data[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
unsigned char old_key[16] = {11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26};
unsigned char change_data[4][4] = {0};
unsigned char byte_change_data[4][4] = {0};
unsigned char shitf_rows_data[4][4]= {0};
/*
列混合数组,视频中给定的数组
*/
unsigned char old_mix_arr[4][4] =
{
0xd4, 0xe0, 0xb8, 0x1e,
0xbf, 0xb4, 0x41, 0x27,
0x5d, 0x52, 0x11, 0x98,
0x30, 0xae, 0xf1, 0xe5
};
unsigned char mix_arr[4][4];
#if 0
origin_change(old_data, old_key, change_data);
printf("\n");
byte_change(change_data, byte_change_data);
printf("\n");
shitf_rows(byte_change_data, shitf_rows_data);
#endif
printf("\n");//列混合
mix_columns(old_mix_arr, mix_arr);
while(1);
}