密码学课程设计
0x40, 0x80, 0x1b,
0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63,
0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb };
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]; 36 密码学课程设计 temp[3] = k; temp[0]=getSBoxValue(temp[0]); temp[1]=getSBoxValue(temp[1]); temp[2]=getSBoxValue(temp[2]); temp[3]=getSBoxValue(temp[3]); temp[0] = temp[0] ^ Rcon[i/Nk]; } else if (Nk > 6 && i % Nk == 4) { temp[0]=getSBoxValue(temp[0]); temp[1]=getSBoxValue(temp[1]); temp[2]=getSBoxValue(temp[2]); temp[3]=getSBoxValue(temp[3]); } RoundKey[i*4+0] = RoundKey[(i-Nk)*4+0] ^ temp[0]; RoundKey[i*4+1] = RoundKey[(i-Nk)*4+1] ^ temp[1]; RoundKey[i*4+2] = RoundKey[(i-Nk)*4+2] ^ temp[2]; RoundKey[i*4+3] = RoundKey[(i-Nk)*4+3] ^ temp[3]; i++; } } void AddRoundKey(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]; } void InvSubBytes() { int i,j; for(i=0;i<4;i++) for(j=0;j<4;j++) state[i][j] = getSBoxInvert(state[i][j]); } void InvShiftRows() { unsigned char temp; 37 密码学课程设计 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; 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; temp=state[3][0]; state[3][0]=state[3][1]; state[3][1]=state[3][2]; state[3][2]=state[3][3]; state[3][3]=temp; } void InvMixColumns() { int i; unsigned char a,b,c,d; for(i=0;i<4;i++) { a = state[0][i]; b = state[1][i]; c = state[2][i]; d = state[3][i]; state[0][i] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); state[1][i] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); state[2][i] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); state[3][i] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); } } void InvCipher() { 38 密码学课程设计 int i,j,round=0; for(i=0;i<4;i++) for(j=0;j<4;j++) state[j][i] = in[i*4 + j]; AddRoundKey(Nr); for(round=Nr-1;round>0;round--) { InvShiftRows(); InvSubBytes(); AddRoundKey(round); InvMixColumns(); } InvShiftRows(); InvSubBytes(); AddRoundKey(0); for(i=0;i<4;i++) { for(j=0;j<4;j++) { out[i*4+j]=state[j][i];} } } void SubBytes() { int i,j; for(i=0;i<4;i++) for(j=0;j<4;j++) state[i][j] = getSBoxValue(state[i][j]); } void ShiftRows() { unsigned char temp; 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; temp=state[2][0]; state[2][0]=state[2][2]; state[2][2]=temp; temp=state[2][1]; 39 密码学课程设计 state[2][1]=state[2][3]; state[2][3]=temp; 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; } #define xtime(x) ((x<<1) ^ (((x>>7) & 1) * 0x1b)) void MixColumns() { int i; unsigned char Tmp,Tm,t; for(i=0;i<4;i++) { t=state[0][i]; Tmp = state[0][i] ^ state[1][i] ^ state[2][i] ^ state[3][i] ; Tm = state[0][i] ^ state[1][i] ; Tm = xtime(Tm); state[0][i] ^= Tm ^ Tmp ; Tm = state[1][i] ^ state[2][i] ; Tm = xtime(Tm); state[1][i] ^= Tm ^ Tmp ; Tm = state[2][i] ^ state[3][i] ; Tm = xtime(Tm); state[2][i] ^= Tm ^ Tmp ; Tm = state[3][i] ^ t ; Tm = xtime(Tm); state[3][i] ^= Tm ^ Tmp ; } } void Cipher() { int i,j,round=0; for(i=0;i<4;i++) for(j=0;j<4;j++) state[j][i] = in[i*4 + j]; AddRoundKey(0); for(round=1;round SubBytes(); ShiftRows(); MixColumns(); AddRoundKey(round); } SubBytes(); ShiftRows(); AddRoundKey(Nr); 40