www.pudn.com > endecipher.rar > Dlg_Vigenere.cpp
// Dlg_Vigenere.cpp : implementation file
//
#include "stdafx.h"
#include "Cipher.h"
#include "Dlg_Vigenere.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlg_Vigenere dialog
CDlg_Vigenere::CDlg_Vigenere(CWnd* pParent /*=NULL*/)
: CDialog(CDlg_Vigenere::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlg_Vigenere)
m_path_c = _T("");
m_key = _T("fast");
m_t = 0;
m_path_p = _T("");
m_plain = _T("");
m_cipher = _T("");
//}}AFX_DATA_INIT
}
void CDlg_Vigenere::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlg_Vigenere)
DDX_Control(pDX, IDC_EDIT_PF, m_FP);
DDX_Control(pDX, IDC_STATIC_String, m_s);
DDX_Control(pDX, IDC_STATIC_SP, m_sp);
DDX_Control(pDX, IDC_STATIC_SC, m_sc);
DDX_Control(pDX, IDC_STATIC_File, m_f);
DDX_Control(pDX, IDC_STATIC_FC, m_fc);
DDX_Control(pDX, IDC_STATIC_FP, m_fp);
DDX_Control(pDX, IDC_EDIT_PlainS, m_SP);
DDX_Control(pDX, IDC_EDIT_CipherS, m_SC);
DDX_Control(pDX, IDC_EDIT_CF, m_FC);
DDX_Control(pDX, IDC_BUTTON_OpenP, m_openp);
DDX_Control(pDX, IDC_BUTTON_OpenC, m_openc);
DDX_Text(pDX, IDC_EDIT_CF, m_path_c);
DDX_Text(pDX, IDC_EDIT_Key, m_key);
DDX_Radio(pDX, IDC_RADIO_S, m_t);
DDX_Text(pDX, IDC_EDIT_PF, m_path_p);
DDX_Text(pDX, IDC_EDIT_PlainS, m_plain);
DDX_Text(pDX, IDC_EDIT_CipherS, m_cipher);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlg_Vigenere, CDialog)
//{{AFX_MSG_MAP(CDlg_Vigenere)
ON_BN_CLICKED(IDC_BUTTON_Encipher, OnBUTTONEncipher)
ON_BN_CLICKED(IDC_BUTTON_Decipher, OnBUTTONDecipher)
ON_BN_CLICKED(IDC_RADIO_S, OnRadioS)
ON_BN_CLICKED(IDC_RADIO_F, OnRadioF)
ON_BN_CLICKED(IDC_BUTTON_OpenP, OnBUTTONOpenP)
ON_BN_CLICKED(IDC_BUTTON_OpenC, OnBUTTONOpenC)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlg_Vigenere message handlers
char * CDlg_Vigenere::Encipher(CString a,CString k)
{
char *b;
int n,i,m,j;
n=strlen(a);
m=strlen(k);
b=new char[n+1];
j=0;
for(i=0;i<=n-1;i++)
{
if(a[i]>='a'&&a[i]<='z'){
b[i]='a'+(tolower(a[i])+tolower(k[j++])-2*'a')%26;
if(j>=m)
j%=m;
}
else if(a[i]>='A'&&a[i]<='Z'){
b[i]='A'+(tolower(a[i])+tolower(k[j++])-2*'a')%26;
if(j>=m)
j%=m;
}
else
b[i]=a[i];
}
b[n]='\0';
return b;
}
char * CDlg_Vigenere::Decipher(CString a,CString k)
{
char *b;
int n,i,j,m;
n=strlen(a);
m=strlen(k);
b=new char[n+1];
j=0;
for(i=0;i<=n-1;i++)
{
if(a[i]>='a'&&a[i]<='z'){
b[i]='a'+(tolower(a[i])-tolower(k[j++])+26)%26;
if(j>=m)
j%=m;
}
else if(a[i]>='A'&&a[i]<='Z'){
b[i]='A'+(tolower(a[i])-tolower(k[j++])+26)%26;
if(j>=m)
j%=m;
}
else
b[i]=a[i];
}
b[n]='\0';
return b;
}
void CDlg_Vigenere::OnBUTTONEncipher()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(strlen(m_key)==0)
MessageBox("Input a key word!");
else{
m_key;
if(m_t==0){ //文本框输入输出
m_cipher="";
m_cipher+=Encipher(m_plain,m_key);
}
else if(m_t==1) //文件读入输出
{
CString s;
char *b;
bool fear,dear;
fear=file.Fread(s,m_path_p);
if(!fear)
return;
else
{
b=Encipher(s,m_key);
dear=file.Fwrite(b,m_path_c);
if(!dear)
return ;
}
}
else
{
MessageBox("Elect a manner!");
}
}
UpdateData(false);
}
void CDlg_Vigenere::OnBUTTONDecipher()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(strlen(m_key)==0)
MessageBox("Input a key word!");
else{
m_key;
if(m_t==0){ //文本框输入输出
m_plain="";
m_plain+=Decipher(m_cipher,m_key);
}
else if(m_t==1) //文件读入输出
{
CString s;
char *b;
bool fear,dear;
fear=file.Fread(s,m_path_c);
if(!fear)
return;
else
{
b=Decipher(s,m_key);
dear=file.Fwrite(b,m_path_p);
if(!dear)
return ;
}
}
else
{
MessageBox("Elect a manner!");
}
}
UpdateData(false);
}
void CDlg_Vigenere::OnRadioS()
{
// TODO: Add your control notification handler code here
m_t=0;
m_SP.EnableWindow(true);
m_SC.EnableWindow(true);
m_s.EnableWindow(true);
m_sc.EnableWindow(true);
m_sp.EnableWindow(true);
m_FP.EnableWindow(false);
m_FC.EnableWindow(false);
m_f.EnableWindow(false);
m_openp.EnableWindow(false);
m_openc.EnableWindow(false);
m_fp.EnableWindow(false);
m_fc.EnableWindow(false);
UpdateData(true);
m_path_c="";
m_path_p="";
UpdateData(false);
}
void CDlg_Vigenere::OnRadioF()
{
// TODO: Add your control notification handler code here
m_t=1;
m_SP.EnableWindow(false);
m_SC.EnableWindow(false);
m_s.EnableWindow(false);
m_sc.EnableWindow(false);
m_sp.EnableWindow(false);
m_FP.EnableWindow(true);
m_FC.EnableWindow(true);
m_f.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_Vigenere::OnBUTTONOpenP()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_path_p);
UpdateData(false);
}
void CDlg_Vigenere::OnBUTTONOpenC()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_path_c);
UpdateData(false);
}