?? edstar.cpp
字號:
// EDStar.cpp : implementation file
//
//功能:通用文件加解密
// 作者:徐景周
//日期:2001年7月18日
//
#include "stdafx.h"
#include "scrgenius.h"
#include "EDStar.h"
#include "DirDialog.h" //目錄瀏覽對話框
#include "AnimEffect.h" //對話框動畫效果顯示
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEDStar dialog
CEDStar::CEDStar(CWnd* pParent /*=NULL*/)
: CDialog(CEDStar::IDD, pParent)
{
//{{AFX_DATA_INIT(CEDStar)
m_Password = _T("");
m_SourcePath = _T("");
m_TargetPath = _T("");
//}}AFX_DATA_INIT
}
void CEDStar::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEDStar)
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_Control(pDX, IDC_PATH2, m_TargetPath1);
DDX_Control(pDX, IDC_PATH1, m_SourcePath1);
DDX_Control(pDX, IDC_PASSWORD, m_Password1);
DDX_Text(pDX, IDC_PASSWORD, m_Password);
DDV_MaxChars(pDX, m_Password, 16);
DDX_Text(pDX, IDC_PATH1, m_SourcePath);
DDX_Text(pDX, IDC_PATH2, m_TargetPath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEDStar, CDialog)
//{{AFX_MSG_MAP(CEDStar)
ON_WM_SHOWWINDOW()
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()
/////////////////////////////////////////////////////////////////////////////
// CEDStar message handlers
BOOL CEDStar::OnInitDialog()
{
CDialog::OnInitDialog();
//把兩個文件選擇路徑的靜態框,設置內部顯示方式為,
//在顯示長路徑名時,省略中間路徑,只顯示兩邊路徑。
m_SourcePath1.SubclassDlgItem (IDC_PATH1, this);
m_SourcePath1.SetPath (TRUE);
m_TargetPath1.SubclassDlgItem (IDC_PATH2, this);
m_TargetPath1.SetPath (TRUE);
//在此將文件瀏覽兩按鈕設為平面狀,顏色和自定義窗體色相同
m_Browse1.SubclassDlgItem(IDC_BROWSE1, this);
m_Browse1.SetActiveBgColor(RGB(240,200,90));
m_Browse1.SetInactiveBgColor(RGB(240,200,90));
m_Browse1.SetTooltipText(IDS_TOOLTIP_BROWER);
m_Browse1.SetIcon(IDI_FILEOPEN);
m_Browse1.SetBtnCursor(IDC_HAND);
m_Browse2.SubclassDlgItem(IDC_BROWSE2, this);
m_Browse2.SetActiveBgColor(RGB(240,200,90));
m_Browse2.SetInactiveBgColor(RGB(240,200,90));
m_Browse2.SetTooltipText(IDS_TOOLTIP_BROWER);
m_Browse2.SetIcon(IDI_FILEOPEN);
m_Browse2.SetBtnCursor(IDC_HAND);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//加密文件算法涵數
BOOL CEDStar::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);
//判斷文件頭八個字符是否為指定字符,
//是的話,則是加密過的,不用再加密了。
if(strncmp(m_buffer,"加密之星",8)==0)
{
m_file.Close();
return false;
}
//取頭3000字節按位于密碼相與,
//并將其存于文件的未尾處。
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);
//在加密后的文件開頭處設置一特征字符串,
//并將其它一些重要特征字符于一固定字符
//串PW相與后,再將其存入文件開頭處。
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;
}
//解密文件算法涵數
BOOL CEDStar::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);
//判斷文件頭八個字符是否為加密后指定特征字
//符串,是的話,則是加密過的,可以解密了,
//否則就不能解密。
if(strncmp(m_buffer,"加密之星",8)!=0)
{
m_file.Close();
return FALSE;
}
//計算出特征字后其它一些重要字節的長度,
//并將其解密后,與指定密碼比較,判斷是
// 否相同。
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字節,
//將解密后,重寫入文件開頭處既可。
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;
}
BOOL CEDStar::DestroyWindow()
{
CRect rect2; //對話框動畫顯示效果
AnimEffect animation2;
//在此加入對話框動畫關閉效果
GetWindowRect(rect2);
rect2.DeflateRect(2,2);//動態方框顯示
animation2.DrawWireRects(rect2, CRect(rect2.CenterPoint().x-10,rect2.CenterPoint().y-10,rect2.CenterPoint().x+10,rect2.CenterPoint().y+10 ), 10);
// animation2.Close(rect2); ////對話框動畫效果顯示關閉
// Sleep(50); //延緩50毫秒后關閉
return CDialog::DestroyWindow();
}
void CEDStar::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
CRect rect2; //對話框動畫顯示效果
AnimEffect animation2;
//在此加入對話框動畫打開效果
GetWindowRect(rect2 );
animation2.Open( rect2 ); //對話框動畫效果顯示打開
}
//選擇加解密文件的對話框
void CEDStar::OnBrowse1()
{
//設置對話框參數并顯示
static char BASED_CODE filter[] = _T("所有文件(*.*)|*.*||");
CFileDialog dlg(TRUE,
NULL,
NULL,
OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT |
OFN_FILEMUSTEXIST,
filter,
0);
//得到加解密的源文件給m_SourcePath
//并顯示它到對話框的相應路徑框中
if (dlg.DoModal() == IDOK) {
m_SourcePath = dlg.GetPathName();
m_Filename=dlg.GetFileName ();
UpdateData(FALSE);
}
}
//顯示選擇加解密后保存路徑對話框
void CEDStar::OnBrowse2()
{
CDirDialog dlg;
if (dlg.DoBrowse(this) == IDOK) {
//得到目標路徑給m_TargetPath
//并顯示它到對話框的相應路徑框中
m_TargetPath = dlg.m_strPath;
UpdateData(FALSE);
}
}
//進行文件加密操作
void CEDStar::OnEncrypt()
{
UpdateData(true); //將對話框中各控件值傳給相應變量
if(m_SourcePath == "" || m_TargetPath == "" || m_Password == "")
{
MessageBox("請先選擇加密文件和加密后的保存路徑,\n然后設置加密密碼既可!","提示");
return ;
}
//將源文件復制到要保存的目標路徑后,再進行加密操作
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("文件加密成功!","提示");
//清空相應變量值,以免連按加、解密按鈕
m_SourcePath = "";
m_Password ="";
UpdateData(false); //將改變的變量值傳給相應控件
}
else
{
MessageBox("文件加密失敗!","提示");
if(tempPath != m_SourcePath)
DeleteFile(tempPath); //路徑不同時,刪除已復制文件
}
}
//進行文件解密操作
void CEDStar::OnDecrypt()
{
UpdateData(true); //將對話框中各控件值傳給相應變量
if(m_SourcePath == "" || m_TargetPath == "" || m_Password == "")
{
MessageBox("請先選擇解密文件和解密后的保存路徑,\n然后設置解密密碼既可!","提示");
return ;
}
//將源文件復制到要保存的目標路徑后,再進行解密操作
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("文件解密成功!","提示");
//清空相應變量值,以免連按加、解密按鈕
m_SourcePath ="";
m_Password ="";
UpdateData(false); //將改變的變量值傳給相應控件
}
else
{
MessageBox("文件解密失敗!","提示");
if(tempPath != m_SourcePath)
DeleteFile(tempPath); //路徑不同時,刪除已復制文件
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -