22恰好我有能运行的C语言的.docx
- 文档编号:4416155
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:16
- 大小:18.06KB
22恰好我有能运行的C语言的.docx
《22恰好我有能运行的C语言的.docx》由会员分享,可在线阅读,更多相关《22恰好我有能运行的C语言的.docx(16页珍藏版)》请在冰点文库上搜索。
22恰好我有能运行的C语言的
恰好我有。
能运行的,C语言的。
#include
#include"aes.h"
#include"commonage.h"
#definebyteunsignedchar
#defineBPOLY0x1b//!
#defineBLOCKSIZE16//! #defineKEYBITS128//! #defineROUNDS10//! #defineKEYLENGTH16//! bytexdatablock1[256];//! bytexdatablock2[256];//! bytexdata*powTbl;//! bytexdata*logTbl;//! bytexdata*sBox;//! bytexdata*sBoxInv;//! bytexdata*expandedKey;//! voidCalcPowLog(byte*powTbl,byte*logTbl) { bytexdatai=0; bytexdatat=1; do{ //Use0x03asrootforexponentiationandlogarithms. powTbl[i]=t; logTbl[t]=i; i++; //Muliplytby3inGF(2^8). t^=(t<<1)^(t&0x80? BPOLY: 0); }while(t! =1);//Cyclicpropertiesensurethati<255. powTbl[255]=powTbl[0];//255='-0',254=-1,etc. } voidCalcSBox(byte*sBox) { bytexdatai,rot; bytexdatatemp; bytexdataresult; //FillallentriesofsBox[]. i=0; do{ //InverseinGF(2^8). if(i>0){ temp=powTbl[255-logTbl[i]]; }else{ temp=0; } //AffinetransformationinGF (2). result=temp^0x63;//StartwithaddingavectorinGF (2). for(rot=0;rot<4;rot++){ //Rotateleft. temp=(temp<<1)|(temp>>7); //AddrotatedbyteinGF (2). result^=temp; } //Putresultintable. sBox[i]=result; }while(++i! =0); } voidCalcSBoxInv(byte*sBox,byte*sBoxInv) { bytexdatai=0; bytexdataj=0; //IteratethroughallelementsinsBoxInvusingi. do{ //SearchthroughsBoxusingj. cleardog(); do{ //Checkifcurrentjistheinverseofcurrenti. if(sBox[j]==i){ //Ifso,setsBoxIncandindicatesearchfinished. sBoxInv[i]=j; j=255; } }while(++j! =0); }while(++i! =0); } voidCycleLeft(byte*row) { //Cycle4bytesinanarrayleftonce. bytexdatatemp=row[0]; row[0]=row[1]; row[1]=row[2]; row[2]=row[3]; row[3]=temp; } voidInvMixColumn(byte*column) { bytexdatar0,r1,r2,r3; r0=column[1]^column[2]^column[3]; r1=column[0]^column[2]^column[3]; r2=column[0]^column[1]^column[3]; r3=column[0]^column[1]^column[2]; column[0]=(column[0]<<1)^(column[0]&0x80? BPOLY: 0); column[1]=(column[1]<<1)^(column[1]&0x80? BPOLY: 0); column[2]=(column[2]<<1)^(column[2]&0x80? BPOLY: 0); column[3]=(column[3]<<1)^(column[3]&0x80? BPOLY: 0); r0^=column[0]^column[1]; r1^=column[1]^column[2]; r2^=column[2]^column[3]; r3^=column[0]^column[3]; column[0]=(column[0]<<1)^(column[0]&0x80? BPOLY: 0); column[1]=(column[1]<<1)^(column[1]&0x80? BPOLY: 0); column[2]=(column[2]<<1)^(column[2]&0x80? BPOLY: 0); column[3]=(column[3]<<1)^(column[3]&0x80? BPOLY: 0); r0^=column[0]^column[2]; r1^=column[1]^column[3]; r2^=column[0]^column[2]; r3^=column[1]^column[3]; column[0]=(column[0]<<1)^(column[0]&0x80? BPOLY: 0); column[1]=(column[1]<<1)^(column[1]&0x80? BPOLY: 0); column[2]=(column[2]<<1)^(column[2]&0x80? BPOLY: 0); column[3]=(column[3]<<1)^(column[3]&0x80? BPOLY: 0); column[0]^=column[1]^column[2]^column[3]; r0^=column[0]; r1^=column[0]; r2^=column[0]; r3^=column[0]; column[0]=r0; column[1]=r1; column[2]=r2; column[3]=r3; } byteMultiply(unsignedcharnum,unsignedcharfactor) { bytemask=1; byteresult=0; while(mask! =0){ //Checkbitoffactorgivenbymask. if(mask&factor){ //AddcurrentmultipleofnuminGF (2). result^=num; } //Shiftmasktoindicatenextbit. mask<<=1; //Doublenum. num=(num<<1)^(num&0x80? BPOLY: 0); } returnresult; } byteDotProduct(unsignedchar*vector1,unsignedchar*vector2) { byteresult=0; result^=Multiply(*vector1++,*vector2++); result^=Multiply(*vector1++,*vector2++); result^=Multiply(*vector1++,*vector2++); result^=Multiply(*vector1,*vector2); returnresult; } voidMixColumn(byte*column) { bytexdatarow[8]={ 0x02,0x03,0x01,0x01, 0x02,0x03,0x01,0x01 };//Preparefirstrowofmatrixtwice,toeliminateneedforcycling. bytexdataresult[4]; //Takedotproductsofeachmatrixrowandthecolumnvector. result[0]=DotProduct(row+0,column); result[1]=DotProduct(row+3,column); result[2]=DotProduct(row+2,column); result[3]=DotProduct(row+1,column); //Copytemporaryresulttooriginalcolumn. column[0]=result[0]; column[1]=result[1]; column[2]=result[2]; column[3]=result[3]; } voidSubBytes(byte*bytes,bytecount) { do{ *bytes=sBox[*bytes];//Substituteeverybyteinstate. bytes++; }while(--count); } voidInvSubBytesAndXOR(byte*bytes,byte*key,bytecount) { do{ //*bytes=sBoxInv[*bytes]^*key;//Inversesubstituteeverybyteinstateandaddkey. *bytes=block2[*bytes]^*key;//Useblock2directly.Increasesspeed. bytes++; key++; }while(--count); } voidInvShiftRows(byte*state) { bytetemp; //Note: Stateisarrangedcolumnbycolumn. //Cyclesecondrowrightonetime. temp=state[1+3*4]; state[1+3*4]=state[1+2*4]; state[1+2*4]=state[1+1*4]; state[1+1*4]=state[1+0*4]; state[1+0*4]=temp; //Cyclethirdrowrighttwotimes. temp=state[2+0*4]; state[2+0*4]=state[2+2*4]; state[2+2*4]=temp; temp=state[2+1*4]; state[2+1*4]=state[2+3*4]; state[2+3*4]=temp; //Cyclefourthrowrightthreetimes,ie.leftonce. temp=state[3+0*4]; state[3+0*4]=state[3+1*4]; state[3+1*4]=state[3+2*4]; state[3+2*4]=state[3+3*4]; state[3+3*4]=temp; } voidShiftRows(byte*state) { bytetemp; //Note: Stateisarrangedcolumnbycolumn. //Cyclesecondrowleftonetime. temp=state[1+0*4]; state[1+0*4]=state[1+1*4]; state[1+1*4]=state[1+2*4]; state[1+2*4]=state[1+3*4]; state[1+3*4]=temp; //Cyclethirdrowlefttwotimes. temp=state[2+0*4]; state[2+0*4]=state[2+2*4]; state[2+2*4]=temp; temp=state[2+1*4]; state[2+1*4]=state[2+3*4]; state[2+3*4]=temp; //Cyclefourthrowleftthreetimes,ie.rightonce. temp=state[3+3*4]; state[3+3*4]=state[3+2*4]; state[3+2*4]=state[3+1*4]; state[3+1*4]=state[3+0*4]; state[3+0*4]=temp; } voidInvMixColumns(byte*state) { InvMixColumn(state+0*4); InvMixColumn(state+1*4); InvMixColumn(state+2*4); InvMixColumn(state+3*4); } voidMixColumns(byte*state) { MixColumn(state+0*4); MixColumn(state+1*4); MixColumn(state+2*4); MixColumn(state+3*4); } voidXORBytes(byte*bytes1,byte*bytes2,bytecount) { do{ *bytes1^=*bytes2;//AddinGF (2),ie.XOR. bytes1++; bytes2++; }while(--count); } voidCopyBytes(byte*to,byte*from,bytecount) { do{ *to=*from; to++; from++; }while(--count); } voidKeyExpansion(byte*expandedKey) { bytexdatatemp[4]; bytei; bytexdataRcon[4]={0x01,0x00,0x00,0x00};//Roundconstant. unsignedcharxdata*key; unsignedcharxdataa[16]; key=a; //以下为加解密密码,共16字节。 可以选择任意值 key[0]=0x30; key[1]=0x30; key[2]=0x30; key[3]=0x30; key[4]=0x30; key[5]=0x30; key[6]=0x30; key[7]=0x30; key[8]=0x30; key[9]=0x30; key[10]=0x30; key[11]=0x30; key[12]=0x30; key[13]=0x30; key[14]=0x30; key[15]=0x30; //////////////////////////////////////////// //Copykeytostartofexpandedkey. i=KEYLENGTH; do{ *expandedKey=*key; expandedKey++; key++; }while(--i); //Preparelast4bytesofkeyintemp. expandedKey-=4; temp[0]=*(expandedKey++); temp[1]=*(expandedKey++); temp[2]=*(expandedKey++); temp[3]=*(expandedKey++); //Expandkey. i=KEYLENGTH; while(i //Areweatthestartofamultipleofthekeysize? if((i%KEYLENGTH)==0){ CycleLeft(temp);//Cycleleftonce. SubBytes(temp,4);//Substituteeachbyte. XORBytes(temp,Rcon,4);//AddconstantinGF (2). *Rcon=(*Rcon<<1)^(*Rcon&0x80? BPOLY: 0); } //Keysizelargerthan24bytes,ie.largerthat192bits? #ifKEYLENGTH>24 //Arewerightpastablocksize? elseif((i%KEYLENGTH)==BLOCKSIZE){ SubBytes(temp,4);//Substituteeachbyte. } #endif //AddbytesinGF (2)oneKEYLENGTHaway. XORBytes(temp,expandedKey-KEYLENGTH,4); //Copyresulttocurrent4bytes. *(expandedKey++)=temp[0]; *(expandedKey++)=temp[1]; *(expandedKey++)=temp[2]; *(expandedKey++)=temp[3]; i+=4;//Next4bytes. } } voidInvCipher(byte*block,byte*expandedKey) { byteround=ROUNDS-1; expandedKey+=BLOCKSIZE*ROUNDS; XORBytes(block,expandedKey,16); expandedKey-=BLOCKSIZE; do{ InvShiftRows(block); InvSubBytesAndXOR(block,expandedKey,16); expandedKey-=BLOCKSIZE; InvMixColumns(block); }while(--round);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 22 恰好 运行 语言