📄 simplecryptdlg.cpp
字号:
// simplecryptDlg.cpp : implementation file
//
/*************************************************Code by Lampros Giampouras*/
#include "stdafx.h"
#include "simplecrypt.h"
#include "simplecryptDlg.h"
#include "DlgProxy.h"
#include "password.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 void OnHelpAbout();
//}}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)
ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimplecryptDlg dialog
IMPLEMENT_DYNAMIC(CSimplecryptDlg, CDialog);
CSimplecryptDlg::CSimplecryptDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSimplecryptDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSimplecryptDlg)
m_filepath = _T("No file selected");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pAutoProxy = NULL;
}
CSimplecryptDlg::~CSimplecryptDlg()
{
// If there is an automation proxy for this dialog, set
// its back pointer to this dialog to NULL, so it knows
// the dialog has been deleted.
if (m_pAutoProxy != NULL)
m_pAutoProxy->m_pDialog = NULL;
}
void CSimplecryptDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSimplecryptDlg)
DDX_Control(pDX, IDC_PROGRESS1, m_progress);
DDX_Text(pDX, IDC_FILEPATH, m_filepath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSimplecryptDlg, CDialog)
//{{AFX_MSG_MAP(CSimplecryptDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_COMMAND(ID_HELP_ABOUT, OnHelpAbout)
ON_BN_CLICKED(IDC_BUTTON1, OnEncryptDecrypt)
ON_COMMAND(ID_FILE_LOAD, OnFileLoad)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_SHOWFILEINFO, OnShowfileinfo)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSimplecryptDlg message handlers
BOOL CSimplecryptDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CSimplecryptDlg::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 CSimplecryptDlg::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 CSimplecryptDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// Automation servers should not exit when a user closes the UI
// if a controller still holds on to one of its objects. These
// message handlers make sure that if the proxy is still in use,
// then the UI is hidden but the dialog remains around if it
// is dismissed.
void CSimplecryptDlg::OnClose()
{
if (CanExit())
CDialog::OnClose();
}
void CSimplecryptDlg::OnOK()
{
if (CanExit())
CDialog::OnOK();
}
void CSimplecryptDlg::OnCancel()
{
if (CanExit())
CDialog::OnCancel();
}
BOOL CSimplecryptDlg::CanExit()
{
// If the proxy object is still around, then the automation
// controller is still holding on to this application. Leave
// the dialog around, but hide its UI.
if (m_pAutoProxy != NULL)
{
ShowWindow(SW_HIDE);
return FALSE;
}
return TRUE;
}
void CAboutDlg::OnHelpAbout()
{
// TODO: Add your command handler code here
DoModal();
}
void CSimplecryptDlg::OnHelpAbout()
{
// TODO: Add your command handler code here
CAboutDlg about;
about.DoModal();
}
void CSimplecryptDlg::OnEncryptDecrypt()
{
// TODO: Add your control notification handler code here
if (length>8)
{
/******************************************************encryption code***********************/
// xoring every 8 bytes with the key
password p;
z: if (p.DoModal()==IDOK)
{
if(p.m_pass1!=p.m_pass2) //checking if the passwords are the same
{
AfxMessageBox("passwords aren't the same");
goto z;
}
if(p.m_pass1.GetLength()!=8) //64 bit encryption (8 bytes)
{
AfxMessageBox("Key must be 8 characters. [64 bit encryption]");
goto z;
}
for (int j=0;j<length;j=j+8)
{
if (j+8>length)
break;
bytes[j+0]^=p.m_pass1[0]; //xoring every 8 bytes
bytes[j+1]^=p.m_pass1[1];
bytes[j+2]^=p.m_pass1[2];
bytes[j+3]^=p.m_pass1[3];
bytes[j+4]^=p.m_pass1[4];
bytes[j+5]^=p.m_pass1[5];
bytes[j+6]^=p.m_pass1[6];
bytes[j+7]^=p.m_pass1[7];
m_progress.StepIt();
info->m_progress.StepIt();
}
/*********************************************************************************************/
}
}
else
AfxMessageBox("File must be over 8 bytes to be encrypted");
}
void CSimplecryptDlg::OnFileLoad()
{
// TODO: Add your command handler code here
if (length>1)
delete info;
static char BASED_CODE szFilter[]="Load a File (*.*)|*.*||";
CFileDialog t(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if (t.DoModal()==IDOK)
{
m_filepath=t.GetPathName(); //enter the file
m_filename=t.GetFileName();
CFile f;
f.Open(m_filepath,CFile::modeRead|CFile::typeBinary);
length=f.GetLength(); //the current logical length of the file in bytes
bytes=new unsigned char[length]; //table holding the bytes of the file
f.Read(bytes,length);
m_progress.SetRange32(0,length);
m_progress.SetStep(8);
m_progress.SetPos(0);
UpdateData (FALSE);
info=new fileinfo;
info->Create(IDD_FILEINFO);
info->ShowWindow(SW_SHOW);
info->updateinfo(m_filename,length);
info->m_progress.SetRange32(0,length);
info->m_progress.SetStep(8);
info->m_progress.SetPos(0);
}
}
void CSimplecryptDlg::OnFileSave()
{
// TODO: Add your command handler code here
CString file;
static char BASED_CODE szFilter[]="Simple Crypt File (*.crp)|*.crp | All Files (*.*)|*.*||";
CFileDialog t(FALSE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if (t.DoModal()==IDOK)
{
file=t.GetPathName(); //write the file
CFile cf(file+".crp",CFile::modeCreate|CFile::modeWrite);
cf.Write(bytes,length);
cf.Close();
m_progress.SetPos(0);
info->m_progress.SetPos(0);
delete bytes;
m_filepath="No file selected";
info->updateinfo("No file selected",0);
length=0;
UpdateData(FALSE);
delete info;
}
}
void CSimplecryptDlg::OnShowfileinfo()
{
// TODO: Add your command handler code here
if (length>1)
{
if (info->shown==true)
{
delete info;
info=new fileinfo;
info->Create(IDD_FILEINFO);
info->ShowWindow(SW_SHOW);
}
else
{
info=new fileinfo;
info->Create(IDD_FILEINFO);
info->ShowWindow(SW_SHOW);
}
info->updateinfo(m_filename,length);
}
else
AfxMessageBox("No file loaded");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -