文件加密解密的参考代码

2015-04-24 10:29:27  访问(2184) 赞(0) 踩(0)

// EncryptFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "EncryptFile.h"
#include "EncryptFileDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CEncryptFileDlg dialog

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

void CEncryptFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEncryptFileDlg)
	DDX_Text(pDX, IDC_EDIT_ENCRYPT_FILE, m_csEncryptFile);
	DDX_Text(pDX, IDC_EDIT_PWD, m_csPwd);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEncryptFileDlg, CDialog)
	//{{AFX_MSG_MAP(CEncryptFileDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_BN_CLICKED(IDC_BUTTON_ENCRYPT, OnButtonEncrypt)
	ON_BN_CLICKED(IDC_BUTTON_UNENCRYPT, OnButtonUnEncrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEncryptFileDlg message handlers

BOOL CEncryptFileDlg::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
	// 使解密按钮无效
	GetDlgItem(IDC_BUTTON_UNENCRYPT)->EnableWindow(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CEncryptFileDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
	CFileDialog FileDlg(TRUE);
	if(FileDlg.DoModal() == IDOK)
	{
		m_csEncryptFile = FileDlg.GetPathName();		
	}

	UpdateData(FALSE);
}

void CEncryptFileDlg::OnButtonEncrypt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	if (m_csEncryptFile.IsEmpty())
	{
		AfxMessageBox(_T("你没有选择要加密的文件!"));
		return;
	}

	if (m_csPwd.IsEmpty())
	{
		AfxMessageBox(_T("你没有输入密码!"));
		GetDlgItem(IDC_EDIT_PWD)->SetFocus();
		return;
	}
	
	BOOL bSuccEncrypt = EncryptFile(m_csEncryptFile);
	if (bSuccEncrypt)
	{
		AfxMessageBox(_T("加密成功!"));
	}
	else
	{
		AfxMessageBox(_T("加密失败!"));
	}

	// 清空列表框
	CleanEditBox();

	// 使解密按钮有效
	GetDlgItem(IDC_BUTTON_UNENCRYPT)->EnableWindow(TRUE);
}

void CEncryptFileDlg::OnButtonUnEncrypt() 
{
	// TODO: Add your control notification handler code here
	UpdateData();

	if (m_csEncryptFile.IsEmpty())
	{
		AfxMessageBox(_T("你没有选择要解密的文件!"));
		return;
	}

	if (m_csPwd.IsEmpty())
	{
		AfxMessageBox(_T("你没有输入密码!"));
		return;
	}

	BOOL bSuccUnEncrypt = UnEncryptFile(m_csEncryptFile);
	if (bSuccUnEncrypt)
	{
		AfxMessageBox(_T("解密成功!"));		
	}
	else
	{
		AfxMessageBox(_T("解密失败!"));
	}
	
	// 清空列表框
	CleanEditBox();
}

// 加密文件
BOOL CEncryptFileDlg::EncryptFile(CString csEncryptFileName)
{
	char *data;
	CFile *MyFile;
	DWORD fLen;

	m_nPwd = GetPwd();

	MyFile = new CFile;
	if ( !MyFile->Open(csEncryptFileName, CFile::shareDenyNone|CFile::modeReadWrite))
	{
		return FALSE;
	}

	fLen = MyFile->GetLength();
	data = new char[(int)fLen];

	MyFile->SeekToBegin();
	MyFile->Read(data, fLen);

	for (int i = 0; i < (int)fLen; i++)
	{
		data[i] ^= m_nPwd;
		data[i] ^= fLen;
	}

	MyFile->SeekToBegin();
	MyFile->Write(data, fLen);
	delete[] data;

	// 添加密码验证信息
	char MyFlag[5] = "abcd";
	for (int j = 0; j < 5; j++)
	{
		MyFlag[j] ^= m_nPwd;
	}

	MyFile->SeekToEnd();
	MyFile->Write(&MyFlag, 5);
	MyFile->Close();
	delete MyFile;

	return TRUE;
}

// 解密文件
BOOL CEncryptFileDlg::UnEncryptFile(CString csUnEncryptFileName)
{
	char *data;
	CFile *MyFile;
	DWORD fLen;
	char MyFlag[5];

	MyFile = new CFile;
	if( !MyFile->Open(csUnEncryptFileName, 
		              CFile::shareDenyNone|CFile::modeReadWrite))
	{
		return FALSE;
	}

	fLen = MyFile->GetLength();
	data = new char[(int)fLen];

	// 检验密码是否正确
	MyFile->Seek(-5, CFile::end);
	MyFile->Read(&MyFlag, 5);

	m_nPwd = GetPwd();

	for (int i = 0; i < 5; i++)
	{
		MyFlag[i] ^= m_nPwd;
	}

	if (strcmp(MyFlag, "abcd")!=0)
	{
		return FALSE;
	}

	// 解密
	MyFile->SeekToBegin();
	MyFile->Read(data, fLen);

	for (int j = 0; j < (int)fLen; j++)
	{
		data[j] ^= m_nPwd;
		data[j] ^= (fLen-5);
	}

	MyFile->SeekToBegin();
	MyFile->Write(data, fLen);
	MyFile->SetLength(fLen-5);
	MyFile->Close();

	delete[] data;
	delete MyFile;
	return TRUE;
}

// 获得密码
__int64 CEncryptFileDlg::GetPwd()
{
	DWORD pLen;
	char *pPwd;
	__int64 mc = 8757735233305;

	UpdateData();

	pPwd = m_csPwd.GetBuffer(0);

	pLen = strlen(pPwd);

	for (int i = 0; i < (int)pLen; i++)
	{
		mc ^= pPwd[i]|128;
	}

	return mc;
}

// 清空列表框
void CEncryptFileDlg::CleanEditBox()
{
	m_csEncryptFile = _T("");
	m_csPwd = _T("");

	UpdateData(FALSE);
}


标签:文件加密解密的参考代码 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)