📄 mainfrm.cpp
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//
//////////////////////////
#include "stdafx.h"
#include "AES.h"
#include "iostream.h"
#include "string.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_AES, OnAes)
ON_UPDATE_COMMAND_UI(ID_AES, OnUpdateAes)
ON_COMMAND(ID_UNAES, OnUnaes)
ON_UPDATE_COMMAND_UI(ID_UNAES, OnUpdateUnaes)
ON_COMMAND(ID_CLEAR, OnClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
m_Aes=false;
m_Unaes=false;
/////////////////////////
for(int i=0;i<10;i++)
table[i]=i+48; //0~9
for(i=10;i<36;i++)
table[i]=i+55; //A~Z
for(i=36;i<62;i++)
table[i]=i+61; // a~z
table[62]='-'; //-
table[63]='/'; ///
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
return 0;
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
//**********************************************//
void CMainFrame::OnAes()
{
if(Dlg.DoModal()==IDOK)
{
if(Dlg.m_str1!=Dlg.m_str2)
{ MessageBox(" 两次密码输入不一致,请重新输入! ","密码错误");
Dlg.m_str1=Dlg.m_str2="";
}
else if(Dlg.m_str2=="")
MessageBox(" 密码不能为空,请重新输入 ","密码错误");
else if(!(Dlg.m_str2.GetLength()>7&&Dlg.m_str2.GetLength()<17))
{
MessageBox(" 密码必须在8—16位之间,请重新输入 ","提示");
Dlg.m_str1=Dlg.m_str2="";
}
else
{
((CEditView*)this->GetActiveView())->GetWindowText(szContent);
AES(szContent);
((CEditView*)this->GetActiveView())->SetWindowText(szContent2);
m_Aes=true;
m_Unaes=false;
m_str=Dlg.m_str1;
Dlg.m_str1=Dlg.m_str2="";
}
}
else
Dlg.m_str1=Dlg.m_str2="";
}
//****************************************//
void CMainFrame::OnUpdateAes(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_Aes)
pCmdUI->Enable(false);
else
pCmdUI->Enable(true);
}
//*************************************//
void CMainFrame::OnUnaes()
{
// TODO: Add your command handler code here
UpdateData(false);
if(Dlg1.DoModal()==IDOK)
{
if(Dlg1.m_str3==m_str)
{
((CEditView*)this->GetActiveView())->GetWindowText(szContent);
UNAES(szContent);
((CEditView*)this->GetActiveView())->SetWindowText(szContent2);
Dlg1.m_str3="";
m_Aes=false;
m_Unaes=true;
}
else
MessageBox(" 密码错误,请重新输入 ","密码错误");
Dlg1.m_str3="";
}
Dlg1.m_str3="";
}
//******************************************//
void CMainFrame::AES(CString)
{
char *key,*text_p,*text_x;
int i,j;
int x,y,z;
key=new char[17];
strncpy(key,(LPCTSTR)m_str,m_str.GetLength());
for(j=0;j<m_str.GetLength();j++)
orig_key[j]=key[j];
for(j=m_str.GetLength();j<16;j++)
orig_key[j]=65+j;
rijd1.set_key(orig_key,128);
//*******************************************//
file_length=szContent.GetLength();
before_residual=file_length%16;
if(before_residual!=0)
file_length=file_length+16-before_residual;
file_word=(file_length)/16;
after_residual=(file_length)%3; // 字长除3的余数
after_file_length=(file_length*4)/3;
//********************************************//
text_p=new char[2*file_length];
text_x=new char[2*after_file_length+3];
strncpy(text_p,(LPCTSTR)szContent,file_length);
for(i=0;i<file_word;i++)
{
for(j=0;j<16;j++)
ming_wen[j]=*(text_p+i*16+j);
rijd1.encrypt(ming_wen,mi_wen);
for(j=0;j<16;j++)
*(text_p+i*16+j)=mi_wen[j];
}
if(after_residual==2)
{
*(text_p+i*16+j)=65;
file_length++;
}
else if(after_residual==1)
{
*(text_p+i*16+j)=66;
*(text_p+i*16+j+1)=67;
file_length=file_length+2;
}
after_file_length=(file_length*4)/3;
for(i=0,j=0;i<after_file_length,j<file_length;) //3*after_file_word;)
{
x=z=*(text_p+j);
j++;
x=x&252;// 11111100;
x=x>>2;
x=x&63;//00111111;
*(text_x+i)=table[x]; //first chracter
i++;
y=x=*(text_p+j);
j++;
y=y>>4;
y=y&15;//00001111;
z=z<<4;
z=z&63;
y=y|z;
*(text_x+i)=table[y]; // second character
i++;
z=y=*(text_p+j);
j++;
z=z>>6;
z=z&3;//00000011;
x=x<<2;
x=x&63;//00111111;
z=x|z;
*(text_x+i)=table[z]; //third character
i++;
y=y&63;//001111111;
*(text_x+i)=table[y]; // fourth character
i++;
}
szContent=text_x;
szContent2=szContent.Left(after_file_length);
delete text_x;
delete text_p;
}
//******************************************//
void CMainFrame::UNAES(CString)
{
// TODO: Add your command handler code here
char *text_p,*text_x;
int i,j;
int x,y,z,z1;
text_p=new char[2*file_length];
text_x=new char[2*after_file_length+3];
strncpy(text_x,(LPCTSTR)szContent,after_file_length);
for(i=0,j=0;i<after_file_length,j<file_length;)
{
x=*(text_x+i); //read 1th byte
i++;
y=find(x);
y=y<<2;
x=*(text_x+i); //read 2th byte
i++;
z=z1=find(x);
z=z>>4;
y=y|z;
*(text_p+j)=y; //save first byte
j++;
x=*(text_x+i); //read 3th byte
i++;
y=z=find(x);
z1=z1<<4;
y=y>>2;
y=y|z1;
*(text_p+j)=y; //save second byte
j++;
x=*(text_x+i);
i++;
y=find(x);
z=z<<6;
y=y|z;
*(text_p+j)=y; //save third byte
j++;
}
for(i=0;i<file_word;i++)
{
for(j=0;j<16;j++)
mi_wen[j]=*(text_p+i*16+j);
rijd1.decrypt(mi_wen,ming_wen);
for(j=0;j<16;j++)
*(text_p+i*16+j)=ming_wen[j];
}
szContent=text_p;
szContent2=szContent.Left(file_length);
delete text_p;
delete text_x;
}
//******************************************//
void CMainFrame::OnUpdateUnaes(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_Unaes)
pCmdUI->Enable(false);
else
pCmdUI->Enable(true);
}
void CMainFrame::OnClear()
{
// TODO: Add your command handler code here
((CEditView*)this->GetActiveView())->GetWindowText(szContent);
szContent.Empty();
((CEditView*)this->GetActiveView())->SetWindowText(szContent);
}
// search table()
int CMainFrame::find(int p)
{
int k=-1;
do{
k++;
} while(table[k]!=p);
return k;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -