📄 desdlg.cpp
字号:
// desDlg.cpp : implementation file
//
#include "stdafx.h"
#include "des.h"
#include "desDlg.h"
#include "des1.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()
/////////////////////////////////////////////////////////////////////////////
// CDesDlg dialog
CDesDlg::CDesDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDesDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDesDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
sourcefile = _T("");
keyfile = _T("");
resultfile = _T("");
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDesDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX,IDC_EDIT2,sourcefile);
DDX_Text(pDX,IDC_EDIT3,keyfile);
DDX_Text(pDX,IDC_EDIT4,resultfile);
//{{AFX_DATA_MAP(CDesDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDesDlg, CDialog)
//{{AFX_MSG_MAP(CDesDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, scan)
ON_BN_CLICKED(IDC_BUTTON3, scan2)
ON_BN_CLICKED(IDC_BUTTON4, scan3)
ON_BN_CLICKED(IDC_BUTTON5, cbc)
ON_BN_CLICKED(IDC_BUTTON7, decipher)
ON_BN_CLICKED(IDC_BUTTON6, cancel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDesDlg message handlers
BOOL CDesDlg::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 CDesDlg::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 CDesDlg::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 CDesDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDesDlg::scan()
{
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY,"ALL Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
sourcefile=dlg.GetPathName();
UpdateData(FALSE);
}
void CDesDlg::scan2()
{
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY,"ALL Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
keyfile=dlg.GetPathName();
UpdateData(FALSE);
}
void CDesDlg::scan3()
{
UpdateData(TRUE);
CFileDialog dlg(1,NULL,NULL,OFN_HIDEREADONLY,"ALL Files(*.*)|*.*||");
if(IDOK!=dlg.DoModal())return;
resultfile=dlg.GetPathName();
UpdateData(FALSE);
}
void CDesDlg::cbc()
{
int i = 0;
int *key = new int[64];
fpm = fopen(sourcefile, "r");
fpk = fopen(keyfile, "r");
fpc = fopen(resultfile, "w");
for(i = 0; i < 64; i ++)
key[i] = fgetc(fpk);
fclose(fpk);
makesubkey(key);
CBC(fpm, fpc);
fclose(fpc);
AfxMessageBox("Completed!");
}
void CBC(FILE *m, FILE *c)
{
int i = 0;
int signal = 1;
int ch = 0;
int *work = new int[64];
int *r = new int[64];
r = IV;
while(signal)
{
if((ch = fgetc(m)) == EOF)
return;
work[0] = ch - '0';
for(i = 1; i < 64; i ++)
if((ch = fgetc(m)) == EOF)
{
for(; i < 64; i ++)
work[i] = 0;
signal = 0;
break;
}
else
work[i] = ch - '0';
r = cipher(work, r); //des加密函数调用
for(i = 0; i < 64; i ++)
printf("%d",r[i]);
printf("\n");
for(i = 0; i < 64; i ++)
{
work[i] = r[i] + '0';
fputc(work[i], c);
}
}
}
int* cipher(int *m, int *c) //
{
int i = 0;
int t = 0;
int ch = 0;
int signal = 1;
int *r = new int[32];
int left[2][32] ;
int right[2][32] ;
int *temp = new int[64];
int *work = new int[64];
for(i = 0; i < 64; i ++) //
work[i] = (m[i] + c[i]) % 2; //
// ip 置换
for(i = 0; i < 64; i ++)
{
temp[i] = work[IP[i]];
}
// 轮函数
for(i = 0; i < 32; i ++)
{
left[0][i] = temp[i];
right[0][i] = temp[i + 32];
}
for(t = 0; t < 4; t ++)
{
r = right[t % 2];
r = F(r, subkey[t]);
for(i = 0; i < 32; i ++)
{
left[(t + 1)%2][i] = right[t%2][i];
right[(t + 1)%2][i] = (left[t%2][i] + r[i]) %2;
}
}
for(i = 0; i < 32; i ++)
{
temp[i] = left[0][i];
temp[i + 32] = right[0][i];
}
// ip逆置换
for(i = 0; i < 64; i ++)
{
work[i] = temp[IPN[i]] ; //
// fputc(work[i], c);
}
// }
return work; //
}
void makesubkey(int *key)
{
int i = 0;
int n = 0;
int temp[56];
int left[28];
int right[28];
for(i = 0; i < 28; i ++)
{
left[i] = key[IPC[i]];
right[i] = key[IPC[i + 28]];
}
for(n = 0; n < 4; n ++)
{
for(i = 0; i < 28; i ++)
{
temp[i] = left[(i + CYC[n]) % 28];
temp[i + 28] = right[(i + CYC[n]) % 28];
}
for(i = 0; i < 28; i ++)
{
left[i] = temp[i];
right[i] = temp[i + 28];
}
for(i = 0; i < 48; i ++)
{
subkey[n][i] = temp[PC[i]];
}
}
}
int * F(int *right, int *skey)
{
int i = 0;
int j = 0;
int h = 0;
int m = 0;
int n = 0;
int x = 0;
int *result = new int[32];
int *temp = new int[32];
int work[48];
int q[6];
for(i = 0; i < 48; i ++)
{
work[i] = right[E[i]];
work[i] = (work[i] + skey[i])%2;
}
for(i = 0; i < 8; i ++)
{
for(j = 0; j < 6; j ++)
q[j] = work[6 * i + j];
m = 2 * q[0] + q[5];
n = 8 * q[1] + 4 * q[2] + 2 * q[3] + q[4];
x = S[i][m][n];
for(h = 0; h < 4; h ++)
{
temp[ 3 + 4 * i -h] = x % 2;
x = x / 2;
}
}
for(i = 0; i < 32; i ++)
result[i] = temp[P[i]];
return result;
}
void CDesDlg::decipher()
{
int i = 0;
int *key = new int[64];
fpm = fopen(sourcefile, "r");
fpk = fopen(keyfile, "r");
fpc = fopen(resultfile, "w");
for(i = 0; i < 64; i ++)
key[i] = fgetc(fpk);
fclose(fpk);
makesubkey(key);
decipher1(fpm, fpc);
fclose(fpc);
AfxMessageBox("Completed!");
}
void decipher1(FILE *m, FILE *c)
{
int i = 0;
int t = 0;
int ch = 0;
int signal = 1;
int *r = new int[32];
int left[2][32] ;
int right[2][32] ;
int *temp = new int[64];
int *work = new int[64];
int *cbc = new int[64];
int *cbc1 = new int[64];
cbc = IV;
while(signal)
{
for(i = 0; i < 64; i ++)
cbc1[i] = cbc[i];//
// for(i = 0; i < 64; i ++)
// printf("%d", cbc1[i]);
// printf("\n");
if((ch = fgetc(m)) == EOF)
return;
work[0] = ch - '0';
// fgetc(m);
for(i = 1; i < 64; i ++)
if((ch = fgetc(m)) == EOF)
{
for(; i < 64; i ++)
work[i] = 0;
signal = 0;
break;
}
else
{
work[i] = ch - '0';
}
for(i = 0; i < 64; i ++)
cbc[i] = work[i];
// for(i = 0; i < 64; i ++)
// printf("%d", cbc[i]);
// printf("\n");
// ip 置换
for(i = 0; i < 64; i ++)
{
temp[i] = work[IP[i]];
}
// 轮函数
for(i = 0; i < 32; i ++)
{
left[0][i] = temp[i];
right[0][i] = temp[i + 32];
}
for(t = 0; t < 4; t ++)
{
r = left[t % 2];
r = F(r, subkey[3 - t]);
for(i = 0; i < 32; i ++)
{
right[(t + 1)%2][i] = left[t%2][i];
left[(t + 1)%2][i] = (right[t%2][i] + r[i]) %2;
}
}
for(i = 0; i < 32; i ++)
{
temp[i] = left[0][i];
temp[i + 32] = right[0][i];
}
// ip逆置换
for(i = 0; i < 64; i ++)
{
work[i] = (temp[IPN[i]] + cbc1[i]) % 2 + '0';//
fputc(work[i], c);
}
}
}
void CDesDlg::cancel()
{
exit(0);
}
void error()
{
AfxMessageBox("ERROR!");
// exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -