⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lockandunlockview.cpp

📁 RSA加密与解密算法的 C++实现
💻 CPP
字号:
// 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("密码错误,请重新登录","错误信息");
		m_Welcome.m_PassWord="";
		m_Welcome.DoModal();
	}
		
	m_InFilePool=NULL;//输入文件的缓冲区
    m_OutFilePool=NULL;//输出文件的缓冲区
	m_intInFilePool=NULL;//输入文件的缓冲区
    m_intOutFilePool=NULL;//输出文件的缓冲区
	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
	//这个部份用来将文件读出数据,并且将数据存放于m_InFilePool中(它是一个BYTE* 指针)
	//它的内容长度放在m_InFileLength中.


	CString m_filename;  //文件名字

	CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);//打开无类型文件

    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("读入错误");
		return;
	 } 
	 else
	 {
	  m_InFileLength=m_InFile.GetLength();
      m_InFilePool=new byte[m_InFileLength+1];//分配内存给它.

      m_InFile.Read(m_InFilePool,m_InFileLength);//原文件的数据放在此处.

	  m_InFilePool[m_InFileLength]='\0';
	  m_InFileContent=m_InFilePool;  //内容加入到一个TXT框中显示出来.中能显示TXT类型的文件.
      
	  UpdateData(false);


	  m_InFile.Close();//文件关掉.
	 } 
	}
	
	
}

void CLockAndUnLockView::OnLocksaveMenu() 
{
	// TODO: Add your control notification handler code here

	//在执行这个操作之前,要将结果的内容放入m_OutFilePool中,
	//并且要设置好m_OutFileLength;
	int tmp=0;
    UpdateData(true);//将数据读入
    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("写入错误");
		  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,不然就不知道有没有释放内存的啦.
		m_InFilePool=NULL;
		m_intOutFilePool=NULL;//delete 完之后一定要清成NULL,不然就不知道有没有释放内存的啦.
		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);//打开无类型文件
    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("读入错误");
			return;
		} 
		else
		{
			m_InFile.Close();

			ifstream length(m_filename,ios::binary);		//打开读文件
			if( !length )
			{
				return ;
			}
			for(m_InFileLength=-1;length;m_InFileLength++) //计算文件长度
			{
				length.read((char*)&tmp,sizeof(int));
			}							
			length.close();
			m_InFilePool=new byte[m_InFileLength+1];//分配内存给它.
			m_intInFilePool=new int[m_InFileLength+1];//分配内存给它.


//			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;  //内容加入到一个TXT框中显示出来.中能显示TXT类型的文件.
			UpdateData(false);
		} 
	}
	
}

void CLockAndUnLockView::OnUnlocksaveMenu() 
{
	// TODO: Add your control notification handler code here
	
	//在执行这个操作之前,要将结果的内容放入m_OutFilePool中,
	//并且要设置好m_OutFileLength;

    UpdateData(true);//将数据读入
    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("写入错误");return;}
	 else
	 {   
		m_OutFile.Close();
	    m_OutFile.Open(m_FileName,CFile::modeReadWrite);
		
 //       m_OutFilePool=m_InFilePool;  //测试时用的.
//		m_OutFileLength=m_InFileLength;//测试时用的.

        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,不然就不知道有没有释放内存的啦.
		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;//假如没有释放内存的在此释放.
	if (m_OutFilePool!=NULL) delete m_OutFilePool;
	if (m_intInFilePool!=NULL) delete m_intInFilePool;//假如没有释放内存的在此释放.
	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];		//分配内存给它.
	if (m_OutFilePool!=NULL) 
	{
		delete m_OutFilePool;
        m_OutFilePool=NULL;//如果不为空的指针,就将之前的输入文件缓冲清空.
	}
	m_OutFilePool=new byte[m_InFileLength+1];		//分配内存给它.
	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;  //内容加入到一个TXT框中显示出来.中能显示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];		//分配内存给它.
	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;  //内容加入到一个TXT框中显示出来.中能显示TXT类型的文件.
		UpdateData(false);
	}
	else
	{
		return;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -