?? edstardlg.cpp
字號(hào):
// EDstarDlg.cpp : implementation file
//
//功能:通用文件加解密
// 作者:徐景周
//日期:2001年7月18日
//
#include "stdafx.h"
#include "EDstar.h"
#include "EDstarDlg.h"
#include "DirDialog.h" //目錄瀏覽對(duì)話框
#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 };
CWBButton m_OK;
//}}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)
virtual BOOL OnInitDialog();
//}}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)
DDX_Control(pDX, IDOK, m_OK);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEDstarDlg dialog
CEDstarDlg::CEDstarDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEDstarDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEDstarDlg)
m_SourcePath = _T("");
m_TargetPath = _T("");
m_Password = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CEDstarDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEDstarDlg)
DDX_Control(pDX, IDC_PASSWORD, m_Password1);
DDX_Control(pDX, IDC_PATH1, m_SourcePath1);
DDX_Control(pDX, IDC_PATH2, m_TargetPath1);
DDX_Control(pDX, IDCANCEL, m_Cancel);
DDX_Control(pDX, IDC_ENCRYPT, m_Encrypt);
DDX_Control(pDX, IDC_DECRYPT, m_Decrypt);
DDX_Control(pDX, IDC_BROWSE2, m_Browse2);
DDX_Control(pDX, IDC_BROWSE1, m_Browse1);
DDX_Text(pDX, IDC_PATH1, m_SourcePath);
DDX_Text(pDX, IDC_PATH2, m_TargetPath);
DDX_Text(pDX, IDC_PASSWORD, m_Password);
DDV_MaxChars(pDX, m_Password, 16);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEDstarDlg, CDialog)
//{{AFX_MSG_MAP(CEDstarDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BROWSE1, OnBrowse1)
ON_BN_CLICKED(IDC_BROWSE2, OnBrowse2)
ON_BN_CLICKED(IDC_ENCRYPT, OnEncrypt)
ON_BN_CLICKED(IDC_DECRYPT, OnDecrypt)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEDstarDlg message handlers
BOOL CEDstarDlg::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
//把兩個(gè)文件選擇路徑的靜態(tài)框,設(shè)置內(nèi)部顯示方式為,
//在顯示長(zhǎng)路徑名時(shí),省略中間路徑,只顯示兩邊路徑。
m_SourcePath1.SubclassDlgItem (IDC_PATH1, this);
m_SourcePath1.SetPath (TRUE);
m_TargetPath1.SubclassDlgItem (IDC_PATH2, this);
m_TargetPath1.SetPath (TRUE);
//設(shè)置位圖按鈕
m_Browse1.LoadBitmaps(IDB_BITMAP2,6, 1, 1, 1, 1 );
m_Browse1.SetFontColor(RGB(240,200,90));
m_Browse2.LoadBitmaps(IDB_BITMAP2,6, 1, 1, 1, 1 );
m_Browse2.SetFontColor(RGB(240,200,90));
m_Encrypt.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
m_Decrypt.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
m_Cancel.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
return TRUE; // return TRUE unless you set the focus to a control
}
void CEDstarDlg::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 CEDstarDlg::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 CEDstarDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//設(shè)置關(guān)于對(duì)話框中的位圖按鈕
m_OK.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//加密文件算法涵數(shù)
BOOL CEDstarDlg::EncryptFile(CString filename,char *password)
{
SetFileAttributes(filename,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_NORMAL);
CFile m_file;
if(m_file.Open(filename,CFile::modeReadWrite|CFile::typeBinary))
{
char m_buffer[3001];
m_file.Read(m_buffer,3000);
//判斷文件頭八個(gè)字符是否為指定字符,
//是的話,則是加密過(guò)的,不用再加密了。
if(strncmp(m_buffer,"加密之星",8)==0)
{
m_file.Close();
return false;
}
//取頭3000字節(jié)按位于密碼相與,
//并將其存于文件的未尾處。
int m_len=strlen(password);
for(int i=0;i<3000;i++)
m_buffer[i]^=password[i%m_len];
m_len=m_file.GetLength();
m_file.SeekToEnd();
m_file.Write(m_buffer,3000);
//在加密后的文件開(kāi)頭處設(shè)置一特征字符串,
//并將其它一些重要特征字符于一固定字符
//串PW相與后,再將其存入文件開(kāi)頭處。
memset(m_buffer,0,3001);
strcpy(m_buffer,"加密之星");
int *data=(int *)&m_buffer[8];
data[0]=m_len;
data[1]=strlen(password);
strcpy((char *)&m_buffer[2*sizeof(int)+8],password);
int temp=(2*sizeof(int)+8)+strlen(password);
char * PW="xujingzhou";
int m_PWlen=strlen(PW);
for(int j=8;j<= temp;j++)
m_buffer[j]^=PW[j%m_PWlen];
m_file.SeekToBegin();
if(m_len<3000)
m_file.Write(m_buffer,m_len);
else
m_file.Write(m_buffer,3000);
m_file.Close();
return true;
}
return false;
}
//解密文件算法涵數(shù)
BOOL CEDstarDlg::DecryptFile(CString filename,char *password)
{
SetFileAttributes(filename,FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_NORMAL);
CFile m_file;
if(m_file.Open(filename,CFile::modeReadWrite|CFile::typeBinary))
{
char m_buffer[3001];
memset(m_buffer,0,3001);
m_file.Read(m_buffer,3000);
//判斷文件頭八個(gè)字符是否為加密后指定特征字
//符串,是的話,則是加密過(guò)的,可以解密了,
//否則就不能解密。
if(strncmp(m_buffer,"加密之星",8)!=0)
{
m_file.Close();
return FALSE;
}
//計(jì)算出特征字后其它一些重要字節(jié)的長(zhǎng)度,
//并將其解密后,與指定密碼比較,判斷是
// 否相同。
int temp=(2*sizeof(int)+8)+strlen(password);
char * PW="xujingzhou";
int m_PWlen=strlen(PW);
for(int j=8;j<=temp;j++)
m_buffer[j]^=PW[j%m_PWlen];
int *data=(int *)&m_buffer[8];
int m_len=data[1];
if((m_len!=(int)strlen(password)) || (strcmp((char *)&m_buffer[8+2*sizeof(int)],password)!=0))
{
m_file.Close();
return FALSE;
}
//從加密后的文件未尾處取出3000字節(jié),
//將解密后,重寫(xiě)入文件開(kāi)頭處既可。
m_len=data[0];
long m_pos=m_file.Seek(-3000,CFile::end);
int m_number=m_file.Read(m_buffer,3000);
int m_passlen=strlen(password);
for(int i=0;i<3000;i++)
m_buffer[i]^=password[i%m_passlen];
m_file.SeekToBegin();
m_file.Write(m_buffer,3000);
m_file.SetLength(m_len);
m_file.Close();
return TRUE;
}
return FALSE;
}
//顯示選擇加解密文件對(duì)話框
void CEDstarDlg::OnBrowse1()
{
//設(shè)置對(duì)話框參數(shù)并顯示
static char BASED_CODE filter[] = _T("所有文件(*.*)|*.*||");
CFileDialog dlg(TRUE,
NULL,
NULL,
OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT |
OFN_FILEMUSTEXIST,
filter,
0);
//得到加解密的源文件給m_SourcePath
//并顯示它到對(duì)話框的相應(yīng)路徑框中
if (dlg.DoModal() == IDOK) {
m_SourcePath = dlg.GetPathName();
m_Filename=dlg.GetFileName ();
UpdateData(FALSE);
}
}
//顯示選擇加解密后保存路徑對(duì)話框
void CEDstarDlg::OnBrowse2()
{
CDirDialog dlg;
if (dlg.DoBrowse(this) == IDOK) {
//得到目標(biāo)路徑給m_TargetPath
//并顯示它到對(duì)話框的相應(yīng)路徑框中
m_TargetPath = dlg.m_strPath;
UpdateData(FALSE);
}
}
//進(jìn)行文件加密操作
void CEDstarDlg::OnEncrypt()
{
UpdateData(true); //將對(duì)話框中各控件值傳給相應(yīng)變量
if(m_SourcePath == "" || m_TargetPath == "" || m_Password == "")
{
MessageBox("請(qǐng)先選擇加密文件和加密后的保存路徑,\n然后設(shè)置加密密碼既可!","提示");
return ;
}
//將源文件復(fù)制到要保存的目標(biāo)路徑后,再進(jìn)行加密操作
CString tempPath;
if(m_TargetPath.Right (1)=='\\')
tempPath=m_TargetPath+m_Filename;
else
tempPath=m_TargetPath+'\\'+m_Filename;
CopyFile(m_SourcePath,tempPath,false);
if(EncryptFile(tempPath,m_Password.GetBuffer (0)))
{
MessageBox("文件加密成功!","提示");
//清空相應(yīng)變量值,以免連按加、解密按鈕
m_SourcePath = "";
m_Password ="";
UpdateData(false); //將改變的變量值傳給相應(yīng)控件
}
else
{
MessageBox("文件加密失敗!","提示");
if(tempPath != m_SourcePath)
DeleteFile(tempPath); //路徑不同時(shí),刪除已復(fù)制文件
}
}
//進(jìn)行文件解密操作
void CEDstarDlg::OnDecrypt()
{
UpdateData(true); //將對(duì)話框中各控件值傳給相應(yīng)變量
if(m_SourcePath == "" || m_TargetPath == "" || m_Password == "")
{
MessageBox("請(qǐng)先選擇解密文件和解密后的保存路徑,\n然后設(shè)置解密密碼既可!","提示");
return ;
}
//將源文件復(fù)制到要保存的目標(biāo)路徑后,再進(jìn)行解密操作
CString tempPath;
if(m_TargetPath.Right (1)=='\\')
tempPath=m_TargetPath+m_Filename;
else
tempPath=m_TargetPath+'\\'+m_Filename;
CopyFile(m_SourcePath,tempPath,false);
if(DecryptFile(tempPath,m_Password.GetBuffer (0)))
{
MessageBox("文件解密成功!","提示");
//清空相應(yīng)變量值,以免連按加、解密按鈕
m_SourcePath ="";
m_Password ="";
UpdateData(false); //將改變的變量值傳給相應(yīng)控件
}
else
{
MessageBox("文件解密失敗!","提示");
if(tempPath != m_SourcePath)
DeleteFile(tempPath); //路徑不同時(shí),刪除已復(fù)制文件
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -