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

📄 blowfish.cpp

📁 BlowFish加密和解密算法 BlowFish加密和解密算法
💻 CPP
📖 第 1 页 / 共 2 页
字号:

inline void BlockToBytes(SBlock const& b, unsigned char* p)
	{
	unsigned int y;
	//Right
	y = b.m_uir;
	*--p = Byte(y);
	y = b.m_uir >> 8;
	*--p = Byte(y);
	y = b.m_uir >> 16;
	*--p = Byte(y);
	y = b.m_uir >> 24;
	*--p = Byte(y);
	//Left
	y = b.m_uil;
	*--p = Byte(y);
	y = b.m_uil >> 8;
	*--p = Byte(y);
	y = b.m_uil >> 16;
	*--p = Byte(y);
	y = b.m_uil >> 24;
	*--p = Byte(y);
	}

//Encrypt Buffer in Place
//Returns false if n is multiple of 8
void CBlowFish::Encrypt(unsigned char* buf, size_t n, int iMode)
	{
	//Check the buffer's length - should be > 0 and multiple of 8
	if((n==0)||(n%8!=0))
		throw exception("Incorrect buffer length");
	SBlock work;
	if(iMode == CBC) //CBC mode, using the Chain
		{
		SBlock chain(m_oChain);
		for(; n >= 8; n -= 8)
			{
			BytesToBlock(buf, work);
			work ^= chain;
			Encrypt(work);
			chain = work;
			BlockToBytes(work, buf+=8);
			}
		}
	else if(iMode == CFB) //CFB mode, using the Chain
		{
		SBlock chain(m_oChain);
		for(; n >= 8; n -= 8)
			{
			Encrypt(chain);
			BytesToBlock(buf, work);
			work ^= chain;
			chain = work;
			BlockToBytes(work, buf+=8);
			}
		}
	else //ECB mode, not using the Chain
		{
		for(; n >= 8; n -= 8)
			{
			BytesToBlock(buf, work);
			Encrypt(work);
			BlockToBytes(work, buf+=8);
			}
		}
	}

//Decrypt Buffer in Place
//Returns false if n is multiple of 8
void CBlowFish::Decrypt(unsigned char* buf, size_t n, int iMode)
	{
	//Check the buffer's length - should be > 0 and multiple of 8
	if((n==0)||(n%8!=0))
		throw exception("Incorrect buffer length");
	SBlock work;
	if(iMode == CBC) //CBC mode, using the Chain
		{
		SBlock crypt, chain(m_oChain);
		for(; n >= 8; n -= 8)
			{
			BytesToBlock(buf, work);
			crypt = work;
			Decrypt(work);
			work ^= chain;
			chain = crypt;
			BlockToBytes(work, buf+=8);
			}
		}
	else if(iMode == CFB) //CFB mode, using the Chain, not using Decrypt()
		{
		SBlock crypt, chain(m_oChain);
		for(; n >= 8; n -= 8)
			{
			BytesToBlock(buf, work);
			Encrypt(chain);
			crypt = work;
			work ^= chain;
			chain = crypt;
			BlockToBytes(work, buf+=8);
			}
		}
	else //ECB mode, not using the Chain
		{
		for(; n >= 8; n -= 8)
			{
			BytesToBlock(buf, work);
			Decrypt(work);
			BlockToBytes(work, buf+=8);
			}
		}
	}

//Encrypt from Input Buffer to Output Buffer
//Returns false if n is multiple of 8
void CBlowFish::Encrypt(const unsigned char* in, unsigned char* out, size_t n, int iMode)
	{
	//Check the buffer's length - should be > 0 and multiple of 8
	if((n==0)||(n%8!=0))
		throw exception("Incorrect buffer length");
	SBlock work;
	if(iMode == CBC) //CBC mode, using the Chain
		{
		SBlock chain(m_oChain);
		for(; n >= 8; n -= 8, in += 8)
			{
			BytesToBlock(in, work);
			work ^= chain;
			Encrypt(work);
			chain = work;
			BlockToBytes(work, out+=8);
			}
		}
	else if(iMode == CFB) //CFB mode, using the Chain
		{
		SBlock chain(m_oChain);
		for(; n >= 8; n -= 8, in += 8)
			{
			Encrypt(chain);
			BytesToBlock(in, work);
			work ^= chain;
			chain = work;
			BlockToBytes(work, out+=8);
			}
		}
	else //ECB mode, not using the Chain
		{
		for(; n >= 8; n -= 8, in += 8)
			{
			BytesToBlock(in, work);
			Encrypt(work);
			BlockToBytes(work, out+=8);
			}
		}
	}

//Decrypt from Input Buffer to Output Buffer
//Returns false if n is multiple of 8
void CBlowFish::Decrypt(const unsigned char* in, unsigned char* out, size_t n, int iMode)
	{
	//Check the buffer's length - should be > 0 and multiple of 8
	if((n==0)||(n%8!=0))
		throw exception("Incorrect buffer length");
	SBlock work;
	if(iMode == CBC) //CBC mode, using the Chain
		{
		SBlock crypt, chain(m_oChain);
		for(; n >= 8; n -= 8, in += 8)
			{
			BytesToBlock(in, work);
			crypt = work;
			Decrypt(work);
			work ^= chain;
			chain = crypt;
			BlockToBytes(work, out+=8);
			}
		}
	else if(iMode == CFB) //CFB mode, using the Chain, not using Decrypt()
		{
		SBlock crypt, chain(m_oChain);
		for(; n >= 8; n -= 8, in += 8)
			{
			BytesToBlock(in, work);
			Encrypt(chain);
			crypt = work;
			work ^= chain;
			chain = crypt;
			BlockToBytes(work, out+=8);
			}
		}
	else //ECB mode, not using the Chain
		{
		for(; n >= 8; n -= 8, in += 8)
			{
			BytesToBlock(in, work);
			Decrypt(work);
			BlockToBytes(work, out+=8);
			}
		}
	}

void CBlowFish::Encrypt( const char* strInput, std::string& strOutput,const const char* strKey,int bitCount)
	{
		CBlowFish oBlowFish((unsigned char*)strKey, bitCount);		
		int nLength = strlen( strInput );
		int nDifference = nLength % 8;
		int nNewLength = nLength;

		if( nDifference	!= 0 )
			nNewLength = (nLength - nDifference) + 8;

		char* pszDataOut = new char[ nNewLength + 1 ];
		
		oBlowFish.Encrypt((unsigned char*)strInput, (unsigned char*)pszDataOut, nNewLength);
		pszDataOut[ nNewLength ]='\0';

		strOutput = pszDataOut;
		delete pszDataOut;
		
	}

void CBlowFish::Decrypt( const char* strInput, std::string& strOutput,const char* strKey,int bitCount)
	{
		CBlowFish oBlowFish((unsigned char*)strKey, bitCount);		
		int nLength = strlen( strInput );
		int nDifference = nLength % 8;
		int nNewLength = nLength;

		if( nDifference	!= 0 )
			nNewLength = (nLength - nDifference) + 8;

		char* pszDataOut = new char[ nNewLength ];
		oBlowFish.Decrypt((unsigned char*)strInput, (unsigned char*)pszDataOut, nNewLength);
		
		strOutput = pszDataOut;

		delete pszDataOut;
	}

/*
void CBlowFish::Encrypt( const CString& strInput, CString& strOutput ,const CString& strKey,int bitCount)
	{
		CBlowFish oBlowFish((unsigned char*)(LPCWSTR)strKey, bitCount);	
		int nLength = strInput.GetLength( );
		char* pszByteArray = 0;
		
		int count = 0;
		
		for(int i=0;i < nLength; i++ )
			{
				int c = strInput[i];
				char result[10];
				itoa(c,result,10);
				int temp = (int)strlen(result) + 1;
				count += temp;
			}
		pszByteArray = new char[ count + 1];
		Encode( strInput ,pszByteArray,count);
		
		nLength = (int)strlen(pszByteArray);
		int nDifference = nLength % 8;
		int nNewLength = nLength;
		
		if( nDifference	!= 0 )
			nNewLength = (nLength - nDifference) + 8;
		
		char* pszDataOut = new char[ nNewLength ];	
								
		oBlowFish.Encrypt((unsigned char*)pszByteArray, (unsigned char*)pszDataOut, nNewLength);
		CharStr2HexStr((unsigned char*)pszDataOut, strOutput, nNewLength);

		delete pszDataOut;
		delete pszByteArray;
	
	}
void CBlowFish::Decrypt( const CString& strInput, CString& strOutput ,const CString& strKey,int bitCount)
	{
		CBlowFish oBlowFish((unsigned char*)(LPCWSTR)strKey, bitCount);
		int nLength = strInput.GetLength( );
		int nDifference = nLength % 8;
		int nNewLength = nLength;

		if( nDifference	!= 0 )
			nNewLength = (nLength - nDifference) + 8;
		
		char* pszByteArray = new char[ nLength+1 ];
		char* pszHex = new char[ nNewLength ];

		for(int i=0;i < nLength; i++ )
			pszByteArray[i] = ( char ) strInput[ i ];
		
		pszByteArray[nLength]='\0';

		HexStr2CharStr( pszByteArray,(unsigned char*)pszHex,nNewLength);
		oBlowFish.Decrypt((unsigned char*)pszHex, (unsigned char*)pszHex, nNewLength);

		pszHex[ nLength/2 ] ='\0';
		strOutput = pszHex;	
		Decode( strOutput, strOutput );
		
		delete pszHex;
		delete pszByteArray;
		
	}
*/
void Char2Hex(const unsigned char ch, char* szHex)
	{
		unsigned char byte[2];
		byte[0] = ch/16;
		byte[1] = ch%16;
		for(int i=0; i<2; i++)
			{
			if(byte[i] >= 0 && byte[i] <= 9)
				szHex[i] = '0' + byte[i];
			else
				szHex[i] = 'A' + byte[i] - 10;
			}
		szHex[2] = 0;
	}

//Function to convert string of length 2 to unsigned char
void Hex2Char(const char* szHex, unsigned char& rch)
	{
		rch = 0;
		for(int i=0; i<2; i++)
			{
			if(*(szHex + i) >='0' && *(szHex + i) <= '9')
				rch = (rch << 4) + (*(szHex + i) - '0');
			else if(*(szHex + i) >='A' && *(szHex + i) <= 'F')
				rch = (rch << 4) + (*(szHex + i) - 'A' + 10);
			else
				break;
			}
	}    

//Function to convert string of unsigned chars to string of chars
void CharStr2HexStr(const unsigned char* pucCharStr, std::string& strOut, int iSize)
	{
		int i;
		char szHex[3];
		
		for(i=0; i<iSize; i++)
			{
				Char2Hex(pucCharStr[i], szHex);
				strOut += szHex;
			}		
		
	}

//Function to convert string of chars to string of unsigned chars
void HexStr2CharStr(const char* pszHexStr, unsigned char* pucCharStr, int iSize)
	{
		int i;
		unsigned char ch;
		for(i=0; i<iSize; i++)
			{
				Hex2Char(pszHexStr+2*i, ch);
				pucCharStr[i] = ch;
			}
	}
/*
void Encode( const CString& strInput,char* szOut ,int count)
	{
		int nLength = strInput.GetLength( );
		
		std::string strOutput;

		for(int i=0;i < nLength; i++ )
			{
				int c = strInput[i];
				char result[20]="";
				itoa(c,result,10);
				strcat(result,"|");
				strOutput += result;
			}
		
		strcpy( szOut,strOutput.c_str() );
	}

void Decode( const CString& strInput,CString& strOutput )
	{
		int nLength = strInput.GetLength();
		int start = 0;
		wchar_t* result = new wchar_t[nLength/2];
		int offset =0;

		while(true)
			{
			    CString input = strInput.Tokenize(_T("|"),start);
				if( start == -1 )
					break;
				result[offset++] = _wtoi( input );
			}

		result[ offset ] ='\0';
		strOutput = result;
		delete result;
	}*/

⌨️ 快捷键说明

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