用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

对一个目录下的多个文件DES加密解密

2016-08-14 作者: 小章举报

[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;
}

[源代码打包下载]




网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...