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

📄 fileblowfish.cpp

📁 BlowFish算法的压缩源码
💻 CPP
字号:
/**********************************************************************
 * Developed by;
 *	Neal Horman - neal@wanlink.com - http://www.wanlink.com
 *	Copyright (c) 2002 Neal Horman, All Rights Reserved
 *
 * This file/code may be used, modified, and distributed, for any 
 * purpose providing that this notice and the copyright notice above
 * is left fully intact, unaltered, and any modifications, additions, 
 * or bug fixes are clearly marked as such.
 *
 * This file/code is provided "as is" with no express or implied 
 * warranty of any kind whatsoever. The author expressly accepts no 
 * liability for any possible damages incurred as a result of it's use.
 * Use it at your own risk.
 *
 * Expect bugs.
 *
 * Portions Copyright (C) Microsoft Corporation
 *
 *		RCSID:  $Id$
 *
 * DESCRIPTION:
 *		application:	BlowFishDocView Demo
 *		source module:	FileBlowFish.cpp
 *
 **********************************************************************/

// FileBlowFish.cpp: implementation of the CFileBlowFish class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileBlowFish.h"
#include "BlowFish.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNAMIC(CFileBlowFish,CFile);

// Overridables
CFileBlowFish::~CFileBlowFish()
{
	if(m_pCodecBuf != NULL)
		free(m_pCodecBuf);
}

// *** Cut and paste from MFC\SRC\filecore.cpp and modified to use CFileBlowFish
CFile* CFileBlowFish::Duplicate() const
{
	ASSERT_VALID(this);
	ASSERT(m_hFile != (UINT)hFileNull);

	CFileBlowFish* pFile = new CFileBlowFish(hFileNull);	// *** Modified line
	HANDLE hFile;
	if (!::DuplicateHandle(::GetCurrentProcess(), (HANDLE)m_hFile,
		::GetCurrentProcess(), &hFile, 0, FALSE, DUPLICATE_SAME_ACCESS))
	{
		delete pFile;
		CFileException::ThrowOsError((LONG)::GetLastError());
	}
	pFile->m_hFile = (UINT)hFile;
	ASSERT(pFile->m_hFile != (UINT)hFileNull);
	pFile->m_bCloseOnDelete = m_bCloseOnDelete;
	return pFile;
}


UINT CFileBlowFish::Read(void* lpBuf, UINT nCount)
{	unsigned int nSize = m_Cypher.GetOutputLength(nCount);	// from blowfish.cpp - "... be sure buffer length is even MOD 8."
	void	*pBuf = GetCodecBuf(nSize);
	UINT	rc = CFile::Read(pBuf, nCount);

	if(rc > 0 )
	{
		if(m_bCodecIng)
			m_Cypher.Decode((unsigned char *)pBuf, (unsigned char *)pBuf, nSize);
		memcpy(lpBuf,pBuf,rc);
	}

	return(rc);
}

void CFileBlowFish::Write(const void* lpBuf, UINT nCount)
{	unsigned int nSize = m_Cypher.GetOutputLength(nCount);	// from blowfish.cpp - "... be sure buffer length is even MOD 8."
	void	*pBuf = GetCodecBuf(nSize);

	if(m_bCodecIng && m_bFirstWrite)
	{
		CFile::Write(m_magicCookie,sizeof(m_magicCookie));
		m_bFirstWrite	= FALSE;
	}

	memcpy(pBuf,lpBuf,nCount);
	if(m_bCodecIng)
		m_Cypher.Encode((unsigned char *)pBuf, (unsigned char *)pBuf, nSize);

	CFile::Write(pBuf, nSize);	// write out the entire blowfish buffer, not the nCount
}

void CFileBlowFish::MemberInit()
{	unsigned char keyBytes[]	= "Bl0wF15h";	// *** You should change this to be your own seed
	unsigned char cookie[]		= "EBF\x55\xaa\x55\xaa\x55";

	m_Cypher.Initialize(keyBytes, sizeof(keyBytes));
	memcpy(m_magicCookie, cookie, sizeof(m_magicCookie));
	m_CodecBufSize	= 0;
	m_pCodecBuf		= NULL;
	m_bFirstWrite	= TRUE;
	m_bCodecIng		= TRUE;
}

void *CFileBlowFish::GetCodecBuf(unsigned int nCount)
{	
	if(nCount > m_CodecBufSize)
	{
		if(m_pCodecBuf != NULL)
			free(m_pCodecBuf);
		m_CodecBufSize = nCount;
		m_pCodecBuf = (void *)malloc(m_CodecBufSize);
	}

	if(m_pCodecBuf != NULL)
		memset(m_pCodecBuf,0,m_CodecBufSize);

	return(m_pCodecBuf);
}


BOOL CFileBlowFish::Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError)
{	BOOL	bOk		= CFile::Open(lpszFileName, nOpenFlags, pError);
	BOOL	bFishForCookie = FALSE;

	m_bFirstWrite	= TRUE;
	m_bCodecIng		= TRUE;

	switch(nOpenFlags & (CFile::modeRead | CFile::modeReadWrite | CFile::modeWrite))
	{
		case CFile::modeRead:
		case CFile::modeReadWrite:
			bFishForCookie = TRUE;
			break;
	};

	if(bOk && bFishForCookie)
	{	void *pCookie = malloc(sizeof(m_magicCookie));

		if(CFile::Read(pCookie,sizeof(m_magicCookie)) == sizeof(m_magicCookie))
			m_bCodecIng = (memcmp(pCookie,m_magicCookie,sizeof(m_magicCookie)) == 0);
		free(pCookie);

		if(!m_bCodecIng)
			CFile::Seek(0,CFile::begin);
	}

	return(bOk);
}

DWORD CFileBlowFish::GetPosition() const
{	DWORD	pos = CFile::GetPosition();

	return(m_bCodecIng ? pos - sizeof(m_magicCookie) : pos);
}

void CFileBlowFish::SetLength(DWORD dwNewLen)
{
	CFile::SetLength(dwNewLen + (m_bCodecIng ? sizeof(m_magicCookie) : 0));
}

LONG CFileBlowFish::Seek(LONG lOff, UINT nFrom)
{	long ofs = CFile::Seek(lOff + (m_bCodecIng && nFrom == CFile::begin ? sizeof(m_magicCookie) : 0), nFrom);

	return(m_bCodecIng ? ofs - sizeof(m_magicCookie) : ofs);
}

⌨️ 快捷键说明

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