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

📄 desdlg.cpp

📁 这是一个关于DES的小程序。用VC编的。不是很经典
💻 CPP
字号:
// DESDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DES.h"
#include "DESDlg.h"
#include "d3des.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()

/////////////////////////////////////////////////////////////////////////////
// CDESDlg dialog

CDESDlg::CDESDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDESDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDESDlg)
	m_key = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDESDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDESDlg)
	DDX_Text(pDX, IDC_KEY, m_key);
	DDV_MaxChars(pDX, m_key, 16);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDESDlg, CDialog)
	//{{AFX_MSG_MAP(CDESDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
	ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDESDlg message handlers

BOOL CDESDlg::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 CDESDlg::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 CDESDlg::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 CDESDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDESDlg::OnEncrypt() 
{
	// TODO: Add your control notification handler code here ok

	unsigned char key[9]={0},input[9]={0},output[9]={0};
	CFile pfile,lfile;
	int i;

	UpdateData(TRUE);
	if(m_key.GetLength ()!=16)
		AfxMessageBox("密钥长度不是8字节!");
	Char_Hex((unsigned char*)m_key.GetBuffer (17),key,8);
	pfile.Open ("g:\\test\\plain.txt",CFile::modeReadWrite);
	lfile.Open ("g:\\test\\cipher.txt",CFile::modeCreate|CFile::modeReadWrite);
	i=pfile.GetLength ()/8;
	if((pfile.GetLength ()%8)!=0)
		i++;
	if(pfile)
	{
		for(;i>0;i--)
		{
			memset(input,0x00,8);
			pfile.Read (input,8);
			my_des(input,output,key,EN0);
			lfile.Write (output,8);
		}
		pfile.Close ();
		lfile.Close ();
	}
}
	
////////////////////////////////
void CDESDlg::OnDecrypt() 
{
	// TODO: Add your control notification handler code here ok

	unsigned char key[9]={0},input[9]={0},output[9]={0};
	CFile pfile,lfile;
	int i;

	UpdateData(TRUE);
	if(m_key.GetLength ()!=16)
		AfxMessageBox("密钥长度不是8字节!");
	Char_Hex((unsigned char*)m_key.GetBuffer (17),key,8);
	pfile.Open ("g:\\test\\cipher.txt",CFile::modeReadWrite);
	lfile.Open ("g:\\test\\decrypt.txt",CFile::modeCreate|CFile::modeReadWrite);
	i=pfile.GetLength ()/8;
	if((pfile.GetLength ()%8)!=0)
		i++;
	if(pfile)
	{
		for(;i>0;i--)
		{
			memset(input,0x00,8);
			pfile.Read (input,8);
			my_des(input,output,key,DE1);
			lfile.Write (output,8);
		}
		pfile.Close ();
		lfile.Close ();
	}
}


void Char_Hex(unsigned char *bufin,unsigned char *bufout,int  len)
{
	int j=0;
	unsigned char temp1=0;
	unsigned char temp2=0;

	for(int i=0;i<len;i++)
	{
		temp1=bufin[j];
		temp2=bufin[j+1];
		if(temp1>=48&&temp1<=57)
			temp1-=48;
		else if(temp1>=97&&temp1<=102)
			temp1-=87;
		else if(temp1>=65 &&temp1<=70)
			temp1-=55;
		if(temp2>=48&&temp2<=57)
			temp2-=48;
		else if(temp2>=97&&temp2<=102)
			temp2-=87;
		else if(temp2>=65 &&temp2<=70)
			temp2-=55;
		bufout[i]=(temp1<<4)|temp2;
		j++;
		j++;
	}
}




void Hex_Char(unsigned char *bufin,unsigned char *bufout,int  len)
{
	int j=0;
	unsigned char temp1=0;
	unsigned char temp2=0;
	for(int i=0;i<len;i++)
	{
		temp1=bufin[i]&0x0F;
		temp2=bufin[i]>>4;
		if(temp1>=0&&temp1<=9)
			temp1+=48;
		else
			temp1+=87;
		if(temp2>=0&&temp2<=9)
			temp2+=48;
		else
			temp2+=87;
		bufout[j]=temp2;
		bufout[j+1]=temp1;
		j++;
		j++;
	}
}	

⌨️ 快捷键说明

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