?? aesdemodlg.cpp
字號(hào):
// AESDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AESDemo.h"
#include "AESDemoDlg.h"
#include "KAES.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
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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAESDemoDlg dialog
CAESDemoDlg::CAESDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAESDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAESDemoDlg)
// 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);
}
void CAESDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAESDemoDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAESDemoDlg, CDialog)
//{{AFX_MSG_MAP(CAESDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnAesStr)
ON_BN_CLICKED(IDC_BUTTON4, OnFileEnAes)
ON_BN_CLICKED(IDC_BUTTON2, OnSelectFile)
ON_BN_CLICKED(IDC_BUTTON3, OnFileDeAes)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAESDemoDlg message handlers
BOOL CAESDemoDlg::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
SetDlgItemText(IDC_EDIT1,"I love king_koo!");
m_filename="";
return TRUE; // return TRUE unless you set the focus to a control
}
void CAESDemoDlg::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 CAESDemoDlg::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 CAESDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAESDemoDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
void CAESDemoDlg::OnAesStr()
{
unsigned char inBuff[25],ouBuff[25];
memset(inBuff,0,25);
memset(ouBuff,0,25);
KAES kaes(24,(unsigned char*)"\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10\x11\x12\x13\x14\x15\x16\x17");
GetDlgItemText(IDC_EDIT1,(char*)inBuff,24);
if(strlen((char*)inBuff)>16)MessageBox("本例只能加密16字節(jié)的字符串,大于截?cái)?quot;);
//memcpy(inBuff,(unsigned char*)"\x0\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff",16);
kaes.Cipher(inBuff,ouBuff);//"dda97ca4......ec0d7191"
memset(inBuff,0x00,sizeof(inBuff));
kaes.InvCipher(ouBuff,inBuff);//"dda97ca4......ec0d7191"
CString str="",strTmp;
for(int i=0;i<16;i++)
{
strTmp.Format("%02x",ouBuff[i]);
str+=strTmp;
}
MessageBox(str,"加密后");
//kaes.InvCipher(ouBuff,inBuff);
//str="";
//for(i=0;i<16;i++)
//{
// strTmp.Format("%02x",inBuff[i]);
// str+=strTmp;
//}
//MessageBox(str,"解迷后");
}
void CAESDemoDlg::OnFileEnAes()
{
if(m_filename=="")return;
FILE* ff;
FILE* fp;
ff=fopen((LPCTSTR)m_filename,"rb");
if(!ff)return;
fseek(ff,0,SEEK_END);
long lFileLen=ftell(ff);
fseek(ff,0,SEEK_SET);
long c=lFileLen/16;
long d=lFileLen%16;
m_filename+=".aen";
fp=fopen((LPCTSTR)m_filename,"ab");
if(!fp){fclose(ff);return;}
unsigned char inBuff[25],ouBuff[25];
KAES kaes(24,(unsigned char*)"\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10\x11\x12\x13\x14\x15\x16\x17");
for(long i=0;i<c;i++)
{
fread(inBuff,1,16,ff);
kaes.Cipher(inBuff,ouBuff);
fwrite(ouBuff,1,16,fp);
}
if(d)
{
memset(inBuff,0,16);
c=fread(inBuff,1,d,ff);
kaes.Cipher(inBuff,ouBuff);
fwrite(ouBuff,1,16,fp);
}
fclose(ff);
fclose(fp);
MessageBox("加密成功!");
SetDlgItemText(IDC_FILESELECT,m_filename);
}
void CAESDemoDlg::OnSelectFile()
{
CString str;
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY ,"All Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
m_filename=dlg.GetPathName();
SetDlgItemText(IDC_FILESELECT,m_filename);
}
void CAESDemoDlg::OnFileDeAes()
{
if(m_filename=="")return;
FILE* ff;
FILE* fp;
ff=fopen((LPCTSTR)m_filename,"rb");
if(!ff)return;
fseek(ff,0,SEEK_END);
long lFileLen=ftell(ff);
fseek(ff,0,SEEK_SET);
long c=lFileLen/16;
long d=lFileLen%16;
m_filename+=".ade";
fp=fopen((LPCTSTR)m_filename,"w+");
if(!fp){fclose(ff);return;}
unsigned char inBuff[25],ouBuff[25];
KAES kaes(24,(unsigned char*)"\x0\x1\x2\x3\x4\x5\x6\x7\x8\x9\xa\xb\xc\xd\xe\xf\x10\x11\x12\x13\x14\x15\x16\x17");
for(long i=0;i<c;i++)
{
fread(inBuff,1,16,ff);
kaes.InvCipher(inBuff,ouBuff);
fwrite(ouBuff,1,16,fp);
}
if(d)
{
MessageBox("文件可能已損壞或非經(jīng)aes加密過");
}
fclose(ff);
fclose(fp);
MessageBox("解密成功!");
SetDlgItemText(IDC_FILESELECT,m_filename);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -