⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aesfunctions.h

📁 面积最小的AES高级加密算法实现
💻 H
📖 第 1 页 / 共 2 页
字号:
		aux3[i] = *(key + i * 4 + 3) ^ aux3[i];
		*(key + i * 4 + 3) = aux3[i];
	}

}
/*----------------------------------------------------------------------------*/
unsigned char
mul(unsigned char a, unsigned char b)
{
	if (a && b)
		return Alogtable[(Logtable[a] + Logtable[b])%255];
	else
		return 0;
}
/*----------------------------------------------------------------------------*/
void
MixColum(unsigned char *state)
{
	int j = 0;
	unsigned char aux = 0;
	unsigned char aux_vector[16];

	for (j = 0; j < 4; j++)
	{
		aux = mul(0x2, *(state + j)) ^ mul(0x3, *(state + j + 4)) ^ *(state + j + 8) ^ *(state + j + 12);
		aux_vector[j] = aux;
		aux = *(state + j) ^ mul(0x2, *(state + j + 4)) ^ mul(0x3, *(state + j + 8)) ^ *(state + j + 12);
		aux_vector[j + 4] = aux;
		aux = *(state + j) ^ *(state + j + 4) ^ mul(0x2, *(state + j + 8)) ^ mul(0x3, *(state + j + 12));
		aux_vector[j + 8] = aux;
		aux = mul(0x3, *(state + j)) ^ *(state + j + 4) ^ *(state + j + 8) ^ mul(0x2, *(state + j + 12));
		aux_vector[j + 12] = aux;
	}
	for (j = 0; j < 16; j++)
		*(state + j) = aux_vector[j];
}
/*----------------------------------------------------------------------------*/
void
InvMixColum(unsigned char *state)
{
	int j = 0;
	unsigned char aux = 0;
	unsigned char aux_vector[16];

	for (j = 0; j < 4; j++)
	{
		aux = mul(0x0e, *(state + j)) ^ mul(0x0b, *(state + j + 4)) ^ mul(0x0d, *(state + j + 8)) ^ mul(0x09, *(state + j + 12));
		aux_vector[j] = aux;
		aux = mul(0x09, *(state + j)) ^ mul(0x0e, *(state + j + 4)) ^ mul(0x0b, *(state + j + 8)) ^ mul(0x0d, *(state + j + 12));
		aux_vector[j + 4] = aux;
		aux = mul(0x0d, *(state + j)) ^ mul(0x09, *(state + j + 4)) ^ mul(0x0e, *(state + j + 8)) ^ mul(0x0b, *(state + j + 12));
		aux_vector[j + 8] = aux;
		aux = mul(0x0b, *(state + j)) ^ mul(0x0d, *(state + j + 4)) ^ mul(0x09, *(state + j + 8)) ^ mul(0x0e, *(state + j + 12));
		aux_vector[j + 12] = aux;
	}
	for (j = 0; j < 16; j++)
		*(state + j) = aux_vector[j];
}

/*----------------------------------------------------------------------------*/
void
AddRoundKey(unsigned char *state, unsigned char *key)
{
	int i = 0;
	for (i = 0; i < 16; i++)
		*(state + i) ^= *(key + i);
}
/*----------------------------------------------------------------------------*/
/*void
PrintState(unsigned char *state)
{
int i = 0;
for(i = 0; i < 16; i++)
    {
    diag_printf("%x   ", *(state + i));
    if((i+1) % 4 == 0)
	diag_printf("\n");

    }
}*/
/*----------------------------------------------------------------------------*/
void
SubBytes(unsigned char *state)
{
	int i = 0;
	for (i = 0; i < 16; i++)
	{
		*(state + i) = Sen[*(state + i)];
	}
}
/*----------------------------------------------------------------------------*/
void
InvSubBytes(unsigned char *state)
{
	int i = 0;
	for (i = 0; i < 16; i++)
	{
		*(state + i) = Sde[*(state + i)];
	}
}
/*----------------------------------------------------------------------------*/

void
ShiftRows(unsigned char *state)
{
	unsigned char AUX[16];
	int i = 0;
	for (i = 0; i < 16; i++)
	{
		AUX[i] = *(state + i);
	}
	*(state + 4) = AUX[5];
	*(state + 5) = AUX[6];
	*(state + 6) = AUX[7];
	*(state + 7) = AUX[4];

	*(state + 8) = AUX[10];
	*(state + 9) = AUX[11];
	*(state + 10) = AUX[8];
	*(state + 11) = AUX[9];

	*(state + 12) = AUX[15];
	*(state + 13) = AUX[12];
	*(state + 14) = AUX[13];
	*(state + 15) = AUX[14];
}
/*----------------------------------------------------------------------------*/

void
InvShiftRows(unsigned char *state)
{
	unsigned char AUX[16];
	int i = 0;
	for (i = 0; i < 16; i++)
	{
		AUX[i] = *(state + i);
	}
	*(state + 4) = AUX[7];
	*(state + 5) = AUX[4];
	*(state + 6) = AUX[5];
	*(state + 7) = AUX[6];

	*(state + 8) = AUX[10];
	*(state + 9) = AUX[11];
	*(state + 10) = AUX[8];
	*(state + 11) = AUX[9];

	*(state + 12) = AUX[13];
	*(state + 13) = AUX[14];
	*(state + 14) = AUX[15];
	*(state + 15) = AUX[12];
}
/*----------------------------------------------------------------------------*/

unsigned char *
KeyExpand(unsigned char *key)
{
	int i = 0;
	int j = 0;

	unsigned char aux[10] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36};
	unsigned char rcon[4] = {0x00, 0x00, 0x00, 0x00};

	unsigned char auxmalloc[1000];
	unsigned char *expandedkey;
	expandedkey = (unsigned char *)auxmalloc;

	for (i = 0; i < 16; i++)
		*(expandedkey + i) = *(key + i);

	for (i = 0; i < 10; i++)
	{
		rcon[0] = aux[i];
		KeySchedule((unsigned char *)(key), (unsigned char *)rcon);
		for (j = 0; j < 16; j++)
			*(expandedkey + 16 * (i + 1) + j) = *(key + j);
	}
	return expandedkey;
}
/*----------------------------------------------------------------------------*/
void
encrypt_aes(unsigned char *block, unsigned char *key)
{
	int i = 0;

	switchblock(block);
	switchblock(key);


	unsigned char *expandedkey;
	expandedkey = KeyExpand((unsigned char *)key);

	AddRoundKey((unsigned char *)block, (unsigned char *)expandedkey);



	for (i = 0; i < 10; i++)
	{
		SubBytes((unsigned char *)block);
		ShiftRows((unsigned char *)block);
		if (i != 9)
			MixColum((unsigned char *)block);
		AddRoundKey((unsigned char *)block, (unsigned char *)(expandedkey + 16 * (i + 1)));
	}
	switchblock(block);
}
/*----------------------------------------------------------------------------*/
void
decrypt_aes(unsigned char *block, unsigned char *key)
{
	int i = 0;

	switchblock(block);
	switchblock(key);

	unsigned char *expandedkey;
	expandedkey = KeyExpand((unsigned char *)key);

	for (i = 10; i > 0; i--)
	{
		AddRoundKey((unsigned char *)block, (unsigned char *)(expandedkey + 16 * i));
		if (i != 10)
			InvMixColum((unsigned char *)block);
		InvShiftRows((unsigned char *)block);
		InvSubBytes((unsigned char *)block);
	}
	AddRoundKey((unsigned char *)block, (unsigned char *)expandedkey);
	switchblock(block);
}
/*----------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -