[c++]代码库
// BatchDESEncrypt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "BatchDESEncrypt.h"
#include "DES.hpp"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
BYTE * m_pFileData;
CDES des;
void EncodeFile(UINT ReadBytes)
{
unsigned char* data;
UINT UpperBound = ReadBytes - 7;
for(UINT i = 0; i < UpperBound ; i += 8)
{
data = (byte*)&m_pFileData[i];
data = des.EncryptData((unsigned char*)data);
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,_T("All Files(*.*)|*.*|"));
if (dlg.DoModal()==IDOK)
{
des.EncryptKey("12345678");
UINT PartLength=(UINT_MAX-INT_MAX)/8;
CString targetPath="D:";
try
{
m_pFileData = new BYTE[PartLength];
}
catch(CMemoryException)
{
}
POSITION pos = dlg.GetStartPosition();
while (pos != 0)
{
CFile* pFile = new CFile(dlg.GetNextPathName(pos),CFile::modeRead | CFile::shareDenyWrite);
CString m_fileTitle=pFile->GetFileName();
DWORD FileLength=pFile->GetLength();
CFile wrFile;
CString strName;
strName.Format("%s\\%s",targetPath,m_fileTitle);
if(wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate))
{
try
{
for (DWORD ReadBytes=pFile->Read(m_pFileData,PartLength);ReadBytes>0;ReadBytes=pFile->Read(m_pFileData,PartLength))
{
EncodeFile(ReadBytes);
wrFile.Write(m_pFileData,ReadBytes);
wrFile.Flush();
}
}
catch(CFileException) {
wrFile.Close();
pFile->Close();
delete pFile;
pFile = NULL;
delete[] m_pFileData;
m_pFileData = NULL;
}
wrFile.Close();
}
pFile->Close();
delete pFile;
pFile = NULL;
}
delete[] m_pFileData;
m_pFileData = NULL;
}
}
return nRetCode;
}
// BatchDESDecrypt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "BatchDESDecrypt.h"
#include "DES.hpp"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
BYTE * m_pFileData;
CDES des;
void DecodeFile(UINT ReadBytes)
{
unsigned char* data;
UINT UpperBound = ReadBytes - 7;
for(UINT i = 0; i < UpperBound ; i += 8)
{
data = (byte*)&m_pFileData[i];
data = des.DecryptData((unsigned char*)data);
}
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,_T("All Files(*.*)|*.*|"));
if (dlg.DoModal()==IDOK)
{
des.EncryptKey("12345678");
UINT PartLength=(UINT_MAX-INT_MAX)/8;
CString targetPath="D:";
try
{
m_pFileData = new BYTE[PartLength];
}
catch(CMemoryException)
{
}
POSITION pos = dlg.GetStartPosition();
while (pos != 0)
{
CFile* pFile = new CFile(dlg.GetNextPathName(pos),CFile::modeRead | CFile::shareDenyWrite);
CString m_fileTitle=pFile->GetFileName();
DWORD FileLength=pFile->GetLength();
CFile wrFile;
CString strName;
strName.Format("%s\\%s",targetPath,m_fileTitle);
if(wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate))
{
try
{
for (DWORD ReadBytes=pFile->Read(m_pFileData,PartLength);ReadBytes>0;ReadBytes=pFile->Read(m_pFileData,PartLength))
{
DecodeFile(ReadBytes);
wrFile.Write(m_pFileData,ReadBytes);
wrFile.Flush();
}
}
catch(CFileException) {
wrFile.Close();
pFile->Close();
delete pFile;
pFile = NULL;
delete[] m_pFileData;
m_pFileData = NULL;
}
wrFile.Close();
}
pFile->Close();
delete pFile;
pFile = NULL;
}
delete[] m_pFileData;
m_pFileData = NULL;
}
}
return nRetCode;
}
[源代码打包下载]
初级程序员
by: 加点盐 发表于:2018-04-04 14:39:29 顶(0) | 踩(0) 回复
谢谢,学习中···
回复评论