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

📄 blowfishex.cpp

📁 利用Blowfish对称加密算法对文件进行加密, 内含VC++ 6.0源码
💻 CPP
字号:

/*
	Copyright 2006 - 2008
	ZhangLuduo <zhangluduo@msn.com>
	All Rights Reserved.

	Blowfish 对称加密算法之扩展, 仅用于对文件进行加密解解

	作者	- 张鲁夺(zhangluduo)
	MSN		- zhangluduo@msn.com
	QQ群	- 34064264

	为所有爱我的人和我爱的人努力!
*/

#include "stdafx.h"
#include "BlowfishEx.h"

BlowfishEx::BlowfishEx()
{
	m_bTerminate = false;
}

int BlowfishEx::Init( unsigned char* const key, int keyLen )
{
	bool bRepeat = true;
	for(int i = 0 ; i < keyLen ; i++)
	{
		if(i < keyLen - 1)
		{
			if(key[i] != key[i + 1])
			{
				bRepeat = false;
				break;
			}
		}
	}
	if(bRepeat)
	{
		m_strError = "the key not validate!";
		return BLOWFISH_KEYNOTVALIDATE;
	}

	int nRes = Blowfish::Init(key, keyLen);

	if(nRes != BLOWFISH_SUCCESS)
	{
		switch(nRes)
		{
			case BLOWFISH_NULL:
				m_strError = "input pointer is null!";
				break;
			case BLOWFISH_INPUTTOOSHORT:
				m_strError = "the key length too short!";
				break;
			case BLOWFISH_INPUTTOOLONG:
				m_strError = "the key length too long!";
				break;
		}
	}
	return nRes;
}

void BlowfishEx::OnFileProcessing(int nProgress, MemberFxn addr)
{
	void*			pThis	= addr.GetThis();
	unsigned long	Addr	= addr.GetAddr();

	__asm
	{
		push	nProgress	;
	 	mov		ecx, pThis	;
		call	Addr		;
	}
}

void BlowfishEx::Buf(unsigned char* const pData, int DataLen, bool IsEncrypt)
{
	if(pData == NULL)
		return ;

	unsigned long	*pLeft  =  NULL,
					*pRight =  NULL;

	for(int i = 0; i < (int)(DataLen / 8) ; i++)
	{
		pLeft  = (unsigned long*) (pData + 4 * (2 * i));
		pRight = (unsigned long*) (pData + 4 * (2 * i + 1));

		if(IsEncrypt)
			Encrypt(pLeft, pRight);
		else
			Decrypt(pLeft, pRight);
	}
}

bool BlowfishEx::FileBF(const char* inFileName, const char* outFileName, bool IsEncrypt, MemberFxn addr)
{
	m_bTerminate = false;
	m_strError = "";

	if(inFileName == NULL || outFileName == NULL)
	{
		m_strError = "file pointer not null!";
		return false;
	}

	FILE* inFile = fopen(inFileName, "rb");
	if(inFile == NULL)
	{
		m_strError = "input file open failed!";
		return false;
	}

	FILE* outFile = fopen(outFileName, "wb");
	if(outFile == NULL)
	{
		m_strError = "output file open failed!";
		return false;
	}

	__int64 nFileSize = GetFileSize(inFileName);
	if(nFileSize == -1)
	{
		m_strError = "get file size failed!";
		return false;
	}

	unsigned char buffer[BUFFERSIZE] = { 0 };

	if(!addr.IsNull())
		OnFileProcessing(0, addr);

	int nLen = fread(buffer, 1, BUFFERSIZE, inFile);
	int ReadCount = 0;

	while(nLen)
	{
		if(m_bTerminate)
		{
			fclose(inFile);
			fclose(outFile);
			m_strError = "user terminate!";
			return false;
		}

		Buf((unsigned char*)buffer, nLen, IsEncrypt);
		fwrite(buffer, 1, nLen, outFile);
		memset(buffer, 0, BUFFERSIZE);
		nLen = fread(buffer, 1, BUFFERSIZE, inFile);

		ReadCount ++;
		if(!addr.IsNull())
		{
			int n = (int)(nFileSize / 100);
			if(n <= 0)
				n = 1;
			OnFileProcessing((int)(BUFFERSIZE * ReadCount / n), addr);
		}
	}

	if(!addr.IsNull())
		OnFileProcessing(100, addr);

	fclose(inFile);
	fclose(outFile);
	
	return true;
}

__int64 BlowfishEx::GetFileSize(const char* szFileName)
{
    HANDLE hFile = CreateFile( szFileName, GENERIC_READ,
								FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );

	if(hFile == INVALID_HANDLE_VALUE)
	{
		CloseHandle(hFile);
		return -1;
	}

	DWORD dwFileSizeH = 0;
	__int64 qwFileSize = ::GetFileSize(hFile, &dwFileSizeH);
	qwFileSize |= (((__int64)dwFileSizeH) << 32);

	CloseHandle(hFile);

	return qwFileSize;
}

string BlowfishEx::GetError()
{
	return m_strError;
}

void BlowfishEx::TerminateBF()
{
	m_bTerminate = true;
}

⌨️ 快捷键说明

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