📄 rijndael.cpp
字号:
void CRijndael::EncryptBlock(char const* in, char* result)
{
if(false==m_bInit)
throw runtime_error(string(sm_szErrorMsg1));
if(DEFAULT_BLOCK_SIZE == m_blockSize)
{
DefEncryptBlock(in, result);
return;
}
int BC = m_blockSize / 4;
int SC = (BC == 4) ? 0 : (BC == 6 ? 1 : 2);
int s1 = sm_shifts[SC][1][0];
int s2 = sm_shifts[SC][2][0];
int s3 = sm_shifts[SC][3][0];
int i;
int tt;
int* pi = t;
for(i=0; i<BC; i++)
{
*pi = ((unsigned char)*(in++) << 24);
*pi |= ((unsigned char)*(in++) << 16);
*pi |= ((unsigned char)*(in++) << 8);
(*(pi++) |= (unsigned char)*(in++)) ^= m_Ke[0][i];
}
//Apply Round Transforms
for(int r=1; r<m_iROUNDS; r++)
{
for(i=0; i<BC; i++)
a[i] = (sm_T1[(t[i] >> 24) & 0xFF] ^
sm_T2[(t[(i + s1) % BC] >> 16) & 0xFF] ^
sm_T3[(t[(i + s2) % BC] >> 8) & 0xFF] ^
sm_T4[ t[(i + s3) % BC] & 0xFF] ) ^ m_Ke[r][i];
memcpy(t, a, 4*BC);
}
int j;
//Last Round is Special
for(i=0,j=0; i<BC; i++)
{
tt = m_Ke[m_iROUNDS][i];
result[j++] = sm_S[(t[i] >> 24) & 0xFF] ^ (tt >> 24);
result[j++] = sm_S[(t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16);
result[j++] = sm_S[(t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8);
result[j++] = sm_S[ t[(i + s3) % BC] & 0xFF] ^ tt;
}
}
//Decrypt exactly one block of ciphertext.
// in - The ciphertext.
// result - The plaintext generated from a ciphertext using the session key.
void CRijndael::DecryptBlock(char const* in, char* result)
{
if(false==m_bInit)
throw runtime_error(string(sm_szErrorMsg1));
if(DEFAULT_BLOCK_SIZE == m_blockSize)
{
DefDecryptBlock(in, result);
return;
}
int BC = m_blockSize / 4;
int SC = BC == 4 ? 0 : (BC == 6 ? 1 : 2);
int s1 = sm_shifts[SC][1][1];
int s2 = sm_shifts[SC][2][1];
int s3 = sm_shifts[SC][3][1];
int i;
int tt;
int* pi = t;
for(i=0; i<BC; i++)
{
*pi = ((unsigned char)*(in++) << 24);
*pi |= ((unsigned char)*(in++) << 16);
*pi |= ((unsigned char)*(in++) << 8);
(*(pi++) |= (unsigned char)*(in++)) ^= m_Kd[0][i];
}
//Apply Round Transforms
for(int r=1; r<m_iROUNDS; r++)
{
for(i=0; i<BC; i++)
a[i] = (sm_T5[(t[i] >> 24) & 0xFF] ^
sm_T6[(t[(i + s1) % BC] >> 16) & 0xFF] ^
sm_T7[(t[(i + s2) % BC] >> 8) & 0xFF] ^
sm_T8[ t[(i + s3) % BC] & 0xFF]) ^ m_Kd[r][i];
memcpy(t, a, 4*BC);
}
int j;
//Last Round is Special
for(i=0,j=0; i<BC; i++)
{
tt = m_Kd[m_iROUNDS][i];
result[j++] = sm_Si[(t[i] >> 24) & 0xFF] ^ (tt >> 24);
result[j++] = sm_Si[(t[(i + s1) % BC] >> 16) & 0xFF] ^ (tt >> 16);
result[j++] = sm_Si[(t[(i + s2) % BC] >> 8) & 0xFF] ^ (tt >> 8);
result[j++] = sm_Si[ t[(i + s3) % BC] & 0xFF] ^ tt;
}
}
void CRijndael::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));
int i;
char const* pin;
char* presult;
if(CBC == m_iMode) //CBC mode, using the Chain
{
for(i=0,pin=in,presult=result; i<n/m_blockSize; i++)
{
Xor(m_chain, pin);
EncryptBlock(m_chain, presult);
memcpy(m_chain, presult, m_blockSize);
pin += m_blockSize;
presult += m_blockSize;
}
}
else if(CFB == m_iMode) //CFB mode, using the Chain
{
for(i=0,pin=in,presult=result; i<n/m_blockSize; i++)
{
EncryptBlock(m_chain, presult);
Xor(presult, pin);
memcpy(m_chain, presult, m_blockSize);
pin += m_blockSize;
presult += m_blockSize;
}
}
else //ECB mode, not using the Chain
{
for(i=0,pin=in,presult=result; i<n/m_blockSize; i++)
{
EncryptBlock(pin, presult);
pin += m_blockSize;
presult += m_blockSize;
}
}
}
void CRijndael::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));
int i;
char const* pin;
char* presult;
if(CBC == m_iMode) //CBC mode, using the Chain
{
for(i=0,pin=in,presult=result; i<n/m_blockSize; i++)
{
DecryptBlock(pin, presult);
Xor(presult, m_chain);
memcpy(m_chain, pin, m_blockSize);
pin += m_blockSize;
presult += m_blockSize;
}
}
else if(CFB == m_iMode) //CFB mode, using the Chain, not using Decrypt()
{
for(i=0,pin=in,presult=result; i<n/m_blockSize; i++)
{
EncryptBlock(m_chain, presult);
//memcpy(presult, pin, m_blockSize);
Xor(presult, pin);
memcpy(m_chain, pin, m_blockSize);
pin += m_blockSize;
presult += m_blockSize;
}
}
else //ECB mode, not using the Chain
{
for(i=0,pin=in,presult=result; i<n/m_blockSize; i++)
{
DecryptBlock(pin, presult);
pin += m_blockSize;
presult += m_blockSize;
}
}
}
void CRijndael::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};
char szBuffIn[DATA_LEN+1] = {0};
char szBuffOut[DATA_LEN+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 CRijndael::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};
char szBuffIn[DATA_LEN+1] = {0};
char szBuffOut[DATA_LEN+1] = {0};
CDoubleBuffering oDoubleBuffering(in, szLargeBuff, BUFF_LEN, DATA_LEN);
int iRead;
while((iRead=oDoubleBuffering.GetData(szBuffIn)) > 0)
{
//Decrypting
Decrypt(szBuffIn, szBuffOut, iRead);
out.write(szBuffOut, iRead);
}
in.close();
out.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -