?? 非標準16位des算法dlg.cpp
字號:
// 非標準16位DES算法Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "非標準16位DES算法.h"
#include "非標準16位DES算法Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//類似DoEvents 轉讓控制權,以便讓操作系統處理其它的事件。
BOOL CheckMessageQueue()
{
MSG msg;
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message==WM_QUIT) return FALSE;
TranslateMessage(&msg); DispatchMessage(&msg);
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMy16DESDlg dialog
CMy16DESDlg::CMy16DESDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy16DESDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy16DESDlg)
FDlg = new CFileDialog(true, "*", NULL, OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT, "任意文件 (*.*)|*.*|");
FDlg2 = new CFileDialog(false, "", NULL, OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT, "任意文件 (*.*)|*.*|");
m_file1 = _T("");
m_file2 = _T("");
m_static1 = _T("請選擇明文文件:");
m_r1 = 0;
m_r2 = -1;
m_key = _T("ok");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMy16DESDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy16DESDlg)
DDX_Control(pDX, IDC_P1, m_p1);
DDX_Text(pDX, IDC_FILE1, m_file1);
DDX_Text(pDX, IDC_FILE2, m_file2);
DDX_Text(pDX, IDC_STATIC1, m_static1);
DDX_Radio(pDX, IDC_R1, m_r1);
DDX_Radio(pDX, IDC_R2, m_r2);
DDX_Text(pDX, IDC_KEY, m_key);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy16DESDlg, CDialog)
//{{AFX_MSG_MAP(CMy16DESDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_R1, OnR1)
ON_BN_CLICKED(IDC_R2, OnR2)
ON_BN_CLICKED(IDC_View1, OnView1)
ON_BN_CLICKED(IDC_View2, OnView2)
ON_BN_CLICKED(IDC_START, OnStart)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy16DESDlg message handlers
BOOL CMy16DESDlg::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 CMy16DESDlg::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 CMy16DESDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
DWORD CMy16DESDlg::GetFSize(LPTSTR lpszFilePath)
{
HANDLE hFile=((HANDLE) CreateFile(lpszFilePath,GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,0,NULL));
DWORD s=GetFileSize(hFile ,NULL);
CloseHandle(hFile);
return s;
}
BOOL CMy16DESDlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
// 處理OnCommand事件防止默認Enter、Esc退出
switch(wParam)
{
case IDOK://攔截回車鍵
case IDCANCEL://攔截ESC鍵
return true;
default://其它消息攔截
return CDialog::OnCommand(wParam, lParam);
}
return CDialog::OnCommand(wParam, lParam);
}
void CMy16DESDlg::OnClose()
{
// 重載OnClose函數,使關閉按鈕有效
CMy16DESDlg::EndDialog(0);
}
void CMy16DESDlg::OnR1()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_r2=-1;
m_static1="請選擇明文文件:";
UpdateData(false);
}
void CMy16DESDlg::OnR2()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_r1=-1;
m_static1="請選擇密文文件:";
UpdateData(false);
}
void CMy16DESDlg::OnView1()
{
// TODO: Add your control notification handler code here
if ( FDlg->DoModal()!=IDOK ) return;
m_file1=FDlg->GetPathName();
UpdateData(false);
}
void CMy16DESDlg::OnView2()
{
// TODO: Add your control notification handler code here
if ( FDlg2->DoModal()!=IDOK ) return;
m_file2=FDlg2->GetPathName();
UpdateData(false);
}
void CMy16DESDlg::OnStart()
{
// TODO: Add your control notification handler code here
CFile cf1,cf2;
byte tmp[2],tmp2[2];
long FileSize,Progress;
UpdateData(true);
if(m_key.GetLength()!=2) {MessageBox("密鑰長度必須為2字節","Error");return;}
unsigned char *key=new unsigned char[2];
memcpy(key,(LPTSTR)(LPCTSTR)m_key,2);
FileSize=GetFSize((LPTSTR)(LPCTSTR)m_file1);
Progress=FileSize;
m_p1.SetRange(0,100);
if(cf1.Open(_T(m_file1),CFile::modeRead|CFile::typeBinary))
{
if(cf2.Open(_T(m_file2),CFile::modeCreate|CFile::modeWrite))
{
GetDlgItem(IDC_START)->EnableWindow(false);
GetDlgItem(IDC_R1)->EnableWindow(false);
GetDlgItem(IDC_R2)->EnableWindow(false);
while(FileSize>0)
{
ZeroMemory(tmp,2);
cf1.Read(tmp,2);
//調用DES16
DES(tmp,tmp2,key,m_r1==0?1:0);
///////////
cf2.Write(tmp2,2);
FileSize-=2;
m_p1.SetPos((Progress-FileSize)*100/Progress);
CheckMessageQueue(); //轉讓控制權,防止無響應
}
GetDlgItem(IDC_START)->EnableWindow(true);
GetDlgItem(IDC_R1)->EnableWindow(true);
GetDlgItem(IDC_R2)->EnableWindow(true);
MessageBox("操作成功","Success");
cf2.Close();
}
else
{
MessageBox("不能寫入文件","Error");
return;
}
cf1.Close();
}
else
{
MessageBox("不能打開文件","Error");
return;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -