利用xor特点进行简单加密解密

兴趣研究代码,写在博客以备今后翻阅参考一下

一、利用内联汇编实现简单加密

// Translate.cpp: implementation of the CTranslate class. // // #include "stdafx.h" #include "Ch121.h" #include "Translate.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif // // Construction/Destruction // CTranslate::CTranslate() { } CTranslate::~CTranslate() { } void CTranslate::TranslateBuffer(char * buf,unsigned count,unsigned char eChar) { __asm { mov esi,buf; mov ecx,count; mov al,eChar; L1: xor [esi],al; inc esi; loop L1; }//end ams }

 

二、功能实现

/************************************************************************/ /* 文件加密 */ /************************************************************************/ void CCh121Dlg::OnButtonEncry() { char key[2]; if (GetDlgItemText(IDC_EDIT_KEY,key,2)==0) { MessageBox("请输入一个字符作为加密Key","文件加密",MB_OK); return; } char eChar = key[0];//取加密字符 // TODO: Add your control notification handler code here //打开文件选择对话框 CFileDialog *lpszOpenFile; lpszOpenFile = new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_HIDEREADONLY,NULL,this); CString szFileName;//原文件名 CString szTargetFileName;//目标文件名 CONST int BUFFERSIZE = 2000;//数据缓冲区大小 char buffer[BUFFERSIZE];//文件数据缓冲区 unsigned count;//字符计数 if (lpszOpenFile->DoModal() == IDCANCEL) { return; } CFile * lpFile = new CFile(lpszOpenFile->GetPathName(),CFile::shareDenyNone); long fileLen =lpFile->GetLength(); lpFile->Close(); delete lpFile; //初始化进度条 CWnd * lpWnd = GetDlgItem(IDC_PROGRESS1); CProgressCtrl *lpProgress = (CProgressCtrl *)lpWnd; lpProgress->SetRange32(0,fileLen); lpProgress->SetPos(0); //得到打开文件的路径 szFileName.Format("%s",lpszOpenFile->GetPathName()); szTargetFileName.Format("%s",szFileName+".jzp"); SetWindowText(szFileName); if (lpszOpenFile!=NULL) { delete lpszOpenFile; } ifstream infile(szFileName,ios::binary); ofstream oufile(szTargetFileName,ios::binary); CTranslate *pTranslate = new CTranslate(); while (!infile.eof()) { infile.read(buffer,BUFFERSIZE); count = infile.gcount(); pTranslate->TranslateBuffer(buffer,count,eChar); oufile.write(buffer,count); lpProgress->SetPos(lpProgress->GetPos()+count); } delete pTranslate; infile.close(); oufile.flush(); oufile.close(); MessageBox("加密完成","文件加密",MB_OK); } void CCh121Dlg::OnChangeEditKey() { // 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 CString keys; if (GetDlgItemText(IDC_EDIT_KEY,keys)>1) { SetDlgItemText(IDC_EDIT_KEY,keys.Left(1)); return; } } void CCh121Dlg::OnButtonDencry() { char key[2]; if (GetDlgItemText(IDC_EDIT_KEY,key,2)==0) { MessageBox("请输入一个字符作为解密Key","文件解密",MB_OK); return; } char eChar = key[0];//取加密字符 // TODO: Add your control notification handler code here CFileDialog * lpFileDlg = new CFileDialog(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,NULL,this); CString pathName; //待解压的文件名 CString targetPathName;//解压后的目标文件名 if (lpFileDlg->DoModal() == IDCANCEL) { return; } pathName.Format("%s",lpFileDlg->GetPathName()); delete lpFileDlg; //取出解压后的目标文件名 targetPathName.Format("%s",pathName); targetPathName.Delete(targetPathName.GetLength()-4,4); //初始化进度条 CFile *lpFile = new CFile(pathName,CFile::shareDenyNone); long fileLen = lpFile->GetLength(); delete lpFile; CProgressCtrl * lpProgress = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS1); lpProgress->SetPos(0); lpProgress->SetRange32(0,fileLen); const int BUFFERSIZE = 2000; char buffer[BUFFERSIZE]; int count = 0; ifstream infile(pathName,ios::binary); ofstream oufile(targetPathName,ios::binary); CTranslate * lpTranslate = new CTranslate(); while (!infile.eof()) { infile.read(buffer,BUFFERSIZE); count = infile.gcount();//获取实际读取字节数 lpTranslate->TranslateBuffer(buffer,count,eChar); oufile.write(buffer,count);//写入目标文件 lpProgress->SetPos(lpProgress->GetPos()+count); } infile.close(); oufile.flush(); oufile.close(); }

三、界面

你可能感兴趣的:(Win32汇编,加密,buffer,delete,null,解密,ios)