📄 xor256block.cpp
字号:
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;
//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::EncryptBlock(unsigned char* pucBlock)
{
unsigned char const* pucXOR = m_pucXOR;
unsigned char const* puc256 = m_puc256;
bool bDir = true;
for(int i=0; i<m_iORounds1; i++,pucXOR+=m_iProd,puc256+=m_iProd)
{
if(true == bDir)
EncryptDirect(pucBlock, pucXOR, puc256);
else
EncryptReverse(pucBlock, pucXOR, puc256);
bDir = !bDir;
}
if(true == bDir)
EncryptDirect1(pucBlock, pucXOR, puc256);
else
EncryptReverse1(pucBlock, pucXOR, puc256);
}
void CXOR256Block::DecryptBlock(unsigned char* pucBlock)
{
unsigned char const* pucXOR = m_pucXOR + m_iDelta;
unsigned char const* puc256 = m_puc256 + m_iDelta;
bool bDir;
if(1 == m_iORounds%2)
{
DecryptDirect1(pucBlock, pucXOR, puc256);
bDir = false;
}
else
{
DecryptReverse1(pucBlock, pucXOR, puc256);
bDir = true;
}
pucXOR-=m_iProd;
puc256-=m_iProd;
for(int i=0; i<m_iORounds1; i++,pucXOR-=m_iProd,puc256-=m_iProd)
{
if(true == bDir)
DecryptDirect(pucBlock, pucXOR, puc256);
else
DecryptReverse(pucBlock, pucXOR, puc256);
bDir = !bDir;
}
}
void CXOR256Block::Encrypt(char const* in, char* result, size_t n)
{
if(false==m_bInit)
throw runtime_error(string(sm_szErrorMsg1));
//n should be > 0 and multiple of m_blockSize
if(n<1 || n%m_blockSize!=0)
throw runtime_error(string(sm_szErrorMsg6));
unsigned int ui;
unsigned char const* pucCurrentIn;
unsigned char* pucCurrentOut;
if(m_iMode == CBC) //CBC mode, using the Chain
{
for(ui=0,pucCurrentIn=reinterpret_cast<unsigned char const*>(in),
pucCurrentOut=reinterpret_cast<unsigned char*>(result); ui<n/m_blockSize; ui++)
{
memcpy(pucCurrentOut, pucCurrentIn, m_blockSize);
Xor(reinterpret_cast<char*>(pucCurrentOut), m_apchain.get());
EncryptBlock(pucCurrentOut);
memcpy(m_apchain.get(), pucCurrentOut, m_blockSize);
pucCurrentIn += m_blockSize;
pucCurrentOut += m_blockSize;
}
}
else if(m_iMode == CFB) //CFB mode, using the Chain
{
for(ui=0,pucCurrentIn=reinterpret_cast<unsigned char const*>(in),
pucCurrentOut=reinterpret_cast<unsigned char*>(result); ui<n/m_blockSize; ui++)
{
EncryptBlock(reinterpret_cast<unsigned char*>(m_apchain.get()));
memcpy(pucCurrentOut, pucCurrentIn, m_blockSize);
Xor(reinterpret_cast<char*>(pucCurrentOut), m_apchain.get());
memcpy(m_apchain.get(), pucCurrentOut, m_blockSize);
pucCurrentIn += m_blockSize;
pucCurrentOut += m_blockSize;
}
}
else //ECB mode, not using the Chain
{
for(ui=0,pucCurrentIn=reinterpret_cast<unsigned char const*>(in),
pucCurrentOut=reinterpret_cast<unsigned char*>(result); ui<n/m_blockSize; ui++)
{
memcpy(pucCurrentOut, pucCurrentIn, m_blockSize);
EncryptBlock(pucCurrentOut);
pucCurrentIn += m_blockSize;
pucCurrentOut += m_blockSize;
}
}
}
void CXOR256Block::Decrypt(char const* in, char* result, size_t n)
{
if(false==m_bInit)
throw runtime_error(string(sm_szErrorMsg1));
//n should be > 0 and multiple of m_blockSize
if(n<1 || n%m_blockSize!=0)
throw runtime_error(string(sm_szErrorMsg6));
unsigned int ui;
unsigned char const* pucCurrentIn;
unsigned char* pucCurrentOut;
if(m_iMode == CBC) //CBC mode, using the Chain
{
for(ui=0,pucCurrentIn=reinterpret_cast<unsigned char const*>(in),
pucCurrentOut=reinterpret_cast<unsigned char*>(result); ui<n/m_blockSize; ui++)
{
memcpy(pucCurrentOut, pucCurrentIn, m_blockSize);
memcpy(m_apTemp.get(), pucCurrentOut, m_blockSize);
DecryptBlock(pucCurrentOut);
Xor(reinterpret_cast<char*>(pucCurrentOut), m_apchain.get());
memcpy(m_apchain.get(), m_apTemp.get(), m_blockSize);
pucCurrentIn += m_blockSize;
pucCurrentOut += m_blockSize;
}
}
else if(m_iMode == CFB) //CFB mode, using the Chain, not using Decrypt()
{
for(ui=0,pucCurrentIn=reinterpret_cast<unsigned char const*>(in),
pucCurrentOut=reinterpret_cast<unsigned char*>(result); ui<n/m_blockSize; ui++)
{
EncryptBlock(reinterpret_cast<unsigned char*>(m_apchain.get()));
memcpy(pucCurrentOut, pucCurrentIn, m_blockSize);
memcpy(m_apTemp.get(), pucCurrentOut, m_blockSize);
Xor(reinterpret_cast<char*>(pucCurrentOut), m_apchain.get());
memcpy(m_apchain.get(), m_apTemp.get(), m_blockSize);
pucCurrentIn += m_blockSize;
pucCurrentOut += m_blockSize;
}
}
else //ECB mode, not using the Chain
{
for(ui=0,pucCurrentIn=reinterpret_cast<unsigned char const*>(in),
pucCurrentOut=reinterpret_cast<unsigned char*>(result); ui<n/m_blockSize; ui++)
{
memcpy(pucCurrentOut, pucCurrentIn, m_blockSize);
DecryptBlock(pucCurrentOut);
pucCurrentIn += m_blockSize;
pucCurrentOut += m_blockSize;
}
}
}
void CXOR256Block::EncryptFile(string const& rostrFileIn, string const& rostrFileOut)
{
if(false==m_bInit)
throw runtime_error(string(sm_szErrorMsg1));
//Check if the same file for input and output
if(rostrFileIn == rostrFileOut)
{
ostrstream ostr;
ostr << sm_szErrorMsg8 << rostrFileIn << "!" << ends;
string ostrMsg = ostr.str();
ostr.freeze(false);
throw runtime_error(ostrMsg);
}
//Open Input File
ifstream in(rostrFileIn.c_str(), ios::binary);
if(!in)
{
ostrstream ostr;
ostr << sm_szErrorMsg7 << rostrFileIn << "!" << ends;
string ostrMsg = ostr.str();
ostr.freeze(false);
throw runtime_error(ostrMsg);
}
//Open Output File
ofstream out(rostrFileOut.c_str(), ios::binary);
if(!out)
{
ostrstream ostr;
ostr << sm_szErrorMsg7 << rostrFileOut << "!" << ends;
string ostrMsg = ostr.str();
ostr.freeze(false);
throw runtime_error(ostrMsg);
}
//Computing the signature
char acSig[33] = {0};
Signature(acSig);
//Writing the Signature
out.write(acSig, 32);
//Resetting the chain
ResetChain();
//Reading from file
char szLargeBuff[BUFF_LEN+1] = {0};
int DATA_LEN = (BUFF_LEN/m_blockSize/2)*m_blockSize;
char szBuffIn[BUFF_LEN/2+1] = {0};
char szBuffOut[BUFF_LEN/2+1] = {0};
CDoubleBuffering oDoubleBuffering(in, szLargeBuff, BUFF_LEN, DATA_LEN);
int iRead;
while((iRead=oDoubleBuffering.GetData(szBuffIn)) > 0)
{
if(iRead < DATA_LEN)
iRead = Pad(szBuffIn, iRead);
//Encrypting
Encrypt(szBuffIn, szBuffOut, iRead);
out.write(szBuffOut, iRead);
}
in.close();
out.close();
}
void CXOR256Block::DecryptFile(string const& rostrFileIn, string const& rostrFileOut)
{
if(false==m_bInit)
throw runtime_error(string(sm_szErrorMsg1));
//Check if the same file for input and output
if(rostrFileIn == rostrFileOut)
{
ostrstream ostr;
ostr << sm_szErrorMsg8 << rostrFileIn << "!" << ends;
string ostrMsg = ostr.str();
ostr.freeze(false);
throw runtime_error(ostrMsg);
}
//Open Input File
ifstream in(rostrFileIn.c_str(), ios::binary);
if(!in)
{
ostrstream ostr;
ostr << sm_szErrorMsg7 << rostrFileIn << "!" << ends;
string ostrMsg = ostr.str();
ostr.freeze(false);
throw runtime_error(ostrMsg);
}
//Open Output File
ofstream out(rostrFileOut.c_str(), ios::binary);
if(!out)
{
ostrstream ostr;
ostr << sm_szErrorMsg7 << rostrFileOut << "!" << ends;
string ostrMsg = ostr.str();
ostr.freeze(false);
throw runtime_error(ostrMsg);
}
//Computing the signature
char acSig[33] = {0};
Signature(acSig);
char acSig1[33] = {0};
//Reading the Signature
in.read(acSig1, 32);
//Compare the signatures
if(memcmp(acSig1, acSig, 32) != 0)
{
ostrstream ostr;
ostr << sm_szErrorMsg9 << rostrFileIn << sm_szErrorMsg10 << ends;
string ostrMsg = ostr.str();
ostr.freeze(false);
throw runtime_error(ostrMsg);
}
//Resetting the chain
ResetChain();
//Reading from file
char szLargeBuff[BUFF_LEN+1] = {0};
int DATA_LEN = (BUFF_LEN/m_blockSize/2)*m_blockSize;
char szBuffIn[BUFF_LEN/2+1] = {0};
char szBuffOut[BUFF_LEN/2+1] = {0};
CDoubleBuffering oDoubleBuffering(in, szLargeBuff, BUFF_LEN, DATA_LEN);
int iRead;
while((iRead=oDoubleBuffering.GetData(szBuffIn)) > 0)
{
//Encrypting
Decrypt(szBuffIn, szBuffOut, iRead);
out.write(szBuffOut, iRead);
}
in.close();
out.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -