www.pudn.com > endecipher.rar > Dlg_Lfsr.cpp
// Dlg_Lfsr.cpp : implementation file
//
#include "stdafx.h"
#include "Cipher.h"
#include "Dlg_Lfsr.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlg_Lfsr dialog
CDlg_Lfsr::CDlg_Lfsr(CWnd* pParent /*=NULL*/)
: CDialog(CDlg_Lfsr::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlg_Lfsr)
m_t = 0;
m_cfpath = _T("");
m_pfpath = _T("");
m_initial = _T("0f");
m_feedback = _T("12");
m_size = 1;
m_ifo1 = _T("(Input 2 numbers in Hex!)");
m_ifo2 = _T("(Input 2 numbers in Hex!)");
m_pstring = _T("");
m_cstring = _T("");
//}}AFX_DATA_INIT
}
void CDlg_Lfsr::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlg_Lfsr)
DDX_Control(pDX, IDC_SIZE_COMBOBOXEX, m_s);
DDX_Control(pDX, IDC_EDIT_PF, m_pfp);
DDX_Control(pDX, IDC_EDIT_CipherS, m_cs);
DDX_Control(pDX, IDC_STATIC_SP, m_sp);
DDX_Control(pDX, IDC_STATIC_SC, m_sc);
DDX_Control(pDX, IDC_STATIC_FC, m_fc);
DDX_Control(pDX, IDC_STATIC_FP, m_fp);
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_CF, m_cfp);
DDX_Control(pDX, IDC_BUTTON_OpenP, m_openpf);
DDX_Control(pDX, IDC_BUTTON_OpenC, m_opencf);
DDX_Radio(pDX, IDC_RADIO_S, m_t);
DDX_Text(pDX, IDC_EDIT_CF, m_cfpath);
DDX_Text(pDX, IDC_EDIT_PF, m_pfpath);
DDX_Text(pDX, IDC_INITIAL_EDIT, m_initial);
DDX_Text(pDX, IDC_FEEDBACK_EDIT, m_feedback);
DDX_CBIndex(pDX, IDC_SIZE_COMBOBOXEX, m_size);
DDX_Text(pDX, IDC_EDIT_IFO1, m_ifo1);
DDX_Text(pDX, IDC_EDIT_IFO2, m_ifo2);
DDX_Text(pDX, IDC_EDIT_PlainS, m_pstring);
DDX_Text(pDX, IDC_EDIT_CipherS, m_cstring);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlg_Lfsr, CDialog)
//{{AFX_MSG_MAP(CDlg_Lfsr)
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)
ON_BN_CLICKED(IDC_SET_BUTTON, OnSetButton)
ON_CBN_SELCHANGE(IDC_SIZE_COMBOBOXEX, OnSelchangeSizeComboboxex)
ON_BN_CLICKED(IDC_BUTTON_Encipher, OnBUTTONEncipher)
ON_BN_CLICKED(IDC_BUTTON_Decipher, OnBUTTONDecipher)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlg_Lfsr message handlers
BOOL CDlg_Lfsr::OnInitDialog()
{
CDialog::OnInitDialog();
OnSetButton();
return TRUE; // return TRUE unless you set the focus to a control
}
void CDlg_Lfsr::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_ps.EnableWindow(true);
m_cs.EnableWindow(true);
m_pfp.EnableWindow(false);
m_cfp.EnableWindow(false);
m_file.EnableWindow(false);
m_openpf.EnableWindow(false);
m_opencf.EnableWindow(false);
m_fc.EnableWindow(false);
m_fp.EnableWindow(false);
UpdateData(true);
m_cfpath="";
m_pfpath="";
UpdateData(false);
}
void CDlg_Lfsr::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_ps.EnableWindow(false);
m_cs.EnableWindow(false);
m_pfp.EnableWindow(true);
m_cfp.EnableWindow(true);
m_file.EnableWindow(true);
m_openpf.EnableWindow(true);
m_opencf.EnableWindow(true);
m_fc.EnableWindow(true);
m_fp.EnableWindow(true);
UpdateData(true);
m_cstring="";
m_pstring="";
UpdateData(false);
}
void CDlg_Lfsr::OnBUTTONOpenP()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_pfpath);
UpdateData(false);
}
void CDlg_Lfsr::OnBUTTONOpenC()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_cfpath);
UpdateData(false);
}
bool CDlg_Lfsr::OnSetButton()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(m_size<0)
{
AfxMessageBox("Select a size!");
return false;
}
else
{
int i,l1,l2;
l1=strlen(m_initial);
l2=strlen(m_feedback);
if(l1!=m_size+1||l2!=m_size+1)
{
AfxMessageBox("Initial or Feedback set error!");
return false;
}
else
{
for(i=0;i<=m_size;i++)
{
if(isdigit(m_initial[i]) //数字0~9
||(islower(m_initial[i])&&m_initial[i]>='a'&&m_initial[i]<='f') //字母a~f
||(isupper(m_initial[i])&&m_initial[i]>='A'&&m_initial[i]<='F')) //字母A~F
;
else
{
AfxMessageBox("Initial or Feedback set error!");
return false;
}
if(isdigit(m_feedback[i])
||(islower(m_feedback[i])&&m_feedback[i]>='a'&&m_feedback[i]<='f')
||(isupper(m_feedback[i])&&m_feedback[i]>='A'&&m_feedback[i]<='F'))
;
else
{
AfxMessageBox("Initial or Feedback set error!");
return false;
}
}
}
}
UpdateData(false);
return true;
}
void CDlg_Lfsr::OnSelchangeSizeComboboxex()
{
// TODO: Add your control notification handler code here
m_size=m_s.GetCurSel();
CString s;
s.Format("%d",m_size+1);
UpdateData(true);
m_ifo1="";
m_ifo2="";
m_ifo1+="(Input ";
m_ifo1+=s;
m_ifo1+=" numbers in Hex!)";
m_ifo2=m_ifo1;
UpdateData(false);
}
char * CDlg_Lfsr::Encipher(CString a)
{
int lr,i,j,k,t;
bool *b;
char *c,*key;
lr=strlen(a);
c=new char [8*(lr+m_size+2)];
key=new char [8*(lr+m_size+2)];
b=new bool [8*(m_size+1)];
k=0;
for(i=0;i<=m_size;i++) //反馈信息处理
{
if(isdigit(m_feedback[i]))
t=m_feedback[i]-'0';
if(islower(m_feedback[i]))
t=m_feedback[i]-'a'+10;
if(isupper(m_feedback[i]))
t=m_feedback[i]-'A'+10;
for(j=0;j<=3;j++){
if(t%2)
b[k]=true;
else
b[k]=false;
t>>=1;
k++;
}
}
k=0;
for(i=0;i<=m_size;i++)
{
if(isdigit(m_initial[i]))
t=m_initial[i]-'0';
if(islower(m_initial[i]))
t=m_initial[i]-'a'+10;
if(isupper(m_initial[i]))
t=m_initial[i]-'A'+10;
for(j=0;j<=3;j++){
key[k++]=t%2;
t>>=1;
}
}
for(;k<=8*lr;k++)
{
key[k]=0;
t=k-4*(m_size+1);
for(i=t;i>=1;
}
}
c[8*lr]='\0';
return c;
}
char * CDlg_Lfsr::Decipher(CString a)
{
int lr,i,j,k,t;
bool *b;
char *c,*key;
lr=strlen(a);
c=new char [lr/8+10];
key=new char [lr+8*(m_size+2)];
b=new bool [8*(m_size+1)];
k=0;
for(i=0;i<=m_size;i++) //反馈信息处理
{
if(isdigit(m_feedback[i]))
t=m_feedback[i]-'0';
if(islower(m_feedback[i]))
t=m_feedback[i]-'a'+10;
if(isupper(m_feedback[i]))
t=m_feedback[i]-'A'+10;
for(j=0;j<=3;j++){
if(t%2)
b[k]=true;
else
b[k]=false;
t>>=1;
k++;
}
}
k=0;
for(i=0;i<=m_size;i++)
{
if(isdigit(m_initial[i]))
t=m_initial[i]-'0';
if(islower(m_initial[i]))
t=m_initial[i]-'a'+10;
if(isupper(m_initial[i]))
t=m_initial[i]-'A'+10;
for(j=0;j<=3;j++){
key[k++]=t%2;
t>>=1;
}
}
for(;k<=lr;k++)
{
key[k]=0;
t=k-4*(m_size+1);
for(i=t;i=0;j--)
{
t=(t<<1)+(a[8*i+j]-'0')^key[8*i+j];
}
c[i]=t;
}
c[lr/8]='\0';
return c;
}
void CDlg_Lfsr::OnBUTTONEncipher()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(!OnSetButton())
return;
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_Lfsr::OnBUTTONDecipher()
{
// TODO: Add your control notification handler code here
UpdateData(true);
if(!OnSetButton())
return;
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);
}