?? blowfishex.cpp
字號:
/*
Copyright 2006 - 2008
ZhangLuduo <zhangluduo@msn.com>
All Rights Reserved.
Blowfish 對稱加密算法之?dāng)U展, 僅用于對文件進(jìn)行加密解解
作者 - 張魯奪(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 = "input data too short!";
break;
case BLOWFISH_INPUTTOOLONG:
m_strError = "input data 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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -