C语言AES列混合及逆列混合的实现
#include #include //函数声明unsigned char xtime (unsigned char input);voidmixcolum(unsigned char input[4][4], unsigned char output[4][4]);voidimixcolum(unsigned char input[4][4], unsigned char output[4][4]);//主函数int main() { //定义并对自己的学号进行填充unsigned char input[4][4] = {'2','0','1','0','1','2','1','0','5','8','0','0','0','0','0','0'}; unsigned char mixcolum_output[4][4]; unsigned char imixcolum_output[4][4]; inti, j; mixcolum(input,mixcolum_output); imixcolum(mixcolum_output,imixcolum_output); //以下代码是按格式输出:明文,列混合后的密文,逆列混合后恢复的明文printf("\n 明文是:\n\n"); for (i = 0; i < 4; i++) { for(j = 0; j < 4; j++) { printf("%c ",input[i][j]); } } printf("\n\n 列混合之后是:\n\n"); for (i = 0; i < 4; i++) { for(j = 0; j < 4; j++) { printf("%c ",mixcolum_output[i][j]); } } printf("\n\n 逆列混合之后是:\n\n"); for (i = 0; i < 4; i++) { for(j = 0; j < 4; j++) { printf("%c ",imixcolum_output[i][j]); } } printf("\n"); return 0; } //一下为三个函数 unsigned char xtime (unsigned char input) // x乘法('02'乘法) { int temp; temp = input<<1; //二进制串左移一位 if(input & 0x80) //判断如果相与如果b7==1,就执行与27相异或{ temp ^= 0x1b; } return temp; } void mixcolum(unsigned char input[4][4], unsigned char output[4][4]) //列混合算法 { inti, j; for(j=0; j< 4; j++) { for(i=0; i<4; i++) { output[i][j] = xtime(input[i%4][j]) //0x02乘法 ^ ( input[ ( i + 1 ) % 4][j] ^ xtime( input[ ( i + 1 ) % 4][j] ) ) //0x03乘法 ^ input[ ( i + 2 ) % 4][j] //0x01乘法 ^ input[ ( i + 3 ) % 4][j]; //0x01乘法 } } } void imixcolum(unsigned char input[4][4], unsigned char output[4][4]) //逆列混合 { inti, j; for(j=0; j< 4; j++) { for(i=0; i<4; i++) { output[i][j] = (xtime(xtime(xtime(input[i % 4][j]))) ^ xtime(xtime(input[i % 4][j]))^xtime(input[i % 4][j]))