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


// Dlg_Caeser.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Cipher.h" 
#include "Dlg_Caeser.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDlg_Caeser dialog 
 
 
CDlg_Caeser::CDlg_Caeser(CWnd* pParent /*=NULL*/) 
	: CDialog(CDlg_Caeser::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDlg_Caeser) 
	m_key = _T("1"); 
	m_cipher = _T(""); 
	m_plain = _T(""); 
	m_t = 0; 
	m_path_read = _T(""); 
	m_path_write = _T(""); 
	//}}AFX_DATA_INIT 
} 
 
 
void CDlg_Caeser::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDlg_Caeser) 
	DDX_Control(pDX, IDC_BUTTON_OpenP, m_openp); 
	DDX_Control(pDX, IDC_BUTTON_OpenC, m_openc); 
	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_EDIT_CF, m_FC); 
	DDX_Control(pDX, IDC_EDIT_PF, m_FP); 
	DDX_Control(pDX, IDC_STATIC_File, m_File); 
	DDX_Control(pDX, IDC_STATIC_String, m_String); 
	DDX_Control(pDX, IDC_EDIT_PlainS, m_SP); 
	DDX_Control(pDX, IDC_EDIT_CipherS, m_SC); 
	DDX_Text(pDX, IDC_EDIT_Key, m_key); 
	DDX_Text(pDX, IDC_EDIT_CipherS, m_cipher); 
	DDX_Text(pDX, IDC_EDIT_PlainS, m_plain); 
	DDX_Radio(pDX, IDC_RADIO_S, m_t); 
	DDX_Text(pDX, IDC_EDIT_PF, m_path_read); 
	DDX_Text(pDX, IDC_EDIT_CF, m_path_write); 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CDlg_Caeser, CDialog) 
	//{{AFX_MSG_MAP(CDlg_Caeser) 
	ON_BN_CLICKED(IDC_BUTTON_Encipher, OnBUTTONEncipher) 
	ON_BN_CLICKED(IDC_RADIO_F, OnRadioF) 
	ON_BN_CLICKED(IDC_RADIO_S, OnRadioS) 
	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_Caeser message handlers 
 
void CDlg_Caeser::OnRadioF()  
{ 
	// TODO: Add your control notification handler code here 
	m_t=1; 
	m_SP.EnableWindow(false); 
	m_SC.EnableWindow(false); 
	m_String.EnableWindow(false); 
	m_sc.EnableWindow(false); 
	m_sp.EnableWindow(false); 
 
	m_FP.EnableWindow(true); 
	m_FC.EnableWindow(true); 
	m_File.EnableWindow(true); 
	m_openp.EnableWindow(true); 
	m_openc.EnableWindow(true); 
	m_fp.EnableWindow(true); 
	m_fc.EnableWindow(true); 
	UpdateData(true); 
	m_cipher=""; 
	m_plain=""; 
	UpdateData(false); 
} 
 
void CDlg_Caeser::OnRadioS()  
{ 
	// TODO: Add your control notification handler code here 
	m_t=0; 
	m_SP.EnableWindow(true); 
	m_SC.EnableWindow(true); 
	m_String.EnableWindow(true); 
	m_sc.EnableWindow(true); 
	m_sp.EnableWindow(true); 
 
	m_FP.EnableWindow(false); 
	m_FC.EnableWindow(false); 
	m_File.EnableWindow(false); 
	m_openp.EnableWindow(false); 
	m_openc.EnableWindow(false); 
	m_fp.EnableWindow(false); 
	m_fc.EnableWindow(false); 
	UpdateData(true); 
	m_path_read=""; 
	m_path_write=""; 
	UpdateData(false); 
} 
 
char * CDlg_Caeser::Encipher(CString a,int k)   //加密算法 
{ 
	int n,i; 
	n=strlen(a); 
	char *b=new char [n+1]; 
	for(i=0;i<=n-1;i++) 
	{ 
		if(a[i]>='a'&&a[i]<='z') 
			b[i]='a'+(a[i]-'a'+k)%26; 
		else if(a[i]>='A'&&a[i]<='Z') 
			b[i]='A'+(a[i]-'A'+k)%26; 
		else  
			b[i]=a[i]; 
	} 
	b[n]='\0'; 
	return b; 
}  //加密算法 
 
char * CDlg_Caeser::Decipher(CString a,int k)   //解密算法 
{ 
	int n,i; 
	n=strlen(a); 
	char *b=new char [n+1]; 
	for(i=0;i<=n-1;i++) 
	{ 
		if(a[i]>='a'&&a[i]<='z') 
			b[i]='a'+(a[i]-'a'-k+26)%26; 
		else if(a[i]>='A'&&a[i]<='Z') 
			b[i]='A'+(a[i]-'A'-k+26)%26; 
		else  
			b[i]=a[i]; 
	} 
	b[n]='\0'; 
	return b; 
}  //解密算法 
 
void CDlg_Caeser::OnBUTTONEncipher()  
{ 
	int k; 
	char a[2]=""; 
	UpdateData(true); 
	CString s; 
	if(strcmp(m_key,a)==0) 
		MessageBox("Input a integer!"); 
	else{ 
		k=atoi(m_key); 
		if(m_t==0){                //文本框输入输出 
			m_cipher=""; 
			m_cipher+=Encipher(m_plain,k); 
		} 
		else if(m_t==1)            //文件读入输出 
		{               
			CString s; 
			char *b; 
			bool fear,dear; 
			fear=file.Fread(s,m_path_read); 
			if(!fear) 
				return; 
			else 
			{ 
				b=Encipher(s,k); 
				dear=file.Fwrite(b,m_path_write); 
				if(!dear) 
					return ; 
			} 
		} 
		else 
		{ 
			MessageBox("Elect a manner!"); 
		} 
	} 
	UpdateData(false); 
} 
 
void CDlg_Caeser::OnBUTTONDecipher()  
{ 
	int k; 
	char a[2]=""; 
	UpdateData(true); 
	if(strcmp(m_key,a)==0) 
		MessageBox("Input a integer!"); 
	else 
	{ 
		k=atoi(m_key);	 
		if(m_t==0){              //文本框输入输出 
			m_plain=""; 
			m_plain+=Decipher(m_cipher,k); 
		} 
		else if(m_t==1)         //文件读入输出 
		{ 
			CString s; 
			char *b; 
			bool fear,dear; 
			fear=file.Fread(s,m_path_write); 
			if(!fear) 
				return; 
			else 
			{ 
				b=Decipher(s,k); 
				dear=file.Fwrite(b,m_path_read); 
				if(!dear) 
					return ; 
			} 
		} 
		else 
		{ 
			MessageBox("Elect a manner!"); 
		} 
	} 
	UpdateData(false); 
} 
 
void CDlg_Caeser::OnBUTTONOpenP()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	file.Path(m_path_read); 
	UpdateData(false); 
} 
 
void CDlg_Caeser::OnBUTTONOpenC()  
{ 
	// TODO: Add your control notification handler code here 
	UpdateData(true); 
	file.Path(m_path_write); 
	UpdateData(false); 
}