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

📄 fileencryptiondlg.cpp

📁 用DES加密算法实现文件的加密和解密
💻 CPP
字号:
// FileEncryptionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileEncryption.h"
#include "FileEncryptionDlg.h"

#include  "math.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)
	virtual void OnOK();
	//}}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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileEncryptionDlg dialog

CFileEncryptionDlg::CFileEncryptionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileEncryptionDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileEncryptionDlg)
	m_oldFileName = _T("E:\\课设\\encryptent.txt");
	m_newFileName = _T("E:\\课设\\");
	m_n = 5;
	m_feedback = 37;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileEncryptionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileEncryptionDlg)
	DDX_Text(pDX, IDC_EDIT1, m_oldFileName);
	DDX_Text(pDX, IDC_EDIT2, m_newFileName);
	DDX_Text(pDX, IDC_EDIT5, m_n);
	DDX_Text(pDX, IDC_EDIT6, m_feedback);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileEncryptionDlg, CDialog)
	//{{AFX_MSG_MAP(CFileEncryptionDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnDeEncryption)
	ON_BN_CLICKED(IDOK, OnEncryption)
	ON_BN_CLICKED(IDC_BUTTON1, OnBrowse1)
	ON_BN_CLICKED(IDC_BUTTON3, OnBrowse2)
	ON_EN_CHANGE(IDC_EDIT5, OnChangeEdit5)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileEncryptionDlg message handlers

BOOL CFileEncryptionDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	MessageBox("wyj","程序作者",MB_OK); 

	// 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 CFileEncryptionDlg::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 CFileEncryptionDlg::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 CFileEncryptionDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CFileEncryptionDlg::OnDeEncryption() 
{
	// TODO: Add your control notification handler code here
	
	   UpdateData(true); 
	   serialLength=(pow(2,m_n)-1);
	   Serial=new int[serialLength];	
	   generateMSerial();
	   
	   //		int a1=Serial[1];
	   //	int a2=Serial[2];
	   int aaaa[31];
	   for(int i=0;i<31;i++)
		   aaaa[i]=Serial[i];
	   
	   if(!getDecryptedFile())
	   {	AfxMessageBox("请输入文件名");return ;}
	   
	   else AfxMessageBox("解密成功了");   
	   delete []Serial;
	   CFileEncryptionDlg::OnOK();
}

void CFileEncryptionDlg::OnEncryption() 
{
	// TODO: Add your control notification handler code here
	///	int a,b;
	UpdateData(true); 
	serialLength=pow(2,m_n)-1;
    Serial=new int[serialLength];	
	generateMSerial();
	
	//		int a1=Serial[1];
	//	int a2=Serial[2];
	int aaaa[31];
	for(int i=0;i<31;i++)
		aaaa[i]=Serial[i];
	
	if(!getEncryptedFile())
	{AfxMessageBox("请输入文件名");return ;}
	
	else	AfxMessageBox("加密成功了");   
    delete []Serial;
	CFileEncryptionDlg::OnOK();
}

void CFileEncryptionDlg::OnBrowse1() 
{
	// TODO: Add your control notification handler code here
	CFileDialog  oldFile(true);
	if(oldFile.DoModal()==IDOK)
	{
		m_oldFileName=oldFile.GetPathName();
		UpdateData(FALSE);   //将信息写入文本框
	}
}

void CFileEncryptionDlg::OnBrowse2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CFileDialog  newFile(false);
	if(newFile.DoModal()==IDOK)
	{
		m_newFileName=newFile.GetPathName();
		UpdateData(FALSE);//将信息写入文本框
	}
}

bool CFileEncryptionDlg::EncryptedOrDeEncryptedFile()
{
   DWORD  flen,realflen;
   if(isEncryption)
   {	
	flen=oldFile.GetLength();
   }
   else
   {
	   oldFile.Seek(-4,CFile::end);
       oldFile.Read(&realflen,4);
	   flen=oldFile.GetLength()-4;
   }
    oldFileData=new  char[flen];
	oldFile.SeekToBegin();
	oldFile.Read(oldFileData,flen);

	int minwen[64];minwen[0]=0;
	int key[64];key[0]=0;
	int str[8];unsigned char ch;  int k;
	int b=0;

    DWORD  i=0;
	while(i<(flen-8))
	{	
         k=1;
		for(int a=0;a<8;a++)
		{
			ch=oldFileData[i];
			for(int j=0;j<8;j++)
			{
				str[j]=ch%2;
				ch=ch/2;
			}
			for(j=7;j>=0;j--)
			{
				minwen[k]=str[j];  //倒写
				k++;   //k指示当前需要处理的明文
			}
			i=i+1;
		}//64位明文获取
		if(isEncryption)
		flen=oldFile.GetLength();
		else
			flen=oldFile.GetLength()-4;

		for(int j=1;j<=64;j++)
		{ key[j]=Serial[b%serialLength];b++;}   //64位密钥获取 
		
		CEncryption  des(minwen,key,isEncryption);//加密
		des.desEncryption();
		//64位密文写回 
	   int temp=0;
	   for(j=0;j<8;j++)
	   {
		   for(int j1=1;j1<=8;j1++)
		   {
			   temp+=des.miwen[j*8+j1]*pow(2,8-j1);
		   }
           oldFileData[i-8+j]=temp;
		   temp=0;
	   }       

	}//for i;
	 
	//文件尾巴处理
	char endOfFile[8];
	int p=0;
	{
		k=1;
		for(int a=0;a<8;a++)
		{
			if(i<flen)
			{
                ch=oldFileData[i];						
			}
			else
				ch=0;
			for(int j=0;j<8;j++)
			{
			str[j]=ch%2;
			ch=ch/2;
			}
			for(j=7;j>=0;j--)
			{
				minwen[k]=str[j];  //倒写
				k++;   //k指示当前需要处理的明文
			}
			i++;		
		}
		if(isEncryption)
		flen=oldFile.GetLength();
		else
			flen=oldFile.GetLength()-4;

        for(int j=1;j<=64;j++)
		{ key[j]=Serial[b%serialLength];b++;}//64位密钥获取 

		CEncryption  des(minwen,key,isEncryption);
		des.desEncryption();
		//64位密文写回 
	   int temp=0;
	   for(j=0;j<8;j++)
	   {
		   for(int j1=1;j1<=8;j1++)
		   {
			   temp+=des.miwen[j*8+j1]*pow(2,8-j1);
		   }
		   if(i-8+j<flen)
		   {
           oldFileData[i-8+j]=temp;
		   }
		   else
		   {endOfFile[p]=temp;
		   p++;}
		   temp=0;
	   } 	   
	}
    if(isEncryption)
	{
		newFile.SeekToBegin();
		newFile.Write(oldFileData,flen);
		newFile.SeekToEnd();
		newFile.Write(endOfFile,p);
		newFile.SeekToEnd();
		newFile.Write(&flen,4);
	}
	else
	{
		oldFile.Seek(-4,CFile::end);
       oldFile.Read(&realflen,4);
		newFile.SeekToBegin();
		newFile.Write(oldFileData,realflen);
	}
	delete []oldFileData;
	return true;

}

bool CFileEncryptionDlg::getEncryptedFile()
{
	isEncryption=true;
   //将原文件读入
	if(!oldFile.Open(m_oldFileName,CFile::shareDenyNone|CFile::modeReadWrite))
		return false;
	if(!newFile.Open(m_newFileName, CFile::modeCreate|CFile::shareDenyNone|CFile::modeReadWrite))
		return false;
EncryptedOrDeEncryptedFile();
	
    
	return true;

}

void CFileEncryptionDlg::generateMSerial()
{
	//初始化
	int ch;
	int  *str;  
	str=new int[m_n+1];
	str[0]=0;
	for(int i=1;i<=m_n-1;i++)
		str[i]=0;
	str[m_n]=1;
	
	int *feedback=new int[m_n+1];
	ch=m_feedback;
	for( i=0;i<=m_n;i++)
	{
		feedback[m_n-i]=ch%2;
		ch=ch/2;
	}
	for( i=1;i<=m_n;i++)
	{
		if(feedback[i]==1)
		{
			str[0]=Xor(str[0],str[i]);
		}
	}
	Serial[0]=str[m_n];
	//pow(2,m_n)-1轮的移位
	// int temp;
//	int a1,a2,a3,a4,a5,a6;
	for(i=1;i<serialLength;i++)
	{
		// temp=str[m_n];
		
		for(int j=m_n;j>0;j--)
			str[j]=str[j-1];
		str[0]=0;
		
		for(int k=1;k<=m_n;k++)
		{
			if(feedback[k]==1)
			{
				str[0]=Xor(str[0],str[k]);
			}
		}
		
		Serial[i]=str[m_n];
		
	}
	
	delete []str;
	delete []feedback;
	
	
}

int CFileEncryptionDlg::Xor(int a, int b)
{
  if(a==b)
	  return 0;
  else return 1;
}

bool CFileEncryptionDlg::getDecryptedFile()
{
   //将原文件读入
	isEncryption=false;
	if(!oldFile.Open(m_oldFileName,CFile::shareDenyNone|CFile::modeReadWrite))
		return false;
	if(!newFile.Open(m_newFileName, CFile::modeCreate|CFile::shareDenyNone|CFile::modeReadWrite))
		return false;


	EncryptedOrDeEncryptedFile();
	return true;
}

void CFileEncryptionDlg::OnChangeEdit5() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CAboutDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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