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

📄 blowfishprojdlg.cpp

📁 利用Blowfish对称加密算法对文件进行加密, 内含VC++ 6.0源码
💻 CPP
字号:
// BlowfishProjDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BlowfishProj.h"
#include "BlowfishProjDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CBlowfishProjDlg dialog

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

void CBlowfishProjDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBlowfishProjDlg)
	DDX_Control(pDX, IDC_PROGRESS, m_Pos);
	DDX_Radio(pDX, IDC_RADIO_ENCRYPT, m_IsEncrypt);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBlowfishProjDlg, CDialog)
	//{{AFX_MSG_MAP(CBlowfishProjDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_START, OnBtnStart)
	ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
	ON_BN_CLICKED(IDC_BTN_BROWSEIN, OnBtnBrowsein)
	ON_BN_CLICKED(IDC_BTN_BROWSEOUT, OnBtnBrowseout)
	ON_WM_DROPFILES()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBlowfishProjDlg message handlers

BOOL CBlowfishProjDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	m_IsEncrypt = 0;
	UpdateData(FALSE);

	::DragAcceptFiles(m_hWnd, true);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CBlowfishProjDlg::OnBtnStart() 
{
	CString strInFile;
	GetDlgItemText(IDC_EDIT_INFILE, strInFile);
	if(strInFile.IsEmpty())
	{
		AfxMessageBox("请指定输入文件!");
		return ;
	}

	CString strOutFile;
	GetDlgItemText(IDC_EDIT_OUTFILE, strOutFile);
	if(strOutFile.IsEmpty())
	{
		AfxMessageBox("请指定输出文件!");
		return ;
	}

	CString strPassword1;
	GetDlgItemText(IDC_EDIT_PASSWORD1, strPassword1);
	if(strPassword1.IsEmpty())
	{
		AfxMessageBox("请输入密码!");
		return ;
	}

	CString strPassword2;
	GetDlgItemText(IDC_EDIT_PASSWORD2, strPassword2);
	if(strPassword2.IsEmpty())
	{
		AfxMessageBox("请输入校验密码!");
		return ;
	}

	if(strPassword1 != strPassword2)
	{
		AfxMessageBox("两次输入的密码不一样, 请重新输入!");
		return ;
	}

	// 校验密强度, 即密码不能为单一串
//	bool bRepeat = true;
//	for(int i = 0 ; i < strPassword1.GetLength() ; i++)
//	{
//		if(i < strPassword1.GetLength() - 1)
//		{
//			if(strPassword1[i] != strPassword1[i + 1])
//			{
//				bRepeat = false;
//				break;
//			}
//		}
//	}
//
//	if(bRepeat)
//	{
//		AfxMessageBox("不能使用单一串做为密码!");
//		return ;
//	}

	int nRes = m_BlowfishEx.Init((unsigned char*)(LPCTSTR)strPassword1, strPassword1.GetLength());
	if(nRes == BlowfishEx::BLOWFISH_KEYNOTVALIDATE)
	{
		AfxMessageBox("不能使用单一串做为密码!");
		return ;
	}
	else if(nRes != BlowfishEx::BLOWFISH_SUCCESS)
	{
		AfxMessageBox(m_BlowfishEx.GetError().c_str());
		return ;
	}

	if(strPassword1.GetLength() < MINKEYBYTES || strPassword1.GetLength() > MAXKEYBYTES)
	{
		CString strTmp;
		strTmp.Format("密码长度不符, 长度必须在 %d ~ %d 之间", MINKEYBYTES, MAXKEYBYTES);
		AfxMessageBox(strTmp);
		return ;
	}

	CString strButton;
	GetDlgItemText(IDC_BTN_START, strButton);
	if(strButton == "Start(&S)")
	{
		SetDlgItemText(IDC_BTN_START, "Stop(&S)");
		void* pProc = m_Thunk.Stdcall(this, Thunk::GetMemberFxnAddr(&CBlowfishProjDlg::ThreadProc));
		HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)pProc, 0, 0, 0);
		CloseHandle(hThread);
	}
	else if(strButton == "Stop(&S)")
	{
		m_BlowfishEx.TerminateBF();
		SetDlgItemText(IDC_BTN_START, "Start(&S)");
	}
}

void CBlowfishProjDlg::OnBtnAbout() 
{
	CAboutDlg dlg;
	dlg.DoModal();
}

void CBlowfishProjDlg::OnBtnBrowsein() 
{
	vector<string> Files = m_FileDlg.GetOpenFileName(m_hWnd);
	if(Files.size() > 0)
		SetDlgItemText(IDC_EDIT_INFILE, Files[0].c_str());
}

void CBlowfishProjDlg::OnBtnBrowseout() 
{
	string strFileName = m_FileDlg.GetSaveFileName(m_hWnd);
	if(!strFileName.empty())
		SetDlgItemText(IDC_EDIT_OUTFILE, strFileName.c_str());	
}

DWORD CBlowfishProjDlg::ThreadProc( LPVOID lpParameter )
{
	CString strPassword1;
	GetDlgItemText(IDC_EDIT_PASSWORD1, strPassword1);
	m_BlowfishEx.Init((unsigned char*)(LPCTSTR)strPassword1, strPassword1.GetLength());

	CString strInFile, strOutFile;
	GetDlgItemText(IDC_EDIT_INFILE, strInFile);
	GetDlgItemText(IDC_EDIT_OUTFILE, strOutFile);

	MemberFxn addr(this, &CBlowfishProjDlg::OnFileProcess);
	if(!m_BlowfishEx.FileBF((LPSTR)(LPCTSTR)strInFile, (LPSTR)(LPCTSTR)strOutFile, ((CButton*)GetDlgItem(IDC_RADIO_ENCRYPT))->GetCheck() ? true : false, addr))
	{
		AfxMessageBox(m_BlowfishEx.GetError().c_str());
		return 0;
	}	
	
	if(((CButton*)GetDlgItem(IDC_RADIO_ENCRYPT))->GetCheck())
		AfxMessageBox("文件加密完成!");
	else
		AfxMessageBox("文件解密完成!");
	SetDlgItemText(IDC_BTN_START, "Start(&S)");

	return 0;
}

void CBlowfishProjDlg::OnFileProcess(int nProgress)
{
	m_Pos.SetPos(nProgress);
}

void CBlowfishProjDlg::OnDropFiles(HDROP hDropInfo) 
{
	// TODO: Add your message handler code here and/or call default

	int nFiles = ::DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, NULL);
	if(nFiles <= 0)
		return ;

	char buf[MAX_PATH] = { 0 };
	memset(buf, 0, MAX_PATH);
	::DragQueryFile(hDropInfo, 0, buf, sizeof(buf));

	if(GetFileAttributes(buf) == FILE_ATTRIBUTE_DIRECTORY)
		return ;

	SetDlgItemText(IDC_EDIT_INFILE, buf);
	
	CDialog::OnDropFiles(hDropInfo);
}

⌨️ 快捷键说明

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