📄 aes.c
字号:
state[2][1]=state[2][3];
state[2][3]=k;
k=state[3][0];
state[3][0]=state[3][3];
state[3][3]=state[3][2];
state[3][2]=state[3][1];
state[3][1]=k;
//AddRoundKey:
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] ^= RoundKey[Nr * Nb * 4 + i * Nb + j];
}
}
// The encryption process is over.
// Copy the state array to output array.
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
out[i*4+j]=state[j][i];
}
}
return i;
}
//Notice: the length of the variables 'in' and 'out' is not great than 16.
int aes_decrypt(const unsigned char *in, unsigned char *out, const unsigned char *key)
{
int i,j,round=0;
unsigned char temp[5]={10,13,100,113,0},k;
unsigned char state[4][4];
// Expand the key:
strcpy(RoundKey,key);
strcat(RoundKey,temp);
i=strlen(RoundKey)/4;
while (i < (Nb * (Nr+1)))
{
for(j=0;j<4;j++)
{
temp[j]=RoundKey[(i-1) * 4 + j];
}
if (i % Nk == 0)
{
// This function rotates the 4 bytes in a word to the left once.
// [a2,a1,a2,a3] becomes [a1,a2,a3,a0]
// Function RotWord()
{
k = temp[0];
temp[0] = temp[1];
temp[1] = temp[2];
temp[2] = temp[3];
temp[3] = k;
}
// SubWord() is a function that takes a four-byte input word and
// applies the S-box to each of the four bytes to produce an output word.
// Function Subword()
{
temp[0]=sbox[temp[0]];
temp[1]=sbox[temp[1]];
temp[2]=sbox[temp[2]];
temp[3]=sbox[temp[3]];
}
temp[0] = temp[0] ^ Rcon[i/Nk];
}
else if (Nk > 6 && i % Nk == 4)
{
// Function Subword()
{
temp[0]=sbox[temp[0]];
temp[1]=sbox[temp[1]];
temp[2]=sbox[temp[2]];
temp[3]=sbox[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++;
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] = in[i*4 + j];
}
}
// Add the First round key to the state before starting the rounds.
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] ^= RoundKey[round * Nb * 4 + i * Nb + j];
}
}
// There will be Nr rounds.
// The first Nr-1 rounds are identical.
// These Nr-1 rounds are executed in the loop below.
for(round=Nr-1;round>0;round--)
{
//1.InvShiftRows:
k=state[1][3];
state[1][3]=state[1][2];
state[1][2]=state[1][1];
state[1][1]=state[1][0];
state[1][0]=k;
k=state[2][0];
state[2][0]=state[2][2];
state[2][2]=k;
k=state[2][1];
state[2][1]=state[2][3];
state[2][3]=k;
k=state[3][0];
state[3][0]=state[3][1];
state[3][1]=state[3][2];
state[3][2]=state[3][3];
state[3][3]=k;
//2.InvSubBytes:
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[i][j] = rsbox[state[i][j]];
}
}
//3.AddRoundKey:
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] ^= RoundKey[round * Nb * 4 + i * Nb + j];
}
}
//4.InvMixColumns:
for(i=0;i<4;i++)
{
temp[0] = state[0][i];
temp[1] = state[1][i];
temp[2] = state[2][i];
temp[3] = state[3][i];
state[0][i] = Multiply(temp[0], 0x0e) ^ Multiply(temp[1], 0x0b) ^ Multiply(temp[2], 0x0d) ^ Multiply(temp[3], 0x09);
state[1][i] = Multiply(temp[0], 0x09) ^ Multiply(temp[1], 0x0e) ^ Multiply(temp[2], 0x0b) ^ Multiply(temp[3], 0x0d);
state[2][i] = Multiply(temp[0], 0x0d) ^ Multiply(temp[1], 0x09) ^ Multiply(temp[2], 0x0e) ^ Multiply(temp[3], 0x0b);
state[3][i] = Multiply(temp[0], 0x0b) ^ Multiply(temp[1], 0x0d) ^ Multiply(temp[2], 0x09) ^ Multiply(temp[3], 0x0e);
}
}
//InvShiftRows:
k=state[1][3];
state[1][3]=state[1][2];
state[1][2]=state[1][1];
state[1][1]=state[1][0];
state[1][0]=k;
k=state[2][0];
state[2][0]=state[2][2];
state[2][2]=k;
k=state[2][1];
state[2][1]=state[2][3];
state[2][3]=k;
k=state[3][0];
state[3][0]=state[3][1];
state[3][1]=state[3][2];
state[3][2]=state[3][3];
state[3][3]=k;
//InvSubBytes:
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[i][j] = rsbox[state[i][j]];
}
}
//AddRoundKey:
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
state[j][i] ^= RoundKey[0 * Nb * 4 + i * Nb + j];
}
}
// The decryption process is over.
// Copy the state array to output array.
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
out[i*4+j]=state[j][i];
}
}
return j;
}
void main()
{
int i;
//unsigned char temp[17] = {0x0a ,0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 ,0x07 ,0x08 ,0x09 ,0x0a ,0x0b ,0x0c ,0x0d ,0x0e ,0x0f,0x00};
//unsigned char temp2[16]= {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 ,0x77 ,0x88 ,0x99 ,0xaa ,0xbb ,0xcc ,0xdd ,0xee ,0xff};
unsigned char temp3[16]= {0xb0,0xdc,0x5f,0x65,0xa4,0xf0,0x09,0xcb,0xaf,0xac,0x71,0xb9,0x7b,0x80,0x82,0x04};
unsigned char temp[17] = {0x2b ,0x7e ,0x15 ,0x16 ,0x28 ,0xae ,0xd2 ,0xa6 ,0xab ,0xf7 ,0x15 ,0x88 ,0x09 ,0xcf ,0x4f ,0x3c,0x00};
unsigned char temp2[16]= {0x32 ,0x43 ,0xf6 ,0xa8 ,0x88 ,0x5a ,0x30 ,0x8d ,0x31 ,0x31 ,0x98 ,0xa2 ,0xe0 ,0x37 ,0x07 ,0x34};
unsigned char out[16];
aes_encrypt(temp2,out,temp);
// Output the encrypted text.
printf("\nText after encryption:\n");
for(i=0;i<Nk*4;i++)
printf("%02x ",out[i]);
printf("\n\n");
aes_decrypt(temp3,out,temp);
// Output the decrypted text.
printf("\nText after decryption:\n");
for(i=0;i<Nk*4;i++)
printf("%02x ",temp2[i]);
printf("\n\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -