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

📄 xor256block.cpp

📁 一些加密算法的介绍,可对文件或字符串加密
💻 CPP
📖 第 1 页 / 共 2 页
字号:

// XOR256Block.cpp
#include "stdafx.h"
#include "XOR256Block.h"
#include "SHA.h"
#include "DoubleBuffering.h"

#include <exception>
#include <strstream>

using namespace std;

char const* CXOR256Block::sm_szErrorMsgXB1 = "Illegal External Rounds!";
char const* CXOR256Block::sm_szErrorMsgXB2 = "Illegal Internal Rounds!";

//Null chain
char const CXOR256Block::sm_chain0[BLOCK_MAX] = {0};

//CONSTRUCTOR
CXOR256Block::CXOR256Block()
{
	m_blockSize = -1;
	m_keylength = KEY_MAX;
}

//DESTRUCTOR
CXOR256Block::~CXOR256Block()
{
	if(m_pucXOR != NULL)
		delete [] m_pucXOR;
	if(m_puc256 != NULL)
		delete [] m_puc256;
};

void CXOR256Block::Initialize(char const* keydata, int keydatalength, char const* chain,
	int blockSize, int iORounds, int iIRounds, int iMode, int iPadding)
{
	//Check Initialization Data
	if(NULL == keydata)
		throw runtime_error(string(sm_szErrorMsg4));
	if(keydatalength<1)
		throw runtime_error(string(sm_szErrorMsg5));
	if(blockSize<1 || blockSize>BLOCK_MAX)
		throw runtime_error(string(sm_szErrorMsg6));
	if(iORounds<1)
		throw runtime_error(string(sm_szErrorMsgXB1));
	if(iIRounds<1)
		throw runtime_error(string(sm_szErrorMsgXB2));
	if(iMode<ECB || iMode>CFB)
		throw runtime_error(string(sm_szErrorMsg2));
	if(iPadding<ZEROES || iPadding>PKCS7)
		throw runtime_error(string(sm_szErrorMsg3));
	m_iORounds = iORounds;
	m_iORounds1 = m_iORounds-1;
	m_iIRounds = iIRounds;
	m_iIRounds1 = m_iIRounds-1;
	m_iMode = iMode;
	m_iPadding = iPadding;
	//Create the Key from Key Data
	int i, j;
	char key[KEY_MAX];
	for(i=0,j=0; i<m_keylength; i++,j=(j+1)%keydatalength)
		key[i] = keydata[j];
	bool bSameKey = false;
	bool bSameChain = false;
	if(true == m_bInit)
	{
		//Only if already initialized
		if(m_blockSize == blockSize)
		{
			//Check the Chain if is the same
			if(0 == memcmp(m_apchain0.get(), chain, m_blockSize))
				bSameChain = true;
		}
		//Check the Key if is the same
		if(0 == memcmp(m_apKey.get(), key, m_keylength))
			bSameKey = true;
	}
	if(true == bSameChain)
		//Just Reset
		memcpy(m_apchain.get(), m_apchain0.get(), m_blockSize);
	else
	{
		if(m_blockSize != blockSize)
		{
			m_blockSize = blockSize;
			m_iBlockSize1 = m_blockSize-1;
			m_iProd = m_blockSize * m_iIRounds;
			m_iDelta = m_iProd*m_iORounds1;
			//Initialize the chain
			m_apchain0 = auto_ptr<char>(new char[m_blockSize]);
			m_apchain = auto_ptr<char>(new char[m_blockSize]);
			//Initializing temporary buffer
			m_apTemp = auto_ptr<unsigned char>(new unsigned char[m_blockSize]);
		}
		memcpy(m_apchain0.get(), chain, m_blockSize);
		memcpy(m_apchain.get(), chain, m_blockSize);
	}
	if(true == bSameKey)
	{
		//Fast Initialization
		m_oArcfourPRNG.Reset();
		return;
	}
	m_apKey = auto_ptr<char>(new char[m_keylength]);
	memcpy(m_apKey.get(), key, m_keylength);
	//Calculating the constants
	m_oArcfourPRNG.SetKey((unsigned char*)key, m_keylength);
	//The number of constants is given by ORounds * IRounds * BlockSize
	int iSize = m_iORounds*m_iIRounds*m_blockSize;
	m_pucXOR = new unsigned char[iSize];
	m_puc256 = new unsigned char[iSize];
	unsigned char* pucXOR = &m_pucXOR[0];
	unsigned char* puc256 = &m_puc256[0];
	for(i=0; i<iSize; i++)
	{
		*(pucXOR++) = m_oArcfourPRNG.Rand();
		*(puc256++) = m_oArcfourPRNG.Rand();
	}
	//Initialization Flag
	m_bInit = true;
}

void CXOR256Block::ResetChain()
{
	if(false==m_bInit)
		throw runtime_error(string(sm_szErrorMsg1));
	memcpy(m_apchain.get(), m_apchain0.get(), m_blockSize);
	//Is including ArcFourPRNG resetting
	m_oArcfourPRNG.Reset();
}

//Compute Signature
void CXOR256Block::Signature(char* pcSig)
{
	//11+256+3+3+3+1+1+1
	char acSigData[280] = {0};
	strcat(acSigData, "XOR256BLOCK");
	int iLen = strlen(acSigData);
	memcpy(acSigData+iLen, m_apKey.get(), m_keylength);
	sprintf(acSigData+iLen+m_keylength, "%d%d%d%d%d", m_blockSize, m_iIRounds, m_iORounds, m_iMode, m_iPadding);
	CSHA oSHA;
	oSHA.AddData(acSigData, strlen(acSigData));
	oSHA.FinalDigest(pcSig);
}

void CXOR256Block::EncryptDirect(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;
	for(int i=0; i<m_blockSize; i++,pucBlock++)
	{
		//The First Internal Round
		(*pucBlock ^= ucPrev^*(pucXOR++)) += *(puc256++);
		//The Last m_iIRounds-1 Internal Rounds
		for(int j=1; j<m_iIRounds; j++)
			(*pucBlock ^= *(pucXOR++)) += *(puc256++);
		ucPrev = *pucBlock;
	}
}

void CXOR256Block::EncryptReverse(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;
	pucBlock += m_iBlockSize1;
	for(int i=0; i<m_blockSize; i++,pucBlock--)
	{
		//The First Internal Round
		(*pucBlock ^= ucPrev^*(pucXOR++)) += *(puc256++);
		//The Last m_iIRounds-1 Internal Rounds
		for(int j=1; j<m_iIRounds; j++)
			(*pucBlock ^= *(pucXOR++)) += *(puc256++);
		ucPrev = *pucBlock;
	}
}

void CXOR256Block::EncryptDirect1(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;
	for(int i=0; i<m_blockSize; i++,pucBlock++)
	{
		ucPrev ^= *pucBlock;
		//The First Internal Round
		(*pucBlock = ucPrev^*(pucXOR++)) += *(puc256++);
		//The Last m_iIRounds-1 Internal Rounds
		for(int j=1; j<m_iIRounds; j++)
			(*pucBlock ^= *(pucXOR++)) += *(puc256++);
	}
}

void CXOR256Block::EncryptReverse1(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;
	pucBlock += m_iBlockSize1;
	for(int i=0; i<m_blockSize; i++,pucBlock--)
	{
		ucPrev ^= *pucBlock;
		//The First Internal Round
		(*pucBlock = ucPrev^*(pucXOR++)) += *(puc256++);
		//The Last m_iIRounds-1 Internal Rounds
		for(int j=1; j<m_iIRounds; j++)
			(*pucBlock ^= *(pucXOR++)) += *(puc256++);
	}
}

void CXOR256Block::DecryptDirect(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;
	unsigned char ucTemp;
	unsigned char const* pucXOR_1;
	unsigned char const* puc256_1;
	for(int i=0; i<m_blockSize; i++,pucBlock++)
	{
		pucXOR += m_iIRounds;
		puc256 += m_iIRounds;
		pucXOR_1 = pucXOR - 1;
		puc256_1 = puc256 - 1;
		ucTemp = *pucBlock;
		//The Last m_iIRounds-1 Internal Rounds
		for(int j=m_iIRounds1; j>0; j--,pucXOR_1--,puc256_1--)
		{
			if(*puc256_1 <= *pucBlock)
				*pucBlock -= *puc256_1;
			else
				(*pucBlock += ~(*puc256_1))++;
			*pucBlock ^= *pucXOR_1;
		}
		//The First Internal Round
		if(*puc256_1 <= *pucBlock)
			*pucBlock -= *puc256_1;
		else
			(*pucBlock += ~(*puc256_1))++;
		*pucBlock ^= ucPrev ^ *pucXOR_1;
		ucPrev = ucTemp;
	}
}

void CXOR256Block::DecryptReverse(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;
	unsigned char ucTemp;
	unsigned char const* pucXOR_1;
	unsigned char const* puc256_1;
	pucBlock += m_iBlockSize1;
	for(int i=0; i<m_blockSize; i++,pucBlock--)
	{
		pucXOR += m_iIRounds;
		puc256 += m_iIRounds;
		pucXOR_1 = pucXOR - 1;
		puc256_1 = puc256 - 1;
		ucTemp = *pucBlock;
		//The Last m_iIRounds-1 Internal Rounds
		for(int j=m_iIRounds1; j>0; j--,pucXOR_1--,puc256_1--)
		{
			if(*puc256_1 <= *pucBlock)
				*pucBlock -= *puc256_1;
			else
				(*pucBlock += ~(*puc256_1))++;
			*pucBlock ^= *pucXOR_1;
		}
		//The First Internal Round
		if(*puc256_1 <= *pucBlock)
			*pucBlock -= *puc256_1;
		else
			(*pucBlock += ~(*puc256_1))++;
		*pucBlock ^= ucPrev ^ *pucXOR_1;
		ucPrev = ucTemp;
	}
}

void CXOR256Block::DecryptDirect1(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;
	unsigned char const* pucXOR_1;
	unsigned char const* puc256_1;
	for(int i=0; i<m_blockSize; i++,pucBlock++)
	{
		pucXOR += m_iIRounds;
		puc256 += m_iIRounds;
		pucXOR_1 = pucXOR - 1;
		puc256_1 = puc256 - 1;
		//The Last m_iIRounds-1 Internal Rounds
		for(int j=m_iIRounds1; j>0; j--,pucXOR_1--,puc256_1--)
		{
			if(*puc256_1 <= *pucBlock)
				*pucBlock -= *puc256_1;
			else
				(*pucBlock += ~(*puc256_1))++;
			*pucBlock ^= *pucXOR_1;
		}
		//The First Internal Round
		if(*puc256_1 <= *pucBlock)
			*pucBlock -= *puc256_1;
		else
			(*pucBlock += ~(*puc256_1))++;
		*pucBlock ^= ucPrev ^ *pucXOR_1;
		ucPrev ^= *pucBlock;
	}
}

void CXOR256Block::DecryptReverse1(unsigned char* pucBlock, unsigned char const* pucXOR, unsigned char const* puc256)
{
	unsigned char ucPrev = 0;

⌨️ 快捷键说明

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