www.pudn.com > endecipher.rar > Dlg_Rc4.cpp
// Dlg_Rc4.cpp : implementation file
//
#include "stdafx.h"
#include "Cipher.h"
#include "Dlg_Rc4.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlg_Rc4 dialog
int s[257];
CDlg_Rc4::CDlg_Rc4(CWnd* pParent /*=NULL*/)
: CDialog(CDlg_Rc4::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlg_Rc4)
m_cfpath = _T("");
m_pfpath = _T("");
m_cstring = _T("");
m_pstring = _T("");
m_t = 0;
m_k = _T("");
m_s = _T("");
m_k1 = 1;
m_k2 = 2;
m_k3 = 3;
m_k4 = 4;
//}}AFX_DATA_INIT
}
void CDlg_Rc4::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlg_Rc4)
DDX_Control(pDX, IDC_STATIC_File, m_file);
DDX_Control(pDX, IDC_STATIC_String, m_string);
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_PlainS, m_ps);
DDX_Control(pDX, IDC_EDIT_CipherS, m_cs);
DDX_Control(pDX, IDC_EDIT_PF, m_pfp);
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_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_K_EDIT, m_k);
DDX_Text(pDX, IDC_S_EDIT, m_s);
DDX_Text(pDX, IDC_EDIT1, m_k1);
DDX_Text(pDX, IDC_EDIT2, m_k2);
DDX_Text(pDX, IDC_EDIT3, m_k3);
DDX_Text(pDX, IDC_EDIT4, m_k4);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlg_Rc4, CDialog)
//{{AFX_MSG_MAP(CDlg_Rc4)
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_SET_BUTTON, OnSetButton)
ON_BN_CLICKED(IDC_BUTTON_OpenP, OnBUTTONOpenP)
ON_BN_CLICKED(IDC_BUTTON_OpenC, OnBUTTONOpenC)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlg_Rc4 message handlers
CString itos(int a)
{
CString s;
s.Format("%d",a);
return s;
}
void Swap(int &a,int &b)
{
int t;
t=a;
a=b;
b=t;
}
BOOL CDlg_Rc4::OnInitDialog()
{
CDialog::OnInitDialog();
OnSetButton();
return TRUE; // return TRUE unless you set the focus to a control
}
void CDlg_Rc4::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_Rc4::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);
}
char * CDlg_Rc4::Encipher(CString a) //RC4加密算法
{
int ci,key,lr,i,j,k,t;
char * c;
lr=strlen(a);
c=new char [8*lr+10];
i=0;
j=0;
for(k=0;k>=1;
}
}
c[8*lr]='\0';
return c;
}
char * CDlg_Rc4::Decipher(CString a) //RC4解密算法
{
int ci,key,lr,i,j,k,t;
char * c;
lr=strlen(a);
c=new char [lr/8+10];
i=0;
j=0;
for(k=0;k=8*i;t--)
ci=(ci<<1)+a[t]-'0';
i=(i+1)%256;
j=(j+s[i])%256;
Swap(s[i],s[j]);
t=(s[i]+s[j])%256;
key=s[t];
c[k]=ci^key;
}
c[lr/8]='\0';
return c;
}
void CDlg_Rc4::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_Rc4::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);
}
bool CDlg_Rc4::OnSetButton()
{
// TODO: Add your control notification handler code here
int i,j,k[4];
UpdateData(true);
k[0]=m_k1;
k[1]=m_k2;
k[2]=m_k3;
k[3]=m_k4;
for(i=0;i<=3;i++)
{
if(k[i]>=0&&k[i]<=255)
;
else
{
AfxMessageBox("Set key error!");
return false;
}
}
m_k="";
m_s="";
for(i=0;i<256;i++)
{
m_k+=itos(i);
m_k+=" ";
m_k+=itos(k[i%4]);
m_k+="\r\n";
s[i]=i;
}
j=0;
for(i=0;i<256;i++)
{
j=(j+s[i]+k[i%4])%256;
Swap(s[i],s[j]);
}
for(i=0;i<256;i++)
{
m_s+=itos(i);
m_s+=" ";
m_s+=itos(s[i]);
m_s+="\r\n";
}
UpdateData(false);
return true;
}
void CDlg_Rc4::OnBUTTONOpenP()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_pfpath);
UpdateData(false);
}
void CDlg_Rc4::OnBUTTONOpenC()
{
// TODO: Add your control notification handler code here
UpdateData(true);
file.Path(m_cfpath);
UpdateData(false);
}