www.pudn.com > endecipher.rar > Dlg_Aessbox.cpp


// Dlg_Aessbox.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Cipher.h" 
#include "Dlg_Aessbox.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDlg_Aessbox dialog 
 
char Hex[16]={  '0', '1', '2', '3', '4',  
		'5', '6','7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'  }; 
int sbox[256]; 
 
CDlg_Aessbox::CDlg_Aessbox(CWnd* pParent /*=NULL*/) 
	: CDialog(CDlg_Aessbox::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDlg_Aessbox) 
	m_sbox = _T(""); 
	m_Num1 = 0; 
	m_Num2 = _T(""); 
	//}}AFX_DATA_INIT 
} 
 
 
void CDlg_Aessbox::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDlg_Aessbox) 
	DDX_Text(pDX, IDC_EDIT_SBOX, m_sbox); 
	DDX_Text(pDX, IDC_EDIT_NUM1, m_Num1); 
	DDX_Text(pDX, IDC_EDIT_NUM2, m_Num2); 
	//}}AFX_DATA_MAP 
} 
 
 
BOOL CDlg_Aessbox::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
	UpdateData(true); 
	SetSBox(); 
	UpdateData(false); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
 
BEGIN_MESSAGE_MAP(CDlg_Aessbox, CDialog) 
	//{{AFX_MSG_MAP(CDlg_Aessbox) 
	ON_BN_CLICKED(IDC_BUTTON_Execute, OnBUTTONExecute) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDlg_Aessbox message handlers 
 
void CDlg_Aessbox::SetSBox() 
{ 
	 
	int log[256]; 
	int alog[256]; 
	 
	int ROOT=0x11B; 
	int box[256][8],cox[256][8]; 
	int i, j=0; 
	int A[8][8]={ {  1, 1, 1, 1, 1, 0, 0, 0  }, 
		 {  0, 1, 1, 1, 1, 1, 0, 0  }, {  0, 0, 1, 1, 1, 1, 1, 0  }, 
		 {  0, 0, 0, 1, 1, 1, 1, 1  }, {  1, 0, 0, 0, 1, 1, 1, 1  }, 
		 {  1, 1, 0, 0, 0, 1, 1, 1  }, {  1, 1, 1, 0, 0, 0, 1, 1  }, 
		 {  1, 1, 1, 1, 0, 0, 0, 1  }   
	}; 
	int c[8]={0, 1, 1, 0, 0, 0, 1, 1}; 
	alog[0]=1; // g(x) ^ 0 
	for(i=1;i<256;i++){ 
		j=(alog[i-1]<<1) ^ alog[i - 1]; // 乘 2 加 1 
		if((j&0x100)!=0) // 如果大于等于255, 模 m(x) 
			j^=ROOT; 
		alog[i]=j; // 计算出 alog[i] 
	} 
	for(i=1;i<255;i++){ 
		log[alog[i]]= i; // 在log表中放置对底 g(x) 的对数 
	} 
 
	int t; 
	memset(box,0,sizeof(box)); 
	box[1][7]=1; 
	for(i=2;i<256;i++){ 
		j=alog[255-log[i]]; 
		for(t=0;t<8;t++) 
			box[i][t]=((j>>(7-t))&0x01); 
	} 
 
	for(i=0;i<256;i++){ 
		for(t=0;t<8;t++){ 
			cox[i][t]=c[t]; 
			for(j=0;j<8;j++)  
				cox[i][t]^=A[t][j]*box[i][j];  
		} 
	} 
	for(i=0;i<256;i++){  
		sbox[i]=(cox[i][0]<< 7); 
		for(t=1;t<8;t++) 
			sbox[i]^=cox[i][t]<< (7 - t); 
	} 
	 
	m_sbox=""; 
 
	m_sbox+="\r\n"; 
	for(i=0;i<256;i++) 
	{ 
		m_sbox+=Hex[sbox[i]/16]; 
		m_sbox+=Hex[sbox[i]%16]; 
		 
		if(i%16==15) 
			m_sbox+="\r\n"; 
		else 
			m_sbox+="  "; 
	} 
} 
 
void CDlg_Aessbox::OnBUTTONExecute()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	m_Num2=""; 
	if(m_Num1>=0&&m_Num1<=255){ 
		m_Num2+=Hex[sbox[m_Num1]/16]; 
		m_Num2+=Hex[sbox[m_Num1]%16]; 
	} 
	UpdateData(false); 
}