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

📄 desencryptiondlg.cpp

📁 DES加密解密文本文件,使用分组加密方法加密解密数据,分组长度为64位.
💻 CPP
字号:
// DESEncryptionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DESEncryption.h"
#include "DESEncryptionDlg.h"
#include "d3des.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

static unsigned long KnL[32] = { 0L };
static unsigned long KnR[32] = { 0L };
static unsigned long Kn3[32] = { 0L };
static unsigned char Df_Key[24] = {
	0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
	0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
	0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 };

static unsigned short bytebit[8]	= {
	0200, 0100, 040, 020, 010, 04, 02, 01 };

static unsigned long bigbyte[24] = {
	0x800000L,	0x400000L,	0x200000L,	0x100000L,
	0x80000L,	0x40000L,	0x20000L,	0x10000L,
	0x8000L,	0x4000L,	0x2000L,	0x1000L,
	0x800L, 	0x400L, 	0x200L, 	0x100L,
	0x80L,		0x40L,		0x20L,		0x10L,
	0x8L,		0x4L,		0x2L,		0x1L	};

/* Use the key schedule specified in the Standard (ANSI X3.92-1981). */

static unsigned char pc1[56] = {
	56, 48, 40, 32, 24, 16,  8,	 0, 57, 49, 41, 33, 25, 17,
	 9,  1, 58, 50, 42, 34, 26,	18, 10,  2, 59, 51, 43, 35,
	62, 54, 46, 38, 30, 22, 14,	 6, 61, 53, 45, 37, 29, 21,
	13,  5, 60, 52, 44, 36, 28,	20, 12,  4, 27, 19, 11,  3 };

static unsigned char totrot[16] = {
	1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };

static unsigned char pc2[48] = {
	13, 16, 10, 23,  0,  4,  2, 27, 14,  5, 20,  9,
	22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
	40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
	43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDESEncryptionDlg dialog

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

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

BEGIN_MESSAGE_MAP(CDESEncryptionDlg, CDialog)
	//{{AFX_MSG_MAP(CDESEncryptionDlg)
	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()

/////////////////////////////////////////////////////////////////////////////
// CDESEncryptionDlg message handlers

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

void CDESEncryptionDlg::OnEncrypt() 
{
	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 ("C:\\test\\plain.txt",CFile::modeReadWrite);
	lfile.Open ("C:\\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 CDESEncryptionDlg::OnDecrypt() 
{
	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 ("C:\\test\\cipher.txt",CFile::modeReadWrite);
	lfile.Open ("C:\\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 + -