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

📄 encryptdlg.cpp

📁 对所需要保密的文件进行加密
💻 CPP
字号:
// EncryptDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Encrypt.h"
#include "EncryptDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg dialog

CEncryptDlg::CEncryptDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEncryptDlg::IDD, pParent),keyLength(6)
{
	//{{AFX_DATA_INIT(CEncryptDlg)
		// 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);
	key="";
}

void CEncryptDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEncryptDlg)
	DDX_Control(pDX, IDC_PROGRESS, m_progress);
	DDX_Control(pDX, IDC_KEY, m_key);
	//}}AFX_DATA_MAP
	m_key.LimitText(6);
	m_key.SetPasswordChar('*');
	m_progress.ShowWindow(false);
}

BEGIN_MESSAGE_MAP(CEncryptDlg, CDialog)
	//{{AFX_MSG_MAP(CEncryptDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_DISENCRYPT, OnDisencrypt)
	ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEncryptDlg message handlers

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

void CEncryptDlg::OnDisencrypt()	//解密
{
	GetDlgItemText(IDC_KEY,key);
	if(key.GetLength()!=keyLength)
	{
		AfxMessageBox("密码为6位!");
		return ;
	}

	CFileDialog fdlg(true);			//OPEN对话框
	if(fdlg.DoModal()==IDOK)
	{
		CFile oldFile,newFile;
		char buffer[1000];
		int num,oddment,pos;
		unsigned long length;

		filePathName=fdlg.GetPathName();
		if(filePathName.Right(4)!=".ljg")
		{
			AfxMessageBox("格式不正确!");
			return;
		}
		pos=filePathName.GetLength()-4;
		CString newName=filePathName.Left(pos);
		newFile.Open(newName,CFile::modeCreate|CFile::modeWrite);
		oldFile.Open(filePathName,CFile::modeRead);
		length=oldFile.GetLength();
		num=length/1000;
		oddment=length%1000;
		m_progress.ShowWindow(true);
		m_progress.SetRange32(0,num+1);
		m_progress.SetStep(1);
		for(int i=0;i<num;i++)
		{
			oldFile.Seek(1000*i,CFile::begin);//从文件头开始往下移动
			oldFile.Read(buffer,1000);
			for(int j=0;j<1000;j++)
				buffer[j]+=key.GetAt(j%keyLength);

			newFile.SeekToEnd();		//移到文件尾
			newFile.Write(buffer,1000);
			m_progress.StepIt();
		}

		if(oddment!=0)
		{
			oldFile.Seek(1000*num,CFile::begin);//从文件头开始往下移动
			oldFile.Read(buffer,oddment);
			for(int k=0;k<1000;k++)
				buffer[k]+=key.GetAt(k%keyLength);

			newFile.SeekToEnd();		//移到文件尾
			newFile.Write(buffer,oddment);
		}
		newFile.Close();
		oldFile.Close();
		m_progress.StepIt();
	}
	m_key.Clear();
	m_progress.ShowWindow(false);
}

void CEncryptDlg::OnEncrypt()	//加密
{
	GetDlgItemText(IDC_KEY,key);
	if(key.GetLength()!=keyLength)
	{
		AfxMessageBox("密码为6位!");
		return ;
	}

	CFileDialog fdlg(true);		//OPEN对话框
	if(fdlg.DoModal()==IDOK)
	{
		CFile oldFile,newFile;
		char buffer[1000];
		int num,oddment;
		unsigned long length;
	
		filePathName=fdlg.GetPathName();
		newFile.Open(filePathName+".ljg",CFile::modeCreate|CFile::modeWrite);
		oldFile.Open(filePathName,CFile::modeRead);
		length=oldFile.GetLength();
		num=length/1000;
		oddment=length%1000;
		m_progress.ShowWindow(true);
		m_progress.SetRange32(0,num+1);
		m_progress.SetStep(1);
		for(int i=0;i<num;i++)
		{
			oldFile.Seek(1000*i,CFile::begin);//从文件头开始往下移动
			oldFile.Read(buffer,1000);
			for(int j=0;j<1000;j++)
				buffer[j]-=key.GetAt(j%keyLength);

			newFile.SeekToEnd();		//移到文件尾
			newFile.Write(buffer,1000);
			m_progress.StepIt();
		}

		if(oddment!=0)
		{
			oldFile.Seek(1000*num,CFile::begin);//从文件头开始往下移动
			oldFile.Read(buffer,oddment);
			for(int k=0;k<1000;k++)
				buffer[k]-=key.GetAt(k%keyLength);

			newFile.SeekToEnd();		//移到文件尾
			newFile.Write(buffer,oddment);
		}
		newFile.Close();
		oldFile.Close();
		m_progress.StepIt();
		CFile::Remove(filePathName);
	}
	m_key.Clear();
	m_progress.ShowWindow(false);
}

⌨️ 快捷键说明

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