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

📄 aes.cpp

📁 自编高级加密算法体制AES
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				for(int j = 0; j < 4;j++)
					cout<<dec<<"bit["<<i<<"]:"<<hex<<m_Current[j][i].to_ulong()<<"\t";
				cout<<endl;
			}

			for(int i = 0; i < 4; i++)
				for(int j = 0; j < 4; j++)
					m_Current[i][j] = InvSubBypes(m_Current[i][j]);
			cout<<"InvSubBytes 处理后:"<<endl;
			for(int i = 0; i < 4; i++)
			{
				for(int j = 0; j < 4;j++)
					cout<<dec<<"bit["<<i<<"]:"<<hex<<m_Current[j][i].to_ulong()<<"\t";
				cout<<endl;
			}
			
			
			

			cout<<"本轮密钥:"<<endl;
			for(int i = 0; i < 4; i++)
			{
				for(int j = 0; j < 4;j++)
					cout<<dec<<"bit["<<i<<"]:"<<hex<<m_KeyB[EncryptionRounds-index][j][i].to_ulong()<<"\t";
				cout<<endl;
			}
			xor(m_Current,m_KeyB[EncryptionRounds-index]);
			cout<<"AddRoundKey 处理后:"<<endl;
			for(int i = 0; i < 4; i++)
			{
				for(int j = 0; j < 4;j++)
					cout<<dec<<"bit["<<i<<"]:"<<hex<<m_Current[j][i].to_ulong()<<"\t";
				cout<<endl;
			}
			
			if(num == m_last-1)
			{
				for(int i = 0; i < 4; i++)
					BitToWord(m_Current[i],m_ClearW[i]);
				cout<<"输出得到明文:"<<endl;
				for(int i = 0; i < 4; i++)
					cout<<hex<<m_ClearW[i].to_ulong()<<endl;
				cout<<"按任意键退出..."<<endl;
				getchar();
				return;
			}
			InvMixColumns(m_Current);
			cout<<"InvMixColumn 处理后:"<<endl;
			for(int i = 0; i < 4; i++)
			{
				for(int j = 0; j < 4;j++)
					cout<<dec<<"bit["<<i<<"]:"<<hex<<m_Current[j][i].to_ulong()<<"\t";
				cout<<endl;
			}
			index++;
					
		}
		cout<<"本轮结束\n"<<endl;
		cout<<"按任意键继续..."<<endl;
		getchar();
	}

}
void AES::WordToBit(const Word w,Bit bit[4])//字转换为字节
{
	
	for(int i = 0; i < 8; i++)
	{
		bit[0][i] = w[i+24];
	}
	for(int i = 0; i < 8; i++)
	{
		bit[1][i] = w[i+16];
	}
	for(int i = 0; i < 8; i++)
	{
		bit[2][i] = w[i+8];
	}
	for(int i = 0; i < 8; i++)
	{
		bit[3][i] = w[i];
	}
//	for(int i = 0; i < 4; i++)
//		cout<<dec<<"bit["<<i<<"]:"<<hex<<bit[i].to_ulong()<<endl; //测试
}
void AES::BitToWord(const Bit bit[4],Word& w)//字节转换为字
{
	for(int i = 0; i < 8; i++)
	{
		w[i+24] = bit[0][i];
	}
	for(int i = 0; i < 8; i++)
	{
		w[i+16] = bit[1][i];
	}
	for(int i = 0; i < 8; i++)
	{
		w[i+8] = bit[2][i];
	}
	for(int i = 0; i < 8; i++)
	{
		w[i] = bit[3][i];
	}
}
Word AES::RotWord(Word& w) //循环位移
{
	Bit b[4];
	Word temp(0x00);
	for(int i = 0; i < 8; i++)
		temp[i] = w[i+24];
	for(int i = 0; i < 24; i++)
		temp[i+8] = w[i];
	w = temp;
//	cout<<hex<<"After RotWord:"<<w.to_ulong()<<endl; 测试
	return w;
}
Bit AES::SubBypes(Bit &b)//S盒置换
{
	int x = 0;
	int y = 0;
	Bit temp1 = b;
	Bit temp2 = b;
	temp1 &= 15;
	y = temp1.to_ulong();
	temp2 = (temp2.to_ulong() - temp1.to_ulong())/pow(2.0,4);
	x = temp2.to_ulong();
	
	b = SDataSource[x][y];
//	cout<<hex<<b.to_ulong();
	return b;	
}

Bit AES::InvSubBypes(Bit &b)//反S盒置换
{
	int x = 0;
	int y = 0;
	Bit temp1 = b;
	Bit temp2 = b;
	temp1 &= 15;
	y = temp1.to_ulong();
	temp2 = (temp2.to_ulong() - temp1.to_ulong())/pow(2.0,4);
	x = temp2.to_ulong();
	
	b = InvSDataSource[x][y];
//	cout<<hex<<b.to_ulong();
	return b;	
}

Word AES::SubWord(Word &w)//以字为单位S盒置换
{
	Bit bit[4];
	for(int i = 0; i < 8; i++)
	{
		bit[0][i] = w[i+24];
	}
	for(int i = 0; i < 8; i++)
	{
		bit[1][i] = w[i+16];
	}
	for(int i = 0; i < 8; i++)
	{
		bit[2][i] = w[i+8];
	}
	for(int i = 0; i < 8; i++)
	{
		bit[3][i] = w[i];
	}
	for(int i = 0; i < 4; i++)
		bit[i] = SubBypes(bit[i]);
	for(int i = 0; i < 8; i++)
	{
		w[i+24] = bit[0][i];
	}
	for(int i = 0; i < 8; i++)
	{
		 w[i+16] = bit[1][i];
	}
	for(int i = 0; i < 8; i++)
	{
		 w[i+8] = bit[2][i];
	}
	for(int i = 0; i < 8; i++)
	{
		 w[i] = bit[3][i];
	}
//	cout<<hex<<"After SubWord:"<<w.to_ulong()<<endl; 测试
	return w;
}
Word AES::xor(Word x,Word y)//以字为单位抑或
{
	x ^= y.to_ulong();
//	cout<<hex<<"After xor:"<<x.to_ulong()<<endl; 测试
	return x;
}
Bit AES::xor(Bit x,Bit y)//以字节为单位抑或
{
	x ^= y.to_ulong();
//	cout<<hex<<"After xor:"<<x.to_ulong()<<endl; 测试
	return x;
}
void AES::ShiftRows(Bit bit[4][4])//行位移运算
{
	Bit temp(0x00);

	temp = bit[0][1];
	for(int j = 1; j < 4; j++)
	{
			bit[j-1][1] = bit[j][1];
	}
	bit[3][1] = temp;

	for(int i = 0; i < 2; i++)
	{
		temp = bit[0][2];
		for(int j = 1; j < 4; j++)
		{
			bit[j-1][2] = bit[j][2];
		}
		bit[3][2] = temp;
	}

	for(int i = 0; i < 3; i++)
	{
		temp = bit[0][3];
		for(int j = 1; j < 4; j++)
		{
			bit[j-1][3] = bit[j][3];
		}
		bit[3][3] = temp;
	}
	for(int i = 0; i < 4; i++)
			for(int j = 0; j < 4; j++)
					m_Current[i][j] = bit[i][j];
}
void AES::InvShiftRows(Bit bit[4][4])//反行位移运算
{
	
	Bit temp(0x00);

	temp = bit[3][1];
	for(int j = 3; j >=0; j--)
	{
			bit[j][1] = bit[j-1][1];
	}
	bit[0][1] = temp;

	for(int i = 0; i < 2; i++)
	{
		temp = bit[3][2];
		for(int j = 3; j >=0; j--)
		{
			 bit[j][2] = bit[j-1][2];
		}
		bit[0][2] = temp;
	}

	for(int i = 0; i < 3; i++)
	{
		temp = bit[3][3];
		for(int j = 3; j >=0; j--)
		{
			 bit[j][3] = bit[j-1][3];
		}
		bit[0][3] = temp;
	}
	for(int i = 0; i < 4; i++)
			for(int j = 0; j < 4; j++)
					m_Current[i][j] = bit[i][j];
}
void AES::MixColumns(Bit state[4][4])//列混合运算
{

	Bit t[4];
	for(int c = 0;c < 4; c++)
	{
		for(int r = 0; r < 4; r++)
			t[r] = state[c][r];
		
		for(int r = 0; r < 4; r++)
		{
//			cout<<"orginal state"<<hex<<state[c][r].to_ulong()<<endl;
			state[c][r] = xor(xor(xor(Ffmul(0x02,t[r]),Ffmul(0x03,t[(r+1)%4])),t[(r+2)%4]),t[(r+3)%4]);
//			cout<<"last state"<<hex<<state[c][r].to_ulong()<<endl;
		}
		
	}

}
void AES::InvMixColumns(Bit state[4][4])//反列混合运算
{

	Bit t[4];
	for(int c = 0;c < 4; c++)
	{
		for(int r = 0; r < 4; r++)
			t[r] = state[c][r];
		
		for(int r = 0; r < 4; r++)
		{
//			cout<<"orginal state"<<hex<<state[c][r].to_ulong()<<endl;
			state[c][r] = xor(xor(xor(Ffmul(0x0e,t[r]),Ffmul(0x0b,t[(r+1)%4])),Ffmul(0x0d,t[(r+2)%4])),Ffmul(0x09,t[(r+3)%4]));
//			cout<<"last state"<<hex<<state[c][r].to_ulong()<<endl;
		}
		
	}

}
Bit AES::Ffmul(Bit bit1,Bit bit2)//乘法运算
{
	if(bit1 == 0 || bit2 == 0)
		return 0;
	Bit temp(0x00);
	int x1 = 0,x2 = 0,y1 = 0,y2 = 0,high1 = 0,high2 = 0,low1 = 0,low2 = 0;
	x1 = bit1[7]*pow(2.0,3) + bit1[6]*pow(2.0,2) + bit1[5]*pow(2.0,1) + bit1[4];
	y1 = bit1[3]*pow(2.0,3) + bit1[2]*pow(2.0,2) + bit1[1]*pow(2.0,1) + bit1[0];
	x2 = bit2[7]*pow(2.0,3) + bit2[6]*pow(2.0,2) + bit2[5]*pow(2.0,1) + bit2[4];
	y2 = bit2[3]*pow(2.0,3) + bit2[2]*pow(2.0,2) + bit2[1]*pow(2.0,1) + bit2[0];
//	cout<<"x1: "<<x1<<"y1: "<<y1<<"x2: "<<x2<<"y2: "<<y2<<endl;
	temp = logTable[x1][y1].to_ulong() + logTable[x2][y2].to_ulong();

	low1 = logTable[x1][y1][3]*pow(2.0,3) + logTable[x1][y1][2]*pow(2.0,2) + logTable[x1][y1][1]*pow(2.0,1) +logTable[x1][y1][0];
	low2 = logTable[x2][y2][3]*pow(2.0,3) + logTable[x2][y2][2]*pow(2.0,2) + logTable[x2][y2][1]*pow(2.0,1) +logTable[x2][y2][0];
	if((low1 + low2) >= 16)
		high1 = 1;
	high1 += logTable[x1][y1][7]*pow(2.0,3) + logTable[x1][y1][6]*pow(2.0,2) + logTable[x1][y1][5]*pow(2.0,1) +logTable[x1][y1][4];
	high2 = logTable[x2][y2][7]*pow(2.0,3) + logTable[x2][y2][6]*pow(2.0,2) + logTable[x2][y2][5]*pow(2.0,1) +logTable[x2][y2][4];
	if((high1 + high2) >= 16)
		temp = temp.to_ulong() + 1;
	
	x1 = temp[7]*pow(2.0,3) + temp[6]*pow(2.0,2) + temp[5]*pow(2.0,1) + temp[4];
	y1 = temp[3]*pow(2.0,3) + temp[2]*pow(2.0,2) + temp[1]*pow(2.0,1) + temp[0];
//	cout<<"x3: "<<x1<<"y3: "<<y1<<endl;
	temp = InvLogTable[x1][y1];
//	cout<<"result: "<<hex<<temp.to_ulong()<<endl;
	return temp;
}
void AES::xor(Bit bit1[4][4],Bit bit2[4][4])//以字节为单位抑或
{
	for(int i = 0; i < 4; i++)
		for(int j = 0; j < 4; j++)
		{
			m_Current[i][j] = xor(bit1[i][j],bit2[i][j]);
		}
}

⌨️ 快捷键说明

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