📄 aescryptosystem.cpp
字号:
w[0] = st_cSBox[w[0]];
w[1] = st_cSBox[w[1]];
w[2] = st_cSBox[w[2]];
w[3] = st_cSBox[w[3]];
}
void AESCryptoSystem::Encode(state &currState)
{
//cout << "\nRound ----------> 0";
//PrintToScreen(currState);
// Round 0
AddRoundKey(currState, m_pExpandedKey);
//PrintToScreenKey(m_pExpandedKey);
// Round 1 -> Nr - 1
for (int round = 1; round < m_iNr; round++)
{
// cout << "\nRound---------->" << round ;
//PrintToScreen(currState);
SubBytes(currState);
//PrintToScreen(currState);
ShiftRows(currState);
//PrintToScreen(currState);
MixColumns(currState);
//PrintToScreen(currState);
AddRoundKey(currState, m_pExpandedKey + round * m_ciNb);
//PrintToScreenKey(m_pExpandedKey + round * m_ciNb);
}
// Final round (Nr)
//cout << "\nfinal Round---------->";
SubBytes(currState);
//PrintToScreen(currState);
ShiftRows(currState);
//PrintToScreen(currState);
AddRoundKey(currState, m_pExpandedKey + m_iNr*m_ciNb);
//PrintToScreen(currState);
}
void AESCryptoSystem::AddRoundKey(AESCryptoSystem::state &s, const AESCryptoSystem::word *key)
{
for (int i = 0; i < 4; i ++)
for (int j = 0; j < 4; j ++)
s[i][j] = s[i][j] ^ key[j][i];
}
void AESCryptoSystem::SubBytes(AESCryptoSystem::state &s)
{
for (int i = 0; i < 4; i ++)
for (int j = 0; j < 4; j ++)
s[i][j] = st_cSBox[s[i][j]];
}
void AESCryptoSystem::ShiftRows(state& s)
{
byte temp = s[1][0];
s[1][0] = s[1][1]; s[1][1] = s[1][2]; s[1][2] = s[1][3]; s[1][3] = temp;
temp = s[2][0];
s[2][0] = s[2][2]; s[2][2] = temp; temp = s[2][1]; s[2][1] = s[2][3]; s[2][3] = temp;
temp = s[3][3];
s[3][3] = s[3][2]; s[3][2] = s[3][1] ; s[3][1] = s[3][0]; s[3][0] = temp;
}
void AESCryptoSystem::MixColumns(AESCryptoSystem::state &s)
{
state temp;
memcpy(&temp, &s, STATE_SIZE);
for (int i = 0; i < 4; i++)
{
//s[0][i] = gmul(temp[0][i], 0x02) ^ gmul(temp[3][i],0x01) ^ gmul(temp[2][i],0x01) ^gmul(temp[1][i], 0x03); /* 2 * a0 + a3 + a2 + 3 * a1 */
//s[1][i] = gmul(temp[1][i], 0x02) ^ gmul(temp[0][i],0x01) ^ gmul(temp[3][i],0x01) ^gmul(temp[2][i], 0x03); /* 2 * a1 + a0 + a3 + 3 * a2 */
//s[2][i] = gmul(temp[2][i], 0x02) ^ gmul(temp[1][i],0x01) ^ gmul(temp[0][i],0x01) ^gmul(temp[3][i], 0x03); /* 2 * a2 + a1 + a0 + 3 * a3 */
//s[3][i] = gmul(temp[3][i], 0x02) ^ gmul(temp[2][i],0x01) ^ gmul(temp[1][i],0x01) ^gmul(temp[0][i], 0x03); /* 2 * a3 + a2 + a1 + 3 * a0 */
s[0][i] = st_cXtime2[temp[0][i]] ^ temp[3][i] ^ temp[2][i] ^ st_cXtime3[temp[1][i]];
s[1][i] = st_cXtime2[temp[1][i]] ^ temp[0][i] ^ temp[3][i] ^ st_cXtime3[temp[2][i]];
s[2][i] = st_cXtime2[temp[2][i]] ^ temp[1][i] ^ temp[0][i] ^ st_cXtime3[temp[3][i]];
s[3][i] = st_cXtime2[temp[3][i]] ^ temp[2][i] ^ temp[1][i] ^ st_cXtime3[temp[0][i]];
}
}
void AESCryptoSystem::Decode(state& currState)
{
//cout << "\nRound ----------> 0";
//PrintToScreen(currState);
// Round 0
AddRoundKey(currState, m_pExpandedKey + m_iNr * m_ciNb);
//PrintToScreenKey(m_pExpandedKey);
// Round 1 -> Nr - 1
for (int round = m_iNr - 1; round > 0; round--)
{
//cout << "\nRound---------->" << round ;
//PrintToScreen(currState);
InvShiftRows(currState);
//PrintToScreen(currState);
InvSubBytes(currState);
//PrintToScreen(currState);
AddRoundKey(currState, m_pExpandedKey + round * m_ciNb);
//PrintToScreen(currState);
InvMixColumns(currState);
//PrintToScreenKey(m_pExpandedKey + round * m_ciNb);
}
// Final round (Nr)
//cout << "\nfinal Round---------->";
InvShiftRows(currState);
//PrintToScreen(currState);
InvSubBytes(currState);
//PrintToScreen(currState);
AddRoundKey(currState, m_pExpandedKey);
//PrintToScreen(currState);
}
void AESCryptoSystem::InvSubBytes(AESCryptoSystem::state &s)
{
for (int i = 0; i < 4; i ++)
for (int j = 0; j < 4; j ++)
s[i][j] = st_cSBoxInv[s[i][j]];
}
void AESCryptoSystem::InvShiftRows(state& s)
{
byte temp = s[1][3];
s[1][3] = s[1][2]; s[1][2] = s[1][1]; s[1][1] = s[1][0]; s[1][0] = temp;
temp = s[2][2];
s[2][2] = s[2][0]; s[2][0] = temp; temp = s[2][3]; s[2][3] = s[2][1]; s[2][1] = temp;
temp = s[3][0];
s[3][0] = s[3][1]; s[3][1] = s[3][2] ; s[3][2] = s[3][3]; s[3][3] = temp;
}
void AESCryptoSystem::InvMixColumns(AESCryptoSystem::state &s)
{
state temp;
memcpy(&temp, &s, STATE_SIZE);
for (int i = 0; i < 4; i++)
{
//s[0][i] = gmul(temp[0][i], 0x0e) ^ gmul(temp[3][i],0x09) ^ gmul(temp[2][i],0x0d) ^gmul(temp[1][i], 0x0b); /* 14a0 + 9a3 + 13a2 + 11a1 */
//s[1][i] = gmul(temp[1][i], 0x0e) ^ gmul(temp[0][i],0x09) ^ gmul(temp[3][i],0x0d) ^gmul(temp[2][i], 0x0b); /* 14a1 + 9a0 + 13a3 + 11a2 */
//s[2][i] = gmul(temp[2][i], 0x0e) ^ gmul(temp[1][i],0x09) ^ gmul(temp[0][i],0x0d) ^gmul(temp[3][i], 0x0b); /* 14a2 + 9a1 + 13a0 + 11a */
//s[3][i] = gmul(temp[3][i], 0x0e) ^ gmul(temp[2][i],0x09) ^ gmul(temp[1][i],0x0d) ^gmul(temp[0][i], 0x0b); /* 14a3 + 9a2 + 13a1 + 11a0 */
s[0][i] = st_cXtimeE[temp[0][i]] ^ st_cXtime9[temp[3][i]] ^ st_cXtimeD[temp[2][i]] ^ st_cXtimeB[temp[1][i]]; /* 14a0 + 9a3 + 13a2 + 11a1 */
s[1][i] = st_cXtimeE[temp[1][i]] ^ st_cXtime9[temp[0][i]] ^ st_cXtimeD[temp[3][i]] ^ st_cXtimeB[temp[2][i]]; /* 14a1 + 9a0 + 13a3 + 11a2 */
s[2][i] = st_cXtimeE[temp[2][i]] ^ st_cXtime9[temp[1][i]] ^ st_cXtimeD[temp[0][i]] ^ st_cXtimeB[temp[3][i]]; /* 14a2 + 9a1 + 13a0 + 11a */
s[3][i] = st_cXtimeE[temp[3][i]] ^ st_cXtime9[temp[2][i]] ^ st_cXtimeD[temp[1][i]] ^ st_cXtimeB[temp[0][i]]; /* 14a3 + 9a2 + 13a1 + 11a0 */
}
}
//**************************************************************
// Interface
//**************************************************************
void AESCryptoSystem::SetKeyFile(const std::string &s)
{
struct stat st;
if (stat(s.c_str(), &st) <= -1)
{
perror(s.c_str());
exit(1);
}
if (st.st_size > 32 )
{
cout << "\nFile to big to be a key";
exit(1);
}
DWORD size;
// SetText(s, size, &m_pKey, NULL);
SetFile(s, size, &m_pKey, NULL, false);
if (size == 16)
{
m_iNr = 10;
m_iNk = 4;
return;
}
if (size == 24)
{
m_iNr = 12;
m_iNk = 6;
return;
}
if (size == 32)
{
m_iNr = 14;
m_iNk = 8;
return;
}
cout << "\nInvalid key length";
exit(1);
}
void AESCryptoSystem::SetPlainTextFile(const std::string &s)
{
// SetText(s, m_iPlainTextSize, &m_pPlainText, &m_pCryptedText);
SetFile(s, m_iPlainTextSize, &m_pPlainText, &m_pCryptedText, false);
}
void AESCryptoSystem::SaveCryptedTextToFile(const std::string &s)
{
//SaveText(s, m_iPlainTextSize, m_pCryptedText);
SaveFile(s, m_iPlainTextSize, m_pCryptedText, true);
}
void AESCryptoSystem::SetCryptoTextFile(const std::string& s)
{
//SetText(s, m_iCryptoTextSize, &m_pCryptoText, &m_pDecryptedText);
SetFile(s, m_iCryptoTextSize, &m_pCryptoText, &m_pDecryptedText, true);
}
void AESCryptoSystem::SaveDecryptedTextToFile(const std::string &s)
{
//SaveText(s, m_iCryptoTextSize, m_pDecryptedText);
SaveFile(s, m_iCryptoTextSize, m_pDecryptedText, false);
}
void AESCryptoSystem::Encrypt()
{
DWORD t1 = GetTickCount();
CalculateRoundKey();
const int stateSize = STATE_SIZE;
state currState;
int plainIdx = 0;
int cryptIdx = 0;
//PrintToScreen(m_pPlainText, m_iPlainTextSize);
while (plainIdx < m_iPlainTextSize)
{
for (int j = 0; j < stateSize; j++, plainIdx++)
currState[j % 4][(int)floor((double)j / 4.0)] = m_pPlainText[plainIdx];
//PrintToScreen(currState);
Encode(currState);
//PrintToScreen(currState);
for (int j = 0; j < stateSize; j++, cryptIdx++)
m_pCryptedText[cryptIdx] = currState[j % 4][(int)floor((double)j / 4.0)];
}
DWORD t2 = GetTickCount();
cout << "\nCripatarea a durat: " << t2 -t1 << " ms";
__int64 start, stop;
rd_clock(&start);
Encode(currState);
rd_clock(&stop);
cout << "\nCiclyes per byte: " << (stop - start) / 16;
//PrintToScreen(m_pCryptedText, m_iPlainTextSize);
}
void AESCryptoSystem::Decrypt()
{
DWORD t1 = GetTickCount();
CalculateRoundKey();
const int stateSize = STATE_SIZE;
state currState;
int cryptIdx = 0;
int decryptIdx = 0;
//cout << "---------------------\n";
while (cryptIdx < m_iCryptoTextSize)
{
for (int j = 0; j < stateSize; j++, cryptIdx++)
currState[j % 4][(int)floor((double)j / 4.0)] = m_pCryptoText[cryptIdx];
//PrintToScreen(currState);
Decode(currState);
//PrintToScreen(currState);
for (int j = 0; j < stateSize; j++, decryptIdx++)
m_pDecryptedText[decryptIdx] = currState[j % 4][(int)floor((double)j / 4.0)];
}
DWORD t2 = GetTickCount();
cout << "\nCripatarea a durat: " << t2 -t1 << " ms";
__int64 start, stop;
rd_clock(&start);
Decode(currState);
rd_clock(&stop);
cout << "\nCiclyes per byte: " << (stop - start) / 16;
//PrintToScreen(m_pDecryptedText, m_iCryptoTextSize);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -