AES算法C源代码Word格式.docx
《AES算法C源代码Word格式.docx》由会员分享,可在线阅读,更多相关《AES算法C源代码Word格式.docx(10页珍藏版)》请在冰点文库上搜索。
![AES算法C源代码Word格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/8/1db068f4-4434-4dca-b810-cc3c4878cc94/1db068f4-4434-4dca-b810-cc3c4878cc941.gif)
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};
{
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];
elseif(Nk>
6&
&
i%Nk==4)
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++;
}
voidAddRoundKey(intround)
{
inti,j;
for(i=0;
i<
4;
i++)
for(j=0;
j<
j++)
state[j][i]^=RoundKey[round*Nb*4+i*Nb+j];
voidInvSubBytes()
state[i][j]=getSBoxInvert(state[i][j]);
Sothefirstrowisnotshifted.
voidInvShiftRows()
unsignedchartemp;
voidInvMixColumns()
inti;
unsignedchara,b,c,d;
{
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);
voidInvCipher()
inti,j,round=0;
state[j][i]=in[i*4+j];
AddRoundKey(Nr);
for(round=Nr-1;
round>
0;
round--)
InvShiftRows();
InvSubBytes();
AddRoundKey(round);
InvMixColumns();
AddRoundKey(0);
out[i*4+j]=state[j][i];
voidSubBytes()
state[i][j]=getSBoxValue(state[i][j]);
voidShiftRows()
voidCipher()
for(round=1;
round<
Nr;
round++)
SubBytes();
ShiftRows();
MixColumns();
char*encrypt(char*str,char*key)
inti,j,Nl;
doublelen;
char*newstr;
Nk=Nc/32;
Nr=Nk+6;
len=strlen(str);
Nl=(int)ceil(len/16);
//printf("
Nl:
%d\n"
Nl);
newstr=(char*)malloc(Nl*32);
memset(newstr,0,sizeof(newstr));
Nl;
Nk*4;
Key[j]=key[j];
in[j]=str[i*16+j];
KeyExpansion();
Cipher();
memcpy(&
newstr[i*32],out,32);
returnnewstr;
char*decrypt(char*str,char*key)
inti,j,len,Nl;
newstr=(char*)malloc(16*Nl);
InvCipher();
memcpy(&
intmain()
{charstr_1[128];
//存明文
charstr_2[128];
//存加密密钥
charstr_3[128];
//存解密密钥
char*str;
char*str2;
printf("
请输入明文:
\n"
);
scanf("
%s"
str_1);
请输入加密密钥:
str_2);
str=encrypt(str_1,str_2);
进行加密后:
%s\n\n"
str);
请输入解密密钥:
str_3);
str2=decrypt(str,str_3);
进行解密后:
%s\n"
str2);
getch();
return0;