dcsimg

CArchiveExt - Enhanced archiving class with encryption and compression

WEBINAR:
On-Demand

Application Security Testing: An Integral Part of DevOps


CArchiveExt is enhanced archiving class derived from CArchive. It have 2 additional features: encryption and compression. CArchiveExt class may be used instead of CArchive class in serialization.

The class uses the zlib source code. The 'zlib' general purpose compression library version 1.1.3, July 9th, 1998. Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler.


To use CArchiveExt, do the following:
  • include CArchiveEx.h in your project
  • link your project with zlib.lib
  • use constructor CArchiveExt instead of CArchive
  • use standart functions of CArchive class for serialization, loading, saving etc.



CArchiveEx.h file:

struct ArcHeader
{
	_TCHAR		chSignature[3];		// "akz" ;-) (it may be replaced anyhow)
	BYTE		uchFlag;		// 0-plane text; 1-compression; 2-encryption; 
						// 3-compression & encryption;
	DWORD		dwOriginalSize;		// decompression size
	ULONG		ulCRC;			// CRC
};

class CArchiveExt : public CArchive
{
	CString 	m_strKey;		// Key for the encryption algorithm. 
	BOOL		m_bCompress;		// If TRUE then compress or de-compress the data
	CFile*		m_pFileTmp; 		// temporary file
	BYTE*		m_pBuf;

public:
	CArchiveExt(CFile* pFile, UINT nMode, int nBufSize = 4096, void* lpBuf = NULL, CString Key = _TEXT(""), BOOL bCompress = FALSE);
	// if Key is not empty then encrypt or decrypt the data
	// if bCompress is TRUE then compress or de-compress the data

	virtual ~CArchiveExt();

	virtual void Close();
	virtual void Abort();

protected:
	virtual bool Crypto(LPSTR lpstrBuffer, ULONG ulLen, CString strKey = _TEXT(""));
	// This function may be overrided with your favourite algorithm
	// lpstrBuffer 	- pointer to data
	// ulLen	- size of buffer
	// strKey	- key of encription algorithm
};

CArchiveEx.cpp file:

CString signature = _TEXT("akz");

// constructor
CArchiveExt::CArchiveExt(CFile* pFile, UINT nMode, int nBufSize, void* lpBuf, CString Key, BOOL bCompress)
			:CArchive(pFile, nMode, nBufSize, lpBuf)
{
	m_strKey = Key;
	m_bCompress = bCompress;
	m_pFileTmp = m_pFile;

// for simplicity, if encrypt is enabled then compress is enabled too
	if(!Key.IsEmpty())
		m_bCompress = TRUE;

	if(m_bCompress)
	{
// if loading exist file
		if(IsLoading())
		{
	 	    	int iComprErr;
			CString Contents, Compress;
			_TCHAR sign[4];
			DWORD dwFileSize = m_pFileTmp -> GetLength();
			LPSTR szContents = Contents.GetBuffer(dwFileSize);
			DWORD dwComprSize;

			m_pBuf = (BYTE *)new char[dwFileSize];
			m_pFile = new CMemFile(m_pBuf, dwFileSize, 1024);

			m_pFileTmp -> SeekToBegin();

// read header
			ArcHeader ahead;
			m_pFileTmp -> Read(&ahead, sizeof(ahead));

			sign[0] = ahead.chSignature[0];
			sign[1] = ahead.chSignature[1];
			sign[2] = ahead.chSignature[2];
			sign[3] = _TEXT('\0');

// if compress and encrypt is enabled, and file contains signature
			if(signature == sign)
			{
				dwFileSize = m_pFileTmp -> ReadHuge(szContents, dwFileSize);

// if file compressed and encryption
				if(ahead.uchFlag == 3)
				{
					ULONG crc = adler32(0L, Z_NULL, 0);
// decription
					Crypto(szContents, dwFileSize, m_strKey);

// compute CRC decripted file
					crc = adler32(crc, (const unsigned char*)szContents, dwFileSize);
					if(crc != ahead.ulCRC)
					{
						AfxMessageBox("Password incorrect!!!");
						Abort();
						AfxThrowUserException();
					}
				}

// size of decompressed file
				dwComprSize = ahead.dwOriginalSize;
				LPSTR szCompress = Compress.GetBuffer(dwComprSize);
				iComprErr = uncompress((LPBYTE)szCompress, &dwComprSize, (LPBYTE)szContents, dwFileSize);
				ASSERT(iComprErr == Z_OK);

				m_pFile -> WriteHuge(szCompress, dwComprSize);
			}
			else
// if compress and encrypt is enabled, but file does not contain signature
			{
				m_pFileTmp -> SeekToBegin();
				dwFileSize = m_pFileTmp -> ReadHuge(szContents, dwFileSize);
				m_pFile -> WriteHuge(szContents, dwFileSize);
			}
			m_pFile -> SeekToBegin();
		}
		else
		{
// if save file
			m_pBuf = (BYTE *)new char[1024];
			m_pFile = new CMemFile(m_pBuf, 1024, 1024);
		}
	}
}

CArchiveExt::~CArchiveExt()
{
	if (m_pFile != NULL && !(m_nMode & bNoFlushOnDelete))
		Close();

	Abort();    // abort completely shuts down the archive
}

void CArchiveExt::Abort()
{
	if(m_bCompress && m_pFile != NULL)
	{
		m_pBuf = (BYTE *)(((CMemFile*)m_pFile)->Detach());
		delete m_pBuf;
		m_pFile -> Close();
		delete m_pFile;
	}

	CArchive::Abort();
}

void CArchiveExt::Close()
{
	ASSERT_VALID(m_pFile);
	Flush();

	if(m_bCompress)
	{
// if save file
		if(IsStoring())
		{
			int iComprErr;
			CString Compress;
			DWORD dwFileSize = m_pFile -> GetLength();
			DWORD dwComprSize;

			m_pBuf = (BYTE *)(((CMemFile*)m_pFile)->Detach());
			dwComprSize = (DWORD)(dwFileSize * 1.1) + 12;
			LPSTR szCompress = Compress.GetBuffer(dwComprSize);
			iComprErr = compress((LPBYTE)szCompress, &dwComprSize, m_pBuf, dwFileSize);
			ASSERT(iComprErr == Z_OK);

			ULONG crc = adler32(0L, Z_NULL, 0);
			crc = adler32(crc, (const unsigned char*)szCompress, dwComprSize);
			Crypto(szCompress, dwComprSize, m_strKey);

			ArcHeader ahead;
			ahead.chSignature[0] = _TEXT('a');
			ahead.chSignature[1] = _TEXT('k');
			ahead.chSignature[2] = _TEXT('z');
			ahead.dwOriginalSize = dwFileSize;
			ahead.uchFlag = m_strKey.IsEmpty()?1:3;
			ahead.ulCRC = crc;

			m_pFileTmp -> Write(&ahead, sizeof(ahead));
			m_pFileTmp -> WriteHuge(szCompress, dwComprSize);
		}
		else
		{
			m_pBuf = (BYTE *)(((CMemFile*)m_pFile)->Detach());
		}
		delete m_pBuf;
		m_pFile -> Close();
		delete m_pFile;
	}
	m_pFile = NULL;
}

bool CArchiveExt::Crypto(LPSTR lpstrBuffer, ULONG ulLen, CString strKey)
{
	if(strKey.IsEmpty())
		return FALSE;
	
	int nLenKey = strKey.GetLength();

	for(ULONG ii=0; ii<ulLen; ii++)
	{
		for(int jj=0; jj<nLenKey; jj++)
		{
			lpstrBuffer[ii] ^= strKey[jj];
		}
	}
	return TRUE;
}

Download demo project - 86 KB

Download source - 40 KB

Date Last Updated: May 7, 1999


Comments

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • CEOs, CIOs, boards and shareholders are demanding digital transformation. They want their organizations to be more customer-focused, competitive and strategic to increase revenue. Data drives all of those aspects. A great first step in optimizing data use is moving data to the cloud. It can quickly show benefits. Here are six aspects of a cloud data management strategy that will help your organization more fully move, manage and use valuable data to successfully support digital transformation.

  • We are in the middle of a computing revolution. All signs point to the rapid adoption of cloud computing as a means to facilitate not just changes in development methodologies, but changes to the very nature of IT architecture and maintenance.  But migrating to the cloud is not a simple matter. As with all disruptive technologies, any cloud migration initiative must be approached with caution, and based on a strategy that includes big-picture thinking with exacting attention to security issues. Read this …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.