📄 aes.cpp
字号:
void AES::Round(unsigned char* RoundKey)
{
int i=0;
unsigned long k0;
unsigned long t0;
unsigned long t1;
unsigned long t2;
unsigned long t3;
ShiftRow();
for(i=0;i<Nb;i++)
{
memcpy(&t0,table0+4*(*(buffer+4*i)),4);
memcpy(&t1,table1+4*(*(buffer+4*i+1)),4);
memcpy(&t2,table2+4*(*(buffer+4*i+2)),4);
memcpy(&t3,table3+4*(*(buffer+4*i+3)),4);
memcpy(&k0,RoundKey+4*i,4);
t0 = t0 ^ t1 ^ t2 ^ t3 ^ k0;
memcpy(buffer+4*i,&t0,4);
}//end for(i)
}
void AES::FinalRound(unsigned char* RoundKey)
{
unsigned char* temp = buffer;
unsigned long k0;
unsigned long t0;
unsigned char b[4];
int i = 0;
ShiftRow();
for (i=0;i<Nb;i++)
{
b[0] = SBoxT[*(temp+4*i)];
b[1] = SBoxT[*(temp+4*i+1)];
b[2] = SBoxT[*(temp+4*i+2)];
b[3] = SBoxT[*(temp+4*i+3)];
memcpy(&k0,RoundKey+4*i,4);
memcpy(&t0,b,4);
t0 = t0 ^ k0;
memcpy(temp+4*i,&t0,4);
}
}
void AES::InvMixColumn(unsigned char*& ExKey)
{
int i=0;
unsigned long t0;
unsigned long t1;
unsigned long t2;
unsigned long t3;
for(i=0;i<Nb;i++)
{
memcpy(&t0,ivctable0+4*(*(ExKey+4*i)),4);
memcpy(&t1,ivctable1+4*(*(ExKey+4*i+1)),4);
memcpy(&t2,ivctable2+4*(*(ExKey+4*i+2)),4);
memcpy(&t3,ivctable3+4*(*(ExKey+4*i+3)),4);
t0 = t0 ^ t1 ^ t2 ^ t3;
memcpy(ExKey+4*i,&t0,4);
}//end for(i)
}
void AES::InvShiftRow()
{
unsigned char c1,c2,c3,t1[7],t2[7],t3[7],i;
c1 = Nb-ShiftT[3*(Nb/2-2)];
c2 = Nb-ShiftT[3*(Nb/2-2)+1];
c3 = Nb-ShiftT[3*(Nb/2-2)+2];
for (i=0;i<7;i++)
{
t1[i] = *(buffer+(4*i)+1);
t2[i] = *(buffer+(4*i)+2);
t3[i] = *(buffer+(4*i)+3);
}
for(i=0;i<Nb;i++)
{
*(buffer+4*i+1) = *(buffer+4*((i+c1)%Nb)+1);
*(buffer+4*i+2) = *(buffer+4*((i+c2)%Nb)+2);
*(buffer+4*i+3) = *(buffer+4*((i+c3)%Nb)+3);
}
for(i=c1;i>=1;i--)
*(buffer+4*(Nb-i)+1) = t1[c1-i];
for(i=c2;i>=1;i--)
*(buffer+4*(Nb-i)+2) = t2[c2-i];
for(i=c3;i>=1;i--)
*(buffer+4*(Nb-i)+3) = t3[c3-i];
}
void AES::I_Round(unsigned char* RoundKey)
{
int i=0;
unsigned long k0;
unsigned long t0;
unsigned long t1;
unsigned long t2;
unsigned long t3;
InvShiftRow();
for(i=0;i<Nb;i++)
{
memcpy(&t0,I_table0+4*(*(buffer+4*i)),4);
memcpy(&t1,I_table1+4*(*(buffer+4*i+1)),4);
memcpy(&t2,I_table2+4*(*(buffer+4*i+2)),4);
memcpy(&t3,I_table3+4*(*(buffer+4*i+3)),4);
memcpy(&k0,RoundKey+4*i,4);
t0 = t0 ^ t1 ^ t2 ^ t3 ^ k0;
memcpy(buffer+4*i,&t0,4);
}//end for(i)
}
void AES::I_FinalRound(unsigned char* RoundKey)
{
unsigned char* temp = buffer;
unsigned long k0;
unsigned long t0;
unsigned char b[4];
int i = 0;
InvShiftRow();
for (i=0;i<Nb;i++)
{
b[0] = InvSBoxT[*(temp+4*i)];
b[1] = InvSBoxT[*(temp+4*i+1)];
b[2] = InvSBoxT[*(temp+4*i+2)];
b[3] = InvSBoxT[*(temp+4*i+3)];
memcpy(&k0,RoundKey+4*i,4);
memcpy(&t0,b,4);
t0 = t0 ^ k0;
memcpy(temp+4*i,&t0,4);
}
}
int ECM_E(const char* inname, const char* outname,unsigned char* key, int keysize, int blocksize)
{
AES aes;
int result = 0;
int filesize = 0;
int padding = -1;
int temp = 0;
unsigned char buffer[BufferSize]={0};
unsigned char wrbuffer[BufferSize]={0};
unsigned char state[32]={0};
FILE *fin,*fout;
if ( (fin = fopen(inname, "rb")) == NULL)
return 4;
if ( (fout = fopen(outname, "wb")) == NULL)
return 5;
temp = blocksize / 8;
do
{
filesize = fread(buffer,1,BufferSize,fin);
if (ferror(fin))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -