📄 vc_sdes_child2.cpp
字号:
// VC_SDES_child2.cpp : implementation file
//
#include "stdafx.h"
#include "VC_SDES.h"
#include "VC_SDES_child2.h"
#include "VC_SDESDlg.h"
#include<math.h>
#include<fstream>
#include <string>
#include "mmsystem.h"
#pragma comment(lib,"Winmm.lib")
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// VC_SDES_child2 dialog
extern int work;
VC_SDES_child2::VC_SDES_child2(CWnd* pParent /*=NULL*/)
: CDialog(VC_SDES_child2::IDD, pParent)
{
//{{AFX_DATA_INIT(VC_SDES_child2)
m_input = _T("");
m_intext = _T("");
m_oper = -1;
m_key = _T("");
m_output = _T("");
m_outext = _T("");
//}}AFX_DATA_INIT
}
void VC_SDES_child2::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(VC_SDES_child2)
DDX_Control(pDX, IDC_music2, m_music2);
DDX_Text(pDX, IDC_INPUT_EDIT, m_input);
DDV_MaxChars(pDX, m_input, 256);
DDX_Text(pDX, IDC_INTEXT_EDIT, m_intext);
DDX_Radio(pDX, IDC_ENCRYPT_RADIO, m_oper);
DDX_Text(pDX, IDC_KEY_EDIT, m_key);
DDV_MaxChars(pDX, m_key, 10);
DDX_Text(pDX, IDC_OUTPUT_EDIT, m_output);
DDX_Text(pDX, IDC_OUTEXT_EDIT, m_outext);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(VC_SDES_child2, CDialog)
//{{AFX_MSG_MAP(VC_SDES_child2)
ON_BN_CLICKED(IDC_INPUT_BROWSE, OnInputBrowse)
ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton)
ON_BN_CLICKED(IDC_FLASH_BUTTON, OnFlashButton)
ON_BN_CLICKED(IDC_KEY_BROWSE, OnKeyBrowse)
ON_BN_CLICKED(IDC_back, Onback)
ON_BN_CLICKED(IDC_store, Onstore)
ON_BN_CLICKED(IDC_music2, Onmusic2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// VC_SDES_child2 message handlers
BOOL VC_SDES_child2::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon2, TRUE); // Set big icon
SetIcon(m_hIcon2, FALSE); // Set small icon
if (work)
m_music2.SetCheck(TRUE);
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void VC_SDES_child2::OnInputBrowse()
{
// TODO: Add your control notification handler code here
UpdateData();
CFileDialog dlg(TRUE,"txt",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"数据文件(*.txt)|*.txt|All Files (*.*)|*.*||",NULL);
if(dlg.DoModal()==IDOK)
{
char name[200];
CString pathname=dlg.GetPathName();
for(int i=0;i<pathname.GetLength();i++)
name[i]=pathname[i];
name[i]='\0';
ifstream file(name,ios::in);
string ch="";
string article="";
int work=1;
while (file>>ch)
{
if (ch!=" ")
{
int len=ch.length();
for (i=0;i<len;i++)
{
if (ch[i]>64&&ch[i]<81)
ch[i]+=32;
article+=ch[i];
}
}
}
file.close();
if (article.length()>256)
{
MessageBox("输入的明文长度太长,第256个字符以后的数据将会丢失!","ERROR");
article=article.substr(0,256);
m_input=article.c_str();
UpdateData(FALSE);
}
else
{
m_input=article.c_str();
UpdateData(FALSE);
}
//VERIFY(file.Open(dlg.GetPathName(),CFile::modeRead));
}
}
void VC_SDES_child2::OnStartButton()
{
// TODO: Add your control notification handler code here
UpdateData();
int state=GetCheckedRadioButton(IDC_ENCRYPT_RADIO,IDC_DECRYPT_RADIO);
int flag1=1;
int flag2=1;
if (m_key==""&&m_input=="")
MessageBox("您还没有输入密钥和明文","ERROR");
else
if (m_input=="")
MessageBox("您还没有输入明文","ERROR");
else
if (m_key=="")
MessageBox("您还没有输入密钥","ERROR");
else
if (state!=IDC_ENCRYPT_RADIO&&state!=IDC_DECRYPT_RADIO)
MessageBox("请选择加密或者解密!","ERROR");
else
{
for(int i=0;i<m_key.GetLength();i++)//判断输入密钥的正确性
{
if(m_key[i]!='0'&&m_key[i]!='1')
{
flag1=0;
break;
}
}//判断输入的正确性
for(i=0;i<m_input.GetLength();i++)
{
if((m_input[i]<97&&m_input[i]>80||m_input[i]>112||m_input[i]<65)&&m_input[i]!=32)
flag2=0;
if (!flag2)
break;
}
if(flag1==0||m_key.GetLength()<10)
MessageBox("输入只能为\"0\"和\"1\",且密钥为10位","ERROR");
else
if (flag2==0)
MessageBox("输入的明文中含有字母a~p或A~P之外的符号!","ERROR");
else
{
int key[10];//密钥
int k1[8],k2[8];//k1,k2
for(i=0;i<10;i++)
key[i]=(m_key[i]-48);//密钥
GetKey(key,k1,k2);
int text[8];//输入
string ch=(LPCTSTR)m_input;
string article_input="";
for (i=0;i<ch.length();i++)
{
if (ch[i]==32)
continue;
if (ch[i]>=65&&ch[i]<=80)
ch[i]+=32;
article_input+=ch[i];
}
if (article_input.length())
article_input+="a";
string number_input="";
string number_output="";
string article_output="";
for (i=0;i<article_input.length()/2;i++)
{
int in0=0,in1=0;
int n=-1;
do
{
n++;
switch(article_input[2*i+n])
{
case 97:
text[4*n]=0;text[4*n+1]=0;text[4*n+2]=0;text[4*n+3]=0;
break;
case 98:
text[4*n]=0;text[4*n+1]=0;text[4*n+2]=0;text[4*n+3]=1;
break;
case 99:
text[4*n]=0;text[4*n+1]=0;text[4*n+2]=1;text[4*n+3]=0;
break;
case 100:
text[4*n]=0;text[4*n+1]=0;text[4*n+2]=1;text[4*n+3]=1;
break;
case 101:
text[4*n]=0;text[4*n+1]=1;text[4*n+2]=0;text[4*n+3]=0;
break;
case 102:
text[4*n]=0;text[4*n+1]=1;text[4*n+2]=0;text[4*n+3]=1;
break;
case 103:
text[4*n]=0;text[4*n+1]=1;text[4*n+2]=1;text[4*n+3]=0;
break;
case 104:
text[4*n]=0;text[4*n+1]=1;text[4*n+2]=1;text[4*n+3]=1;
break;
case 105:
text[4*n]=1;text[4*n+1]=0;text[4*n+2]=0;text[4*n+3]=0;
break;
case 106:
text[4*n]=1;text[4*n+1]=0;text[4*n+2]=0;text[4*n+3]=1;
break;
case 107:
text[4*n]=1;text[4*n+1]=0;text[4*n+2]=1;text[4*n+3]=0;
break;
case 108:
text[4*n]=1;text[4*n+1]=0;text[4*n+2]=1;text[4*n+3]=1;
break;
case 109:
text[4*n]=1;text[4*n+1]=1;text[4*n+2]=0;text[4*n+3]=0;
break;
case 110:
text[4*n]=1;text[4*n+1]=1;text[4*n+2]=0;text[4*n+3]=1;
break;
case 111:
text[4*n]=1;text[4*n+1]=1;text[4*n+2]=1;text[4*n+3]=0;
break;
case 112:
text[4*n]=1;text[4*n+1]=1;text[4*n+2]=1;text[4*n+3]=1;
break;
}
}while (n<1);//输入字符转化成数字1或0
ch="";
for (int j=0;j<8;j++)
ch+=char(text[j]+48);
number_input+=ch;
if(state==IDC_ENCRYPT_RADIO)
{
//加密操作
Operate(text,k1,k2);
}
if(state==IDC_DECRYPT_RADIO)
{
//解密操作
Operate(text,k2,k1);
}
int out0=0,out1=0;
ch="";
for(j=0;j<8;j++)
ch+=(text[j]+48);
number_output+=ch;
for(j=0;j<4;j++)//获取输出所表示的字母
{
out0+=(text[j]*pow(2,3-j));
out1+=(text[j+4]*pow(2,3-j));
}
article_output+=char(out0+97);
article_output+=char(out1+97);
}
m_outext=article_output.c_str();
m_input1=m_input;
m_IP_1=m_output;
m_intext=number_input.c_str();
m_output=number_output.c_str();
}
}
UpdateData(FALSE);
}
void VC_SDES_child2::GetKey(int key[], int k1[], int k2[])
{
UpdateData();
char temp[12];
m_key1=m_key;
int k[10],kl[5],kr[5];
//P10置换
k[0]=key[2];k[1]=key[4];k[2]=key[1];k[3]=key[6];k[4]=key[3];
k[5]=key[9];k[6]=key[0];k[7]=key[8];k[8]=key[7];k[9]=key[5];
for(int j=0;j<10;j++)
temp[j]=k[j]+48;//
temp[j]='\0';
CString t(temp);
m_P10=t;
//置换
for(int i=0;i<4;i++)
kl[i]=k[i+1];
kl[4]=k[0];
for(i=5;i<9;i++)
kr[i-5]=k[i+1];
kr[4]=k[5];//分成左五位和右五位,已经左移了一位了。
char temp0[8],temp1[8];
for(i=0;i<5;i++)
{
temp0[i]=kl[i]+48;
temp1[i]=kr[i]+48;
}
temp0[i]='\0';
temp1[i]='\0';
CString t0(temp0);
CString t1(temp1);
m_LS1=t0+t1;//赋给控件
//K1的值
k1[0]=kr[0];k1[1]=kl[2];k1[2]=kr[1];k1[3]=kl[3];
k1[4]=kr[2];k1[5]=kl[4];k1[6]=kr[4];k1[7]=kr[3];
for(i=0;i<8;i++)
{
temp[i]=k1[i]+48;
}
temp[8]='\0';
CString t2(temp);
m_P8_1=t2;//赋给控件
m_K1=t2;//赋给控件
//求K2
//置换 直接从k的前后五位左移三位得到
kl[0]=k[3];kl[1]=k[4];kl[2]=k[0];kl[3]=k[1];kl[4]=k[2];
kr[0]=k[8];kr[1]=k[9];kr[2]=k[5];kr[3]=k[6];kr[4]=k[7];
for(i=0;i<5;i++)
{
temp0[i]=kl[i]+48;
temp1[i]=kr[i]+48;
}
temp0[i]='\0';
temp1[i]='\0';
CString t3(temp0);
CString t4(temp1);
m_LS2=t3+t4;
k2[0]=kr[0];k2[1]=kl[2];k2[2]=kr[1];k2[3]=kl[3];
k2[4]=kr[2];k2[5]=kl[4];k2[6]=kr[4];k2[7]=kr[3];
for(i=0;i<8;i++)
{
temp[i]=k2[i]+48;
}
temp[8]='\0';
CString t5(temp);
m_P8_2=t5;//赋给控件
m_K2=t5;//赋给控件
}
void VC_SDES_child2::Operate(int text[], int k1[], int k2[])
{
IP(text);
int L[4],R[4];
for(int i=0;i<4;i++)
{
L[i]=text[i];
R[i]=text[i+4];//分成前四位和后四位
}
FK(L,R,k1);
//将中间结果赋给控件
char temp[10];
for(i=0;i<4;i++)
{
temp[i]=L[i]+48;
temp[i+4]=R[i]+48;
}
temp[8]='\0';
CString t0(temp);
m_FK1=t0;//控件
//SW置换
for(i=0;i<4;i++)
{
temp[i]=R[i]+48;
temp[i+4]=L[i]+48;
}
temp[8]='\0';
CString t1(temp);
m_SW=t1;
FK(R,L,k2);
for(i=0;i<4;i++)
{
temp[i]=R[i]+48;
temp[i+4]=L[i]+48;
}
temp[8]='\0';
CString t2(temp);
m_FK2=t2;//控件
for(i=0;i<4;i++)
{
text[i]=R[i];
text[i+4]=L[i];
}
IP_1(text);
}
void VC_SDES_child2::IP(int text[])
{
int t[8];
for(int i=0;i<8;i++)
t[i]=text[i];
text[0]=t[1];text[1]=t[5];text[2]=t[2];text[3]=t[0];
text[4]=t[3];text[5]=t[7];text[6]=t[4];text[7]=t[6];
//赋给控件
char p[10];
for(i=0;i<8;i++)
p[i]=text[i]+48;
p[i]='\0';
CString tt(p);
m_IP=tt;
}
void VC_SDES_child2::IP_1(int text[])
{
int t[8];
for(int i=0;i<8;i++)
t[i]=text[i];
text[0]=t[3];text[1]=t[0];text[2]=t[2];text[3]=t[4];
text[4]=t[6];text[5]=t[1];text[6]=t[7];text[7]=t[5];
}
void VC_SDES_child2::FK(int L[], int R[], int k[])
{
int CR[4];//返回F函数后出来的值
F(R,k,CR);
for(int i=0;i<4;i++)
L[i]=L[i]^CR[i];
}
void VC_SDES_child2::F(int R[], int SK[], int CR[])
{
int t[8];
t[0]=R[3];t[1]=R[0];t[2]=R[1];t[3]=R[2];
t[4]=R[1];t[5]=R[2];t[6]=R[3];t[7]=R[0];
for(int i=0;i<8;i++)
t[i]=t[i]^SK[i];
int S0[4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2};
int S1[4][4]={0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
int temp[4];
temp[0]=S0[t[0]*2+t[3]*1][t[1]*2+t[2]*1]/2;
temp[1]=S0[t[0]*2+t[3]*1][t[1]*2+t[2]*1]%2;
temp[2]=S1[t[4]*2+t[7]*1][t[5]*2+t[6]*1]/2;
temp[3]=S1[t[4]*2+t[7]*1][t[5]*2+t[6]*1]%2;
CR[0]=temp[1];//P4置换
CR[1]=temp[3];
CR[2]=temp[2];
CR[3]=temp[0];
}
void VC_SDES_child2::OnFlashButton()
{
// TODO: Add your control notification handler code here
m_key="";
m_input="";
m_output="";
m_intext="";
m_outext="";
m_FK1="";
m_FK2="";
m_input1="";
m_IP="";
m_IP_1="";
m_K1="";
m_K2="";
m_key1="";
m_LS1="";
m_LS2="";
m_P10="";
m_P8_1="";
m_P8_2="";
m_SW="";
UpdateData(FALSE);
}
void VC_SDES_child2::OnKeyBrowse()
{
// TODO: Add your control notification handler code here
UpdateData();
CFileDialog m_MyOpenDialog(TRUE,"dat",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"数据文件(*.dat)|*.dat|All Files (*.*)|*.*||",NULL);
if(m_MyOpenDialog.DoModal()==IDOK)
{
char name[200];
char data[11];
CString pathname=m_MyOpenDialog.GetPathName();
for(int i=0;i<pathname.GetLength();i++)
name[i]=pathname[i];
name[i]='\0';
ifstream file(name,ios::binary);
file.read((char*)&data,sizeof(data));
data[10]='\0';
CString temp(data);
m_key=temp;
UpdateData(FALSE);
}
}
extern int choose;
void VC_SDES_child2::Onback()
{
// TODO: Add your control notification handler code here
OnOK();
CVC_SDESDlg dlg;
dlg.DoModal();
}
void VC_SDES_child2::Onstore()
{
// TODO: Add your control notification handler code here
UpdateData();
if (m_outext!="")
{
CFileDialog m_MyOpenDialog(FALSE,"txt","*.txt",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"数据文件(*.txt)|*.txt||",NULL);
if(m_MyOpenDialog.DoModal()==IDOK)
{
char name[200];
CString pathname=m_MyOpenDialog.GetPathName();
for(int i=0;i<pathname.GetLength();i++)
name[i]=pathname[i];
name[i]='\0';
string ch=m_outext;
ofstream file(name,ios::out);
file<<ch;
file.close();
}
}
else
{
MessageBox("没有可操作密文!","ERROR");
}
}
void VC_SDES_child2::Onmusic2()
{
// TODO: Add your control notification handler code here
if(m_music2.GetCheck())
{
PlaySound((LPCTSTR)IDR_WAVE1, AfxGetInstanceHandle(), SND_ASYNC|SND_RESOURCE|SND_LOOP );
work=1;
}
else
{
PlaySound(NULL,NULL,SND_ASYNC);
work=0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -