?? lockandunlockview.cpp
字號(hào):
// LockAndUnLockView.cpp : implementation of the CLockAndUnLockView class
//
#include "stdafx.h"
#include "LockAndUnLock.h"
#include "rsa.h"
#include "input.h"
#include "LockAndUnLockDoc.h"
#include "LockAndUnLockView.h"
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <fstream.h>
#include "WelCome.h"//包含頭文件.
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLockAndUnLockView
IMPLEMENT_DYNCREATE(CLockAndUnLockView, CFormView)
BEGIN_MESSAGE_MAP(CLockAndUnLockView, CFormView)
//{{AFX_MSG_MAP(CLockAndUnLockView)
ON_BN_CLICKED(ID_LOCKFILE_MENU, OnLockfileMenu)
ON_BN_CLICKED(ID_LOCKSAVE_MENU, OnLocksaveMenu)
ON_BN_CLICKED(ID_UNLOCKFILE_MENU, OnUnlockfileMenu)
ON_BN_CLICKED(ID_UNLOCKSAVE_MENU, OnUnlocksaveMenu)
ON_BN_CLICKED(ID_LOCKRAS_MENU, OnLockrasMenu)
ON_BN_CLICKED(ID_UNLOCKRAS_MENU, OnUnlockrasMenu)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLockAndUnLockView construction/destruction
CLockAndUnLockView::CLockAndUnLockView()
: CFormView(CLockAndUnLockView::IDD)
{
//{{AFX_DATA_INIT(CLockAndUnLockView)
m_InFileContent = _T("");
m_OutFileContent = _T("");
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CLockAndUnLockView::~CLockAndUnLockView()
{
}
void CLockAndUnLockView::DoDataExchange(CDataExchange* pDX)
{
CFormView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLockAndUnLockView)
DDX_Text(pDX, IDC_INFILE_EDIT, m_InFileContent);
DDX_Text(pDX, IDC_OUTFILE_EDIT, m_OutFileContent);
//}}AFX_DATA_MAP
}
BOOL CLockAndUnLockView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CFormView::PreCreateWindow(cs);
}
void CLockAndUnLockView::OnInitialUpdate()
{
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
CWelCome m_Welcome;
m_Welcome.DoModal();
while (m_Welcome.m_HavePass!=true )
{
MessageBox("密碼錯(cuò)誤,請(qǐng)重新登錄","錯(cuò)誤信息");
m_Welcome.m_PassWord="";
m_Welcome.DoModal();
}
m_InFilePool=NULL;//輸入文件的緩沖區(qū)
m_OutFilePool=NULL;//輸出文件的緩沖區(qū)
m_intInFilePool=NULL;//輸入文件的緩沖區(qū)
m_intOutFilePool=NULL;//輸出文件的緩沖區(qū)
m_InFileLength=0;
m_OutFileLength=0;
}
/////////////////////////////////////////////////////////////////////////////
// CLockAndUnLockView printing
BOOL CLockAndUnLockView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CLockAndUnLockView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CLockAndUnLockView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CLockAndUnLockView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
// TODO: add customized printing code here
}
/////////////////////////////////////////////////////////////////////////////
// CLockAndUnLockView diagnostics
#ifdef _DEBUG
void CLockAndUnLockView::AssertValid() const
{
CFormView::AssertValid();
}
void CLockAndUnLockView::Dump(CDumpContext& dc) const
{
CFormView::Dump(dc);
}
CLockAndUnLockDoc* CLockAndUnLockView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLockAndUnLockDoc)));
return (CLockAndUnLockDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLockAndUnLockView message handlers
void CLockAndUnLockView::OnLockfileMenu()
{
// TODO: Add your control notification handler code here
//這個(gè)部份用來(lái)將文件讀出數(shù)據(jù),并且將數(shù)據(jù)存放于m_InFilePool中(它是一個(gè)BYTE* 指針)
//它的內(nèi)容長(zhǎng)度放在m_InFileLength中.
CString m_filename; //文件名字
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);//打開無(wú)類型文件
if (m_InFilePool!=NULL)
{
delete m_InFilePool;
m_InFilePool=NULL;//如果不為空的指針,就將之前的輸入文件緩沖清空.
}
if(dlg.DoModal()==IDOK)
{
m_filename=dlg.GetPathName(); //得名字
if(!m_InFile.Open(m_filename,CFile::modeRead))
{
MessageBox("讀入錯(cuò)誤");
return;
}
else
{
m_InFileLength=m_InFile.GetLength();
m_InFilePool=new byte[m_InFileLength+1];//分配內(nèi)存給它.
m_InFile.Read(m_InFilePool,m_InFileLength);//原文件的數(shù)據(jù)放在此處.
m_InFilePool[m_InFileLength]='\0';
m_InFileContent=m_InFilePool; //內(nèi)容加入到一個(gè)TXT框中顯示出來(lái).中能顯示TXT類型的文件.
UpdateData(false);
m_InFile.Close();//文件關(guān)掉.
}
}
}
void CLockAndUnLockView::OnLocksaveMenu()
{
// TODO: Add your control notification handler code here
//在執(zhí)行這個(gè)操作之前,要將結(jié)果的內(nèi)容放入m_OutFilePool中,
//并且要設(shè)置好m_OutFileLength;
int tmp=0;
UpdateData(true);//將數(shù)據(jù)讀入
CString m_FileName;//保存的文件名
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
if(dlg.DoModal()==IDOK)
{
m_FileName=dlg.GetPathName();
if(!m_OutFile.Open(m_FileName,CFile::modeCreate))
{
MessageBox("寫入錯(cuò)誤");
return;
}
else
{
m_OutFile.Close();
ofstream writefile(m_FileName,ios::binary); //打開寫文件
if( !writefile )
{
return ;
}
for(int n=0; n<m_OutFileLength; n++)
{
tmp=m_intOutFilePool[n];
writefile.write((char *)&tmp,sizeof(int));
}
writefile.close();
// m_OutFile.Write(m_OutFilePool,m_OutFileLength);
UpdateData(false);
if (m_intOutFilePool!=NULL) delete m_intOutFilePool;
if (m_intInFilePool!=NULL) delete m_intInFilePool;
if (m_OutFilePool!=NULL) delete m_OutFilePool;
if (m_InFilePool!=NULL) delete m_InFilePool;
m_OutFilePool=NULL;//delete 完之后一定要清成NULL,不然就不知道有沒有釋放內(nèi)存的啦.
m_InFilePool=NULL;
m_intOutFilePool=NULL;//delete 完之后一定要清成NULL,不然就不知道有沒有釋放內(nèi)存的啦.
m_intInFilePool=NULL;
}
}
}
void CLockAndUnLockView::OnUnlockfileMenu()
{
// TODO: Add your control notification handler code here
// OnLockfileMenu();
int tmp;
CString m_filename; //文件名字
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);//打開無(wú)類型文件
if (m_InFilePool!=NULL)
{
delete m_InFilePool;
m_InFilePool=NULL;//如果不為空的指針,就將之前的輸入文件緩沖清空.
}
if (m_intInFilePool!=NULL)
{
delete m_intInFilePool;
m_intInFilePool=NULL;//如果不為空的指針,就將之前的輸入文件緩沖清空.
}
if(dlg.DoModal()==IDOK)
{
m_filename=dlg.GetPathName(); //得名字
if(!m_InFile.Open(m_filename,CFile::modeRead))
{
MessageBox("讀入錯(cuò)誤");
return;
}
else
{
m_InFile.Close();
ifstream length(m_filename,ios::binary); //打開讀文件
if( !length )
{
return ;
}
for(m_InFileLength=-1;length;m_InFileLength++) //計(jì)算文件長(zhǎng)度
{
length.read((char*)&tmp,sizeof(int));
}
length.close();
m_InFilePool=new byte[m_InFileLength+1];//分配內(nèi)存給它.
m_intInFilePool=new int[m_InFileLength+1];//分配內(nèi)存給它.
// readfile.fseek(0,ios::beg); //移到文件首
ifstream readfile(m_filename, ios::binary); //打開讀文件
if( !readfile )
{
return ;
}
for(int n=0; n<m_InFileLength; n++)
{
readfile.read((char*)&tmp,sizeof(int));
m_intInFilePool[n]=tmp;
m_InFilePool[n]=(char)tmp;
}
readfile.close();
m_InFilePool[m_InFileLength]='\0';
m_InFileContent=m_InFilePool; //內(nèi)容加入到一個(gè)TXT框中顯示出來(lái).中能顯示TXT類型的文件.
UpdateData(false);
}
}
}
void CLockAndUnLockView::OnUnlocksaveMenu()
{
// TODO: Add your control notification handler code here
//在執(zhí)行這個(gè)操作之前,要將結(jié)果的內(nèi)容放入m_OutFilePool中,
//并且要設(shè)置好m_OutFileLength;
UpdateData(true);//將數(shù)據(jù)讀入
CString m_FileName;//保存的文件名
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
if(dlg.DoModal()==IDOK)
{
m_FileName=dlg.GetPathName();
if(!m_OutFile.Open(m_FileName,CFile::modeCreate))
{MessageBox("寫入錯(cuò)誤");return;}
else
{
m_OutFile.Close();
m_OutFile.Open(m_FileName,CFile::modeReadWrite);
// m_OutFilePool=m_InFilePool; //測(cè)試時(shí)用的.
// m_OutFileLength=m_InFileLength;//測(cè)試時(shí)用的.
m_OutFile.Write(m_OutFilePool,m_OutFileLength);
UpdateData(false);
m_OutFile.Close();
if (m_OutFilePool!=NULL) delete m_OutFilePool;
if (m_InFilePool!=NULL) delete m_InFilePool;
m_OutFilePool=NULL;//delete 完之后一定要清成NULL,不然就不知道有沒有釋放內(nèi)存的啦.
m_InFilePool=NULL;
}
}
}
BOOL CLockAndUnLockView::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if (m_InFilePool!=NULL) delete m_InFilePool;//假如沒有釋放內(nèi)存的在此釋放.
if (m_OutFilePool!=NULL) delete m_OutFilePool;
if (m_intInFilePool!=NULL) delete m_intInFilePool;//假如沒有釋放內(nèi)存的在此釋放.
if (m_intOutFilePool!=NULL) delete m_intOutFilePool;
return CFormView::DestroyWindow();
}
void CLockAndUnLockView::OnLockrasMenu()
{
// TODO: Add your control notification handler code here
input input_ME;
rsa rsa;
if (m_intOutFilePool!=NULL)
{
delete m_intOutFilePool;
m_intOutFilePool=NULL;//如果不為空的指針,就將之前的輸入文件緩沖清空.
}
m_intOutFilePool=new int[m_InFileLength+1]; //分配內(nèi)存給它.
if (m_OutFilePool!=NULL)
{
delete m_OutFilePool;
m_OutFilePool=NULL;//如果不為空的指針,就將之前的輸入文件緩沖清空.
}
m_OutFilePool=new byte[m_InFileLength+1]; //分配內(nèi)存給它.
m_OutFileLength=m_InFileLength;
if(input_ME.DoModal()==IDOK) //輸入
{
if( !rsa.set_pqde(input_ME.m_intM,input_ME.m_intE) )
{
AfxMessageBox("輸入的 M 不正確");
return;
}
for(int n=0;n<m_InFileLength;n++)
{
m_intOutFilePool[n]=rsa.encry((int)m_InFilePool[n]);
}
for(n=0;n<m_OutFileLength;n++)
{
m_OutFilePool[n]=(byte)m_intOutFilePool[n];
}
m_OutFilePool[m_InFileLength]='\0';
m_OutFileContent=m_OutFilePool; //內(nèi)容加入到一個(gè)TXT框中顯示出來(lái).中能顯示TXT類型的文件.
UpdateData(false);
}
else
{
return;
}
}
void CLockAndUnLockView::OnUnlockrasMenu()
{
// TODO: Add your control notification handler code here
input input_ME;
rsa rsa;
if (m_OutFilePool!=NULL)
{
delete m_OutFilePool;
m_OutFilePool=NULL;//如果不為空的指針,就將之前的輸入文件緩沖清空.
}
m_OutFilePool=new byte[m_InFileLength+1]; //分配內(nèi)存給它.
m_OutFileLength=m_InFileLength;
if(input_ME.DoModal()==IDOK) //輸入
{
if( !rsa.set_pqde(input_ME.m_intM,input_ME.m_intE) )
{
AfxMessageBox("輸入的 M 不正確");
return;
}
for(int n=0;n<m_InFileLength;n++)
{
(byte)m_OutFilePool[n]=rsa.decode((int)m_intInFilePool[n]);
}
m_OutFilePool[m_InFileLength]='\0';
m_OutFileContent=m_OutFilePool; //內(nèi)容加入到一個(gè)TXT框中顯示出來(lái).中能顯示TXT類型的文件.
UpdateData(false);
}
else
{
return;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -