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


// Dlg_Keyword.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Cipher.h" 
#include "Dlg_Keyword.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDlg_Keyword dialog 
int enkey[26];    //加密用的密钥 
int dekey[26];    //解密用的密钥 
bool bf[26]; 
 
CDlg_Keyword::CDlg_Keyword(CWnd* pParent /*=NULL*/) 
	: CDialog(CDlg_Keyword::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDlg_Keyword) 
	m_cfpath = _T(""); 
	m_pfpath = _T(""); 
	m_cstring = _T(""); 
	m_pstring = _T(""); 
	m_t = 0; 
	m_end = _T(""); 
	m_start = _T(""); 
	m_begin = _T("S"); 
	m_keyword = _T("KEYWORD"); 
	//}}AFX_DATA_INIT 
} 
 
 
void CDlg_Keyword::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDlg_Keyword) 
	DDX_Control(pDX, IDC_BUTTON_OpenP, m_openpf); 
	DDX_Control(pDX, IDC_BUTTON_OpenC, m_opencf); 
	DDX_Control(pDX, IDC_STATIC_SP, m_sp); 
	DDX_Control(pDX, IDC_STATIC_SC, m_sc); 
	DDX_Control(pDX, IDC_STATIC_FP, m_fp); 
	DDX_Control(pDX, IDC_STATIC_FC, m_fc); 
	DDX_Control(pDX, IDC_STATIC_String, m_string); 
	DDX_Control(pDX, IDC_STATIC_File, m_file); 
	DDX_Control(pDX, IDC_EDIT_PlainS, m_ps); 
	DDX_Control(pDX, IDC_EDIT_CipherS, m_cs); 
	DDX_Control(pDX, IDC_EDIT_CF, m_cfp); 
	DDX_Control(pDX, IDC_EDIT_PF, m_pfp); 
	DDX_Text(pDX, IDC_EDIT_CF, m_cfpath); 
	DDX_Text(pDX, IDC_EDIT_PF, m_pfpath); 
	DDX_Text(pDX, IDC_EDIT_CipherS, m_cstring); 
	DDX_Text(pDX, IDC_EDIT_PlainS, m_pstring); 
	DDX_Radio(pDX, IDC_RADIO_S, m_t); 
	DDX_Text(pDX, IDC_EDIT_END, m_end); 
	DDX_Text(pDX, IDC_EDIT_START, m_start); 
	DDX_Text(pDX, IDC_EDIT_BEGIN, m_begin); 
	DDV_MaxChars(pDX, m_begin, 1); 
	DDX_Text(pDX, IDC_EDIT_KEYWORD, m_keyword); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CDlg_Keyword, CDialog) 
	//{{AFX_MSG_MAP(CDlg_Keyword) 
	ON_BN_CLICKED(IDC_BUTTON_SET, OnButtonSet) 
	ON_BN_CLICKED(IDC_RADIO_S, OnRadioS) 
	ON_BN_CLICKED(IDC_RADIO_F, OnRadioF) 
	ON_BN_CLICKED(IDC_BUTTON_Encipher, OnBUTTONEncipher) 
	ON_BN_CLICKED(IDC_BUTTON_Decipher, OnBUTTONDecipher) 
	ON_BN_CLICKED(IDC_BUTTON_OpenP, OnBUTTONOpenP) 
	ON_BN_CLICKED(IDC_BUTTON_OpenC, OnBUTTONOpenC) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDlg_Keyword message handlers 
 
 
BOOL CDlg_Keyword::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
	OnButtonSet(); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
bool CDlg_Keyword::OnButtonSet()  
{ 
	// TODO: Add your control notification handler code here 
	int i,n,m,k,s,j,key[26]; 
	UpdateData(true); 
	m_start=""; 
	m_end=""; 
	m=strlen(m_begin); 
	if(m==0){ 
		AfxMessageBox("Set key error!"); 
		return false; 
	} 
	n=strlen(m_keyword); 
	for(i=0;i<=25;i++){ 
		key[i]=enkey[i]=dekey[i]=i; 
		bf[i]=true; 
	} 
	j=0; 
	for(i=0;i='a'&&a[i]<='z') 
		{ 
			k=a[i]-'a'; 
			b[i]='a'+enkey[k]; 
		} 
		else if(a[i]>='A'&&a[i]<='Z') 
		{ 
			k=a[i]-'A'; 
			b[i]='A'+enkey[k]; 
		} 
		else  
			b[i]=a[i]; 
	} 
	b[n]='\0'; 
	return b; 
} 
 
char * CDlg_Keyword::Decipher(CString a) 
{ 
	int n,i,k; 
	n=strlen(a); 
	char *b=new char [n+1]; 
	for(i=0;i<=n-1;i++) 
	{ 
		if(a[i]>='a'&&a[i]<='z') 
		{ 
			k=a[i]-'a'; 
			b[i]='a'+dekey[k]; 
		} 
		else if(a[i]>='A'&&a[i]<='Z') 
		{ 
			k=a[i]-'A'; 
			b[i]='A'+dekey[k]; 
		} 
		else  
			b[i]=a[i]; 
	} 
	b[n]='\0'; 
	return b; 
} 
 
void CDlg_Keyword::OnBUTTONEncipher()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	if(m_t==0){                //文本框输入输出 
		m_cstring=""; 
		m_cstring+=Encipher(m_pstring); 
	} 
	else if(m_t==1)            //文件读入输出 
	{               
		CString s; 
		char *b; 
		bool fear,dear; 
		fear=file.Fread(s,m_pfpath); 
		if(!fear) 
			return; 
		else 
		{ 
			b=Encipher(s); 
			dear=file.Fwrite(b,m_cfpath); 
			if(!dear) 
				return ; 
		} 
	} 
	else 
		MessageBox("Elect a manner!"); 
	UpdateData(false); 
} 
 
void CDlg_Keyword::OnBUTTONDecipher()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	if(m_t==0){                //文本框输入输出 
		m_pstring=""; 
		m_pstring+=Decipher(m_cstring); 
	} 
	else if(m_t==1)            //文件读入输出 
	{               
		CString s; 
		char *b; 
		bool fear,dear; 
		fear=file.Fread(s,m_cfpath); 
		if(!fear) 
			return; 
		else 
		{ 
			b=Decipher(s); 
			dear=file.Fwrite(b,m_pfpath); 
			if(!dear) 
				return ; 
		} 
	} 
	else 
		MessageBox("Elect a manner!"); 
	UpdateData(false); 
} 
 
void CDlg_Keyword::OnBUTTONOpenP()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	file.Path(m_pfpath); 
	UpdateData(false); 
} 
 
void CDlg_Keyword::OnBUTTONOpenC()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	file.Path(m_cfpath); 
	UpdateData(false); 
}