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

📄 aesdlg.cpp

📁 AES的基本要求是
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		}		
	}
	out<<endl;//--------------------------------------------

}

void CAESDlg::MixColumn()
{
int Mix_temp[4][4],Mix_temp1[4][4];
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			Mix_temp[i][j]=B1[2*i+8*j]*16+B1[2*i+8*j+1];
			Mix_temp1[i][j]=0;
		}
	}

	for( i=0;i<4;i++){
		for(int j=0;j<4;j++){
			for(int ij=0;ij<4;ij++){
				int temp=0 ;
				if((Mix_temp[ij][j]&0x80)!=0)///如果*2要加1B
				{
					if(Mix[i][ij]==1)     {temp=Mix_temp[ij][j];}
					else if(Mix[i][ij]==2){temp=(Mix_temp[ij][j]<<1)&0x0ff^0x1b;}
					else if(Mix[i][ij]==3){temp=((Mix_temp[ij][j]<<1)&0x0ff^0x1b)^Mix_temp[ij][j];}
					///如果列混合不是这个矩阵就要换
				}
				else
				{
					if(Mix[i][ij]==1)     {temp=Mix_temp[ij][j];}
					else if(Mix[i][ij]==2){temp=(Mix_temp[ij][j]<<1)&0x0ff;}
					else if(Mix[i][ij]==3){temp=((Mix_temp[ij][j]<<1)&0x0ff)^Mix_temp[ij][j];}
				}
				Mix_temp1[i][j]^=temp;
			}
		}
	}
	out<<"列混合后"<<endl;//////////-------------------------------------------------
	for( i=0;i<4;i++){
		for(int j=0;j<4;j++){
			B1[2*i+8*j]=(Mix_temp1[i][j]>>4)&0x0f;
			B1[2*i+8*j+1]=Mix_temp1[i][j]& 0x00f;
		//	out<<B1[2*i+8*j]<<' '<<B1[2*i+8*j+1]<<' ';
		}
	}
	//------------------------------------------------------
	for( i=0;i<32;i++){
			out<<B1[i]<<' ';
	}
	//-----------------------------------------------------
	out<<endl;//////////////////////////////////-------------------------------------
}

void CAESDlg::RotByte()
{
		W_TEMP[0] = W[3][2];
		W_TEMP[1] = W[3][3];
		W_TEMP[2] = W[3][4];
		W_TEMP[3] = W[3][5];
		W_TEMP[4] = W[3][6];
		W_TEMP[5] = W[3][7];
		W_TEMP[6] = W[3][0];
		W_TEMP[7] = W[3][1];
}

void CAESDlg::SubByte()
{
		W[4][0] = Sbox[ W_TEMP[0] ][ W_TEMP[1]  ]>>4;
		W[4][1] = Sbox[ W_TEMP[0] ][ W_TEMP[1]  ]& 0x0f;
		W[4][2] = Sbox[ W_TEMP[2] ][ W_TEMP[3]  ]>>4;
		W[4][3] = Sbox[ W_TEMP[2] ][ W_TEMP[3]  ]& 0x0f;
		W[4][4] = Sbox[ W_TEMP[4] ][ W_TEMP[5]  ]>>4;
		W[4][5] = Sbox[ W_TEMP[4] ][ W_TEMP[5]  ]& 0x0f;
		W[4][6] = Sbox[ W_TEMP[6] ][ W_TEMP[7]  ]>>4;
		W[4][7] = Sbox[ W_TEMP[6] ][ W_TEMP[7]  ]& 0x0f;
}

void CAESDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
		int i,j;
	for(i=0;i<32;i++){////读明文
		char	C_temp;
		C_temp=m_miwen[i];
		switch(C_temp){
		case '0':C[i]=0x0;break;
		case '1':C[i]=0x1;break;
		case '2':C[i]=0x2;break;
		case '3':C[i]=0x3;break;
		case '4':C[i]=0x4;break;
		case '5':C[i]=0x5;break;
		case '6':C[i]=0x6;break;
		case '7':C[i]=0x7;break;
		case '8':C[i]=0x8;break;
		case '9':C[i]=0x9;break;
		case 'a':C[i]=0xa;break;
		case 'b':C[i]=0xb;break;
		case 'c':C[i]=0xc;break;
		case 'd':C[i]=0xd;break;
		case 'e':C[i]=0xe;break;
		case 'f':C[i]=0xf;break;
		default:{MessageBox("请输入0~f的数,字母要小写", "友情提示");   }; ;/////////////////////////出错
		}		
	}

	for(i=0;i<32;i++){///////读密钥
		char K_temp;
		K_temp=m_key[i];
		switch(K_temp){
		case '0':K[i]=0x0;break;
		case '1':K[i]=0x1;break;
		case '2':K[i]=0x2;break;
		case '3':K[i]=0x3;break;
		case '4':K[i]=0x4;break;
		case '5':K[i]=0x5;break;
		case '6':K[i]=0x6;break;
		case '7':K[i]=0x7;break;
		case '8':K[i]=0x8;break;
		case '9':K[i]=0x9;break;
		case 'a':K[i]=0xa;break;
		case 'b':K[i]=0xb;break;
		case 'c':K[i]=0xc;break;
		case 'd':K[i]=0xd;break;
		case 'e':K[i]=0xe;break;
		case 'f':K[i]=0xf;break;
		default:{MessageBox("请输入0~f的数,字母要小写", "友情提示");   }; ;/////////////////////////出错
		}		
	}
	//////////////////////////////////////////////////////计算密钥
	for(i=0;i<4;i++){
		for(j=0;j<8;j++){
			W[i][j]=K[8*i+j];
			key[0][8*i+j]=K[8*i+j];
		}
	}
	//////////////////////////////////////////////////////加密
	//////////////////////////////////////////(1)初始轮加密钥加

	makekey();
	for(i=0;i<32;i++){     
		C1[i]=C[i];		
	}

	//////////////////////////////++++++++++++++++++++++++++十轮加密
	for(int loop=10;loop>=1;loop--){
		///////////////////////////////////////(2)变换,行移,列混合
		for(i=0;i<32;i++)///把1 到10的密钥取出来
		{
			K[i]=key[loop][i];
		}
		for(i=0;i<32;i++){   //第一轮加密的结果  
			C1[i]=C1[i]^K[i];		
		}
		/////------------------------------------
		out<<"检查相与后的答案:"<<endl;
		for(i=0;i<32;i++){     
			out<<C1[i]<<' ';		
		}
		out<<endl;
		////////----------------------------------------------
		if(loop!=10)InvMixColumn();//列混合
		InvShiftRow();///行移位
		InvByteSub();  //查SBOX,字节转换

		///----------------------------------------------------------
		out<<loop<<endl;
		out<<'K'<<loop<<": ";
		for(i=0;i<32;i++)
			out<<K[i]<<' ';
		out<<endl;
		out<<'C'<<loop<<": ";
		for(i=0;i<32;i++)
			out<<C1[i]<<' ';
		out<<endl;	

		///-----------------------------------------------------------
	}/////////////+++++++++++++++++++++++++++++++++++++++++++十轮加密+loop
	out<<"最终结果:"<<endl;
	m_menwen="";
	for(i=0;i<32;i++){   //第一轮加密的结果  
		C1[i]=C1[i]^key[0][i];
		out<<C1[i]<<' ';////////------------------------------------
				char miwen;

				switch(C1[i]){
				case 0:miwen='0';break;
				case 1:miwen='1';break;
				case 2:miwen='2';break;
				case 3:miwen='3';break;
				case 4:miwen='4';break;
				case 5:miwen='5';break;
				case 6:miwen='6';break;
				case 7:miwen='7';break;
				case 8:miwen='8';break;
				case 9:miwen='9';break;
				case 10:miwen='a';break;
				case 11:miwen='b';break;
				case 12:miwen='d';break;
				case 13:miwen='d';break;
				case 14:miwen='e';break;
				case 15:miwen='f';break;
				default: ;/////////////////////////出错
				}
		m_menwen+=miwen;
	}
	out<<endl;
	out<<endl;
	////////////////////////////
	UpdateData(false);
	
}

//DEL void CAESDlg::Invnextkey(int num_key)
//DEL {
//DEL 
//DEL }

void CAESDlg::InvByteSub()
{
	out<<"查iSBOX,字节表换"<<endl;///-------------------------------
	for(int i=0;i<16;i++){
		//B_TEMP[2*i]=iSbox[C1[2*i]][C1[2*i+1]]>>4;
		//B_TEMP[2*i+1]=iSbox[C1[2*i]][C1[2*i+1]]& 0x0f;
		//out<<B_TEMP[2*i]*16+B_TEMP[2*i+1]<<' ';//----------------------
		C1[2*i  ]=iSbox[C_TEMP[2*i]][C_TEMP[2*i+1]]>>4;
		C1[2*i+1]=iSbox[C_TEMP[2*i]][C_TEMP[2*i+1]]& 0x0f;
		//out<<C1[2*i]*16+C1[2*i+1]<<' ';//----------------------
		//out<<C1[2*i]<<' '<<C1[2*i+1]<<' ';//----------------------
	}
	/////------------------------------------
		out<<"检查查iSBOX,字节表换答案:"<<endl;
		for(i=0;i<32;i++){     
			out<<C1[i]<<' ';		
		}
		out<<endl;
	////////----------------------------------------------
	out<<endl;//-----------------------------------------------------
}

void CAESDlg::InvShiftRow()
{
	out<<"行移位"<<endl;///-------------------------------
		for(int i=0;i<4;i++){
			for(int j=0;j<4;j++){
				//C1[2*i+8*j  ]=B_TEMP[(2*i+8*(j+i))%32  ];///第1行
				//C1[2*i+8*j+1]=B_TEMP[(2*i+8*(j+i)+1)%32];
	////////////C1[(2*i+8*(j+i))%32  ]=B_TEMP[2*i+8*j  ];///第1行
	////////////C1[(2*i+8*(j+i)+1)%32]=B_TEMP[2*i+8*j+1];
				//out<<C1[2*i+8*j  ]*16+C1[2*i+8*j+1]<<' ';//----------------------
				C_TEMP[(2*i+8*(j+i))%32  ]=C1[2*i+8*j  ];
				C_TEMP[(2*i+8*(j+i)+1)%32]=C1[2*i+8*j+1];
			}
		}
	////////////------------------------------------
/*	for( i=0;i<4;i++){
		for(int j=0;j<4;j++){
			out<<Mix_temp1[j][i]<<' ';
		}
	}*/
	out<<endl;
	//-----------------------------------------------------
}

void CAESDlg::InvMixColumn()
{
	int Mix_temp[4][4],Mix_temp1[4][4];
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			Mix_temp[i][j]=C1[2*i+8*j]*16+C1[2*i+8*j+1];
			Mix_temp1[i][j]=0;
		}
	}

	for( i=0;i<4;i++){
		for(int j=0;j<4;j++){
			for(int ij=0;ij<4;ij++){
				int temp=0,temp1=0,temp2=0,temp3=0;
				if(InvMix[i][ij]==0x0e)
				{	
					if((Mix_temp[ij][j]&0x80)!=0){temp1=((Mix_temp[ij][j]<<1)&0x0ff)^0x1b;}
					else {temp1=(Mix_temp[ij][j]<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x40)!=0){temp2=((temp1<<1)&0x0ff)^0x1b;}
					else {temp2=(temp1<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x20)!=0){temp3=((temp2<<1)&0x0ff)^0x1b;}
					else {temp3=(temp2<<1)&0x0ff;}
					temp=temp1^temp2^temp3;
				}
				else if(InvMix[i][ij]==0x0b)
				{
					if((Mix_temp[ij][j]&0x80)!=0){temp1=((Mix_temp[ij][j]<<1)&0x0ff)^0x1b;}
					else {temp1=(Mix_temp[ij][j]<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x40)!=0){temp2=((temp1<<1)&0x0ff)^0x1b;}
					else {temp2=(temp1<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x20)!=0){temp3=((temp2<<1)&0x0ff)^0x1b;}
					else {temp3=(temp2<<1)&0x0ff;}
					temp=temp1^temp3^Mix_temp[ij][j];
				}
				else if(InvMix[i][ij]==0x0d)
				{
					if((Mix_temp[ij][j]&0x80)!=0){temp1=((Mix_temp[ij][j]<<1)&0x0ff)^0x1b;}
					else {temp1=(Mix_temp[ij][j]<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x40)!=0){temp2=((temp1<<1)&0x0ff)^0x1b;}
					else {temp2=(temp1<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x20)!=0){temp3=((temp2<<1)&0x0ff)^0x1b;}
					else {temp3=(temp2<<1)&0x0ff;}
					temp=Mix_temp[ij][j]^temp2^temp3;
				}
				else if(InvMix[i][ij]==0x09)
				{
					if((Mix_temp[ij][j]&0x80)!=0){temp1=((Mix_temp[ij][j]<<1)&0x0ff)^0x1b;}
					else {temp1=(Mix_temp[ij][j]<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x40)!=0){temp2=((temp1<<1)&0x0ff)^0x1b;}
					else {temp2=(temp1<<1)&0x0ff;}
					if((Mix_temp[ij][j]&0x20)!=0){temp3=((temp2<<1)&0x0ff)^0x1b;}
					else {temp3=(temp2<<1)&0x0ff;}
					temp=Mix_temp[ij][j]^temp3;
				}

				Mix_temp1[i][j]^=temp;
			}
		}
	}
	out<<"列混合后"<<endl;//////////-------------------------------------------------
	for( i=0;i<4;i++){
		for(int j=0;j<4;j++){
			C1[2*i+8*j]=(Mix_temp1[i][j]>>4)&0x0f;
			C1[2*i+8*j+1]=Mix_temp1[i][j]& 0x00f;
		}
	}
	//------------------------------------------------------
	for( i=0;i<32;i++)
	{
		out<<C1[i]<<' ';
	}
/*	for( i=0;i<4;i++){
		for(int j=0;j<4;j++){
			out<<Mix_temp1[j][i]<<' ';
		}
	}*/
	//-----------------------------------------------------
	out<<endl;//////////////////////////////////-------------------------------------
	
}

void CAESDlg::makekey()
{
	for(int i=1;i<=10;i++){
		//out<<"key"<<i<<endl;//////////-------------
		nextkey(i);
		for(int j=0;j<32;j++){
			key[i][j]=K[j];
			//out<<key[i][j]<<' ';///-----------------
		}
		//out<<endl;///////---------------------------		
	}
}

void CAESDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	m_menwen="3243f6ad885a308d313198a2e0370734";
	   m_key="2b7e151628aed2a6abf7158809cf4f3c";
	m_miwen="";
	UpdateData(false);
	
}

⌨️ 快捷键说明

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