资源描述:
《AES算法C语言讲解与实现》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、字节替换VoidAES::SubBytes(unsignedcharstate[][4]){Intr,c;for(r=0;r<4;r++){for(c=0;c<4;c++){state[r][c]=Sbox[state[r][c]];}}}行移位VoidAES::ShiftRows(unsignedcharstate[][4]){unsignedchar t[4];Intr,c;for(r=1;r<4;r++){for(c=0;c<4;c++){t[c]=state[r][(c+r)%4];}for(c=0;c<4;c++){state[r][
2、c]=t[c];}}}列混淆VoidAES::MixColumns(unsignedcharstate[][4]){unsignedchart[4];Intr,c;for(c=0;c<4;c++){for(r=0;r<4;r++){t[r]=state[r][c];}for(r=0;r<4;r++){state[r][c]=FFmul(0x02,t[r])^FFmul(0x03,t[(r+1)%4])^FFmul(0x01,t[(r+2)%4])^FFmul(0x01,t[(r+3)%4]);}}}unsignedcharAES::FFmul(
3、unsignedchara,unsignedcharb){unsignedcharbw[4];unsignedcharres=0;Inti;bw[0]=b;for(i=1;i<4;i++){bw[i]=bw[i-1]<<1;if(bw[i-1]&0x80){bw[i]^=0x1b;}}for(i=0;i<4;i++){if((a>>i)&0x01){res^=bw[i];}}Returnres;}轮密钥加算法VoidAES::AddRoundKey(unsignedcharstate[][4],unsignedchark[][4]){Intr,
4、c;for(c=0;c<4;c++){for(r=0;r<4;r++){state[r][c]^=k[r][c];}}}密钥扩展算法VoidAES::KeyExpansion(unsignedchar*key,unsignedcharw[][4][4]){Inti,j,r,c;unsignedcharrc[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36};for(r=0;r<4;r++){for(c=0;c<4;c++){w[0][r][c]=key[r+c*4];}}for(i=1;i
5、<=10;i++){for(j=0;j<4;j++){unsignedchart[4];for(r=0;r<4;r++){t[r]=j?w[i][r][j-1]:w[i-1][r][3];}if(j==0){unsignedchartemp=t[0];for(r=0;r<3;r++){t[r]=Sbox[t[(r+1)%4]];}t[3]=Sbox[temp];t[0]^=rc[i-1];}for(r=0;r<4;r++){w[i][r][j]=w[i-1][r][j]^t[r];}}}}解密过程(只有逆字节替换,逆行移位,逆列混淆)VoidA
6、ES::InvSubBytes(unsignedcharstate[][4]){Intr,c;for(r=0;r<4;r++){for(c=0;c<4;c++){state[r][c]=InvSbox[state[r][c]];}}}VoidAES::InvShiftRows(unsignedcharstate[][4]){unsignedchart[4];Intr,c;for(r=1;r<4;r++){for(c=0;c<4;c++){t[c]=state[r][(c-r+4)%4];}for(c=0;c<4;c++){state[r][c]
7、=t[c];}}}VoidAES::InvMixColumns(unsignedcharstate[][4]){unsignedchart[4];Intr,c;for(c=0;c<4;c++){for(r=0;r<4;r++){t[r]=state[r][c];}for(r=0;r<4;r++){state[r][c]=FFmul(0x0e,t[r])^FFmul(0x0b,t[(r+1)%4])^FFmul(0x0d,t[(r+2)%4])^FFmul(0x09,t[(r+3)%4]);}}}整体加密过程unsignedchar*AES::C
8、ipher(unsignedchar*input){unsignedcharstate[4][4];inti,r,c;for(r=0;r<4;r++)