📄 blowfish encrypt tooldlg.cpp
字号:
// BlowFish Encrypt ToolDlg.cpp : implementation file
//
#include "stdafx.h"
#include "BlowFish Encrypt Tool.h"
#include "BlowFish Encrypt ToolDlg.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
typedef unsigned long ulong;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CBlowFishEncryptToolDlg dialog
CBlowFishEncryptToolDlg::CBlowFishEncryptToolDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBlowFishEncryptToolDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBlowFishEncryptToolDlg)
m_srcfile = _T("");
m_tarfile = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBlowFishEncryptToolDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBlowFishEncryptToolDlg)
DDX_Text(pDX, IDC_EDIT1, m_srcfile);
DDX_Text(pDX, IDC_EDIT2, m_tarfile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBlowFishEncryptToolDlg, CDialog)
//{{AFX_MSG_MAP(CBlowFishEncryptToolDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON1, OnOpen)
ON_BN_CLICKED(IDC_BUTTON3, OnSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBlowFishEncryptToolDlg message handlers
BOOL CBlowFishEncryptToolDlg::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 CBlowFishEncryptToolDlg::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 CBlowFishEncryptToolDlg::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 CBlowFishEncryptToolDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBlowFishEncryptToolDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData(TRUE);
CFile file1;
CFile file2;
file1.Open(m_srcfile,CFile::modeRead);
file2.Open(m_tarfile,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
unsigned long key[]={0x8c,0xdb,0x39,0x73};
unsigned long buffer[2];
memset(buffer,0,sizeof(buffer));
int filesize=file1.GetLength();
for(int i=8;i<filesize;i+=8)
{
file1.Read(buffer,8);
tea_encrypt(buffer,key);
file2.Write(buffer,8);
}
memset(buffer,0,sizeof(buffer));
int f=i-filesize;
if(f>0)
{
int end=8%f;
file1.Read(buffer,end);
tea_decrypt(buffer,key);
file2.Write(buffer,end);
}
file1.Close();
file2.Close();
MessageBox("Congratulations,Encrypt Finish!","Information",MB_OK);
m_srcfile="";
m_tarfile="";
UpdateData(FALSE);
}
void CBlowFishEncryptToolDlg::OnButton2()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFile file2;
CFile file1;
file1.Open(m_srcfile,CFile::modeRead);
file2.Open(m_tarfile,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
unsigned long key[]={0x8c,0xdb,0x39,0x73};
unsigned long buffer[2];
memset(buffer,0,sizeof(buffer));
int filesize=file1.GetLength();
for(int i=8;i<filesize;i+=8)
{
file1.Read(buffer,8);
tea_decrypt(buffer,key);
file2.Write(buffer,8);
}
memset(buffer,0,sizeof(buffer));
int f=i-filesize;
if(f>0)
{
int end=8%f;
file1.Read(buffer,end);
tea_decrypt(buffer,key);
file2.Write(buffer,end);
}
file1.Close();
file2.Close();
MessageBox("Congratulations,Decrypt Finish!","Information",MB_OK);
m_srcfile="";
m_tarfile="";
UpdateData(FALSE);
}
void CBlowFishEncryptToolDlg::tea_encrypt(unsigned long* v, unsigned long* k)
{
unsigned long v0=v[0], v1=v[1], sum=0, i;
unsigned long delta=0x9e3779b9;
unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3];
for (i=0; i < 32; i++)
{
sum += delta;
v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
}
v[0]=v0; v[1]=v1;
}
void CBlowFishEncryptToolDlg::tea_decrypt(unsigned long* v, unsigned long* k)
{
unsigned long v0=v[0], v1=v[1], sum=0xC6EF3720, i;
unsigned long delta=0x9e3779b9;
unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3];
for(i=0; i<32; i++)
{
v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
sum -= delta;
}
v[0]=v0; v[1]=v1;
}
void CBlowFishEncryptToolDlg::OnOpen()
{
// TODO: Add your control notification handler code here
CFileDialog OpenFile(TRUE);
OpenFile.m_ofn.lpstrTitle="Open File";
OpenFile.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0Dat Files(*.dat)\0*.dat\0All Files(*.*)\0*.*\0\0";
if(IDOK==OpenFile.DoModal())
{
m_srcfile=OpenFile.GetPathName();
UpdateData(FALSE);
}
}
void CBlowFishEncryptToolDlg::OnSave()
{
// TODO: Add your control notification handler code here
CFileDialog SaveFile(FALSE);
SaveFile.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0Dat File(*.dat)\0*.dat\0All Files(*.*)\0*.*\0\0";
SaveFile.m_ofn.lpstrDefExt="dat";
if(IDOK==SaveFile.DoModal())
{
m_tarfile=SaveFile.GetPathName();
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -