⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getdlg.cpp

📁 该程序可实现对文本文件的加密
💻 CPP
字号:
// getDlg.cpp : implementation file
//

#include "stdafx.h"
#include "get.h"
#include "getDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGetDlg dialog

CGetDlg::CGetDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGetDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGetDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGetDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGetDlg)
	DDX_Control(pDX, IDC_EDIT3, y);
	DDX_Control(pDX, IDC_EDIT2, l);
	DDX_Control(pDX, IDC_EDIT1, x);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGetDlg, CDialog)
	//{{AFX_MSG_MAP(CGetDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGetDlg message handlers

BOOL CGetDlg::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_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGetDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGetDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGetDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CGetDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	x.GetWindowText(key,9);
	y.GetWindowText(filepath,H);
	int i,num;
	for(i=0;key[i]!='\0';i++);
    if(i!=8){l.SetWindowText("密码长度错误!解密失败");return;}
	else    l.SetWindowText("解密成功啦");

	mima();
	FILE *file;
	file=fopen("C:\\hold.txt","rb");
	if(file!=NULL)
	{
		while(!feof(file))
		{
			num=fread(sec1,sizeof(char),8*HH,file);
			sec1[num]='\0';
			miwen();
    if(nsec==0){l.SetWindowText("密文长度为0 解密失败 ");return;}
	if(nsec%8!=0){l.SetWindowText("密文长度不是8的倍数!    解密失败 ");return;}

	jiemi();

		}
	}
	fclose(file);
	
	
}

void CGetDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();	
}

void CGetDlg::mima()
{
   keyToBit();
   keyPC_1();
   keyP();

}


/*将code11[]中的8bit字符转换为int放在reg中*/
void CGetDlg::keyToBit()
{
int i,j,k;
for(i=0;i<8;i++){
	k=key[i];
	if(k<0)k=k+256;
	for(j=7;j>=0;j--){
		reg[i*8+j]=k%2;
		k=k/2;       }
}
}              


/*将code1[]中的8bit字符转换为int放在reg中*/
void CGetDlg::charToBit()
{
char c[8];
int i,j,k;
  for(i=0;i<8;i++)
   c[i]=code1[n1+i];
     n1+=8;
for(i=7;i>=0;i--){
k=(int)(c[i]);
if(k<0)k=k+256;
for(j=7;j>=0;j--){
reg[8*i+j]=k%2;
k=k/2;}}
return;}

/*密钥pc-1置换,将reg[64]中的密钥转换到key1[56]*/
void CGetDlg::keyPC_1()
{
int i;
for(i=0;i<56;i++)
key1[i]=reg[pc1[i]-1];
return;}

/*密钥pc-2转换,将密钥key1[56]转换后放在key2[16][48]中*/
void CGetDlg::keyPC_2(int a[],int i)
{
int j;
for(j=0;j<48;j++)
key2[i][j]=a[pc2[j]-1];
return;}

/*将key1[]中的密钥转换后放在key2[16][48]中*/
void CGetDlg::keyP()
{
int i,j,k,g;
for(i=0;i<16;i++){
    if(m[i]==1)             {
           k=key1[0];
           for(j=0;j<55;j++)key1[j]=key1[j+1];
           key1[55]=key1[27];
           key1[27]=k;      }
    else                           {
           k=key1[0];
           g=key1[1];
           for(j=0;j<54;j++)key1[j]=key1[j+2];
           key1[55]=key1[27];
           key1[54]=key1[26];
           key1[26]=k;
           key1[27]=g;      }
    keyPC_2(key1,i);   }
return;}

void CGetDlg::mingwen()
{n11=0;
for(pp=0;code1[pp]!='\0';pp++);
n11=pp;
pp=pp/8;
}


void CGetDlg::miwen()
{
pp=0;nsec=0;
for(nsec=0;sec1[nsec]!='\0';nsec++)
	sec[nsec]=(int)(sec1[nsec])-48;
pp=nsec/64;
n11=nsec/8;
}

/*IP置换,将reg[64]中的int 转换放到mreg[64]中*/
void CGetDlg::IPConverse()
{   int i;
	for(i=0;i<64;i++)
mreg[i]=reg[ip[i]-1];}


/*G函数,i表示加密圈数*/
void CGetDlg::GT(int t)
{
    ET();
    KI(t);
    SBox();
    PConverse();
}
/*异或函数:L:P->R*/
void CGetDlg::YT()
{
int i;
for(i=0;i<32;i++)
R[i]=(P[i]+L[i])%2;
}


/*E扩展,R[32]->E[48]*/
void CGetDlg::ET()
{
int i;
for(i=0;i<48;i++)
E[i]=R[e[i]-1];
}


/*异或密钥,E[48]->E[48]*/
void CGetDlg::KI(int t)
{
int v;
for(v=0;v<48;v++)
E[v]=(E[v]+key2[t][v])%2;
}

/*P置换,S[32]->P[32]*/
void CGetDlg::PConverse()
{
int i;
for(i=0;i<32;i++)
P[i]=S[p[i]-1];
}


/*S盒,由E[48]->S[32]*/
void CGetDlg::SBox()
{
int v,u=0,w,h,l,y,z=0;
for(v=0;v<8;v++){
  h=2*E[u]+E[u+5];
  l=8*E[u+1]+4*E[u+2]+2*E[u+3]+E[u+4];
  w=s[v][h][l];
  for(y=3;y>=0;y--){
   S[z+y]=w%2;
   w=w/2;}
  z=z+4;
  u=u+6;}
return;}


/*将mreg[]中的64int 转化为char放在code1[]中*/
void CGetDlg::bitToChar()
{
int i,j,k,q;
for(i=0;i<8;i++){k=0;
    for(j=0;j<8;){
q=8*i+j;
k=2*k+mreg[q];
j++;}
code1[n1++]=(char)(k);}
}


void CGetDlg::jiemi()
{ int d,t,x,i;

n1=0;
t=n11-8*pp;                            /*  短块处理*/
if(t!=0){
  for(i=63;i>=0;i--)
    reg[i]=sec[--nsec];
  IPConverse();
  for(t=0;t<32;t++)       {
    L[t]=mreg[t];
    RR[t]=R[t]=mreg[t+32];}
  for(t=0;t<16;t++){
    for(i=0;i<32;i++)RR[i]=R[i];
    GT(15-t);
    YT();
    for(x=0;x<32;x++)
       L[x]=RR[x];
           }
    for(x=0;x<32;x++)   {
        reg[x]=R[x];
    reg[x+32]=L[x]; }
    FIPConverse();

    for(d=0;d<64-8*(n11-8*pp);d++)
      sec[nsec++]=mreg[d];
    n1=n11-8;
    bitToChar();
    n1=0;
             }
                                                                
nsec=0;
for(d=0;d<pp;d++){
for(t=0;t<64;t++)
reg[t]=sec[nsec++];

IPConverse();

for(t=0;t<32;t++){
L[t]=mreg[t];
RR[t]=R[t]=mreg[t+32];}

for(t=0;t<16;t++){
    for(i=0;i<32;i++)RR[i]=R[i];
    GT(15-t);
    YT();

    for(x=0;x<32;x++)
       L[x]=RR[x];
}

    for(x=0;x<32;x++)   {
        reg[x]=R[x];
        reg[x+32]=L[x];}
    FIPConverse();

    bitToChar();
}
code1[n11+1]='\0';
//w.SetWindowText(code1);
t=strlen(code1);
FILE *filew;
filew=fopen(filepath,"ab");
if(filew!=NULL)
{
	fwrite(code1,sizeof(char),t,filew);
}
fclose(filew);
}

void CGetDlg::FIPConverse()
{
int i;
for(i=0;i<64;i++)
mreg[i]=reg[fip[i]-1];
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -