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

📄 aes.cpp

📁 cRijndael - Advanced Encryption Standard (AES)
💻 CPP
📖 第 1 页 / 共 2 页
字号:

// AES.cpp

#include "StdAfx.h"
#include "aes.h"

// S-Box Lookup Table
const char CAES::sm_S[256] = 
{
	99, 124, 119, 123, -14, 107, 111, -59, 48, 1, 103, 43, -2, -41, -85, 118, 
	-54, -126, -55, 125, -6, 89, 71, -16, -83, -44, -94, -81, -100, -92, 114, -64, 
	-73, -3, -109, 38, 54, 63, -9, -52, 52, -91, -27, -15, 113, -40, 49, 21, 
	4, -57, 35, -61, 24, -106, 5, -102, 7, 18, -128, -30, -21, 39, -78, 117, 
	9, -125, 44, 26, 27, 110, 90, -96, 82, 59, -42, -77, 41, -29, 47, -124, 
	83, -47, 0, -19, 32, -4, -79, 91, 106, -53, -66, 57, 74, 76, 88, -49, 
	-48, -17, -86, -5, 67, 77, 51, -123, 69, -7, 2, 127, 80, 60, -97, -88, 
	81, -93, 64, -113, -110, -99, 56, -11, -68, -74, -38, 33, 16, -1, -13, -46, 
	-51, 12, 19, -20, 95, -105, 68, 23, -60, -89, 126, 61, 100, 93, 25, 115, 
	96, -127, 79, -36, 34, 42, -112, -120, 70, -18, -72, 20, -34, 94, 11, -37, 
	-32, 50, 58, 10, 73, 6, 36, 92, -62, -45, -84, 98, -111, -107, -28, 121, 
	-25, -56, 55, 109, -115, -43, 78, -87, 108, 86, -12, -22, 101, 122, -82, 8, 
	-70, 120, 37, 46, 28, -90, -76, -58, -24, -35, 116, 31, 75, -67, -117, -118, 
	112, 62, -75, 102, 72, 3, -10, 14, 97, 53, 87, -71, -122, -63, 29, -98, 
	-31, -8, -104, 17, 105, -39, -114, -108, -101, 30, -121, -23, -50, 85, 40, -33, 
	-116, -95, -119, 13, -65, -26, 66, 104, 65, -103, 45, 15, -80, 84, -69, 22
};

// Inverse S-Box Lookup Table
const char CAES::sm_Si[256] = 
{
	82, 9, 106, -43, 48, 54, -91, 56, -65, 64, -93, -98, -127, -13, -41, -5, 
	124, -29, 57, -126, -101, 47, -1, -121, 52, -114, 67, 68, -60, -34, -23, -53, 
	84, 123, -108, 50, -90, -62, 35, 61, -18, 76, -107, 11, 66, -6, -61, 78, 
	8, 46, -95, 102, 40, -39, 36, -78, 118, 91, -94, 73, 109, -117, -47, 37, 
	114, -8, -10, 100, -122, 104, -104, 22, -44, -92, 92, -52, 93, 101, -74, -110, 
	108, 112, 72, 80, -3, -19, -71, -38, 94, 21, 70, 87, -89, -115, -99, -124, 
	-112, -40, -85, 0, -116, -68, -45, 10, -9, -28, 88, 5, -72, -77, 69, 6, 
	-48, 44, 30, -113, -54, 63, 15, 2, -63, -81, -67, 3, 1, 19, -118, 107, 
	58, -111, 17, 65, 79, 103, -36, -22, -105, -14, -49, -50, -16, -76, -26, 115, 
	-106, -84, 116, 34, -25, -83, 53, -123, -30, -7, 55, -24, 28, 117, -33, 110, 
	71, -15, 26, 113, 29, 41, -59, -119, 111, -73, 98, 14, -86, 24, -66, 27, 
	-4, 86, 62, 75, -58, -46, 121, 32, -102, -37, -64, -2, 120, -51, 90, -12, 
	31, -35, -88, 51, -120, 7, -57, 49, -79, 18, 16, 89, 39, -128, -20, 95, 
	96, 81, 127, -87, 25, -75, 74, 13, 45, -27, 122, -97, -109, -55, -100, -17, 
	-96, -32, 59, 77, -82, 42, -11, -80, -56, -21, -69, 60, -125, 83, -103, 97, 
	23, 43, 4, 126, -70, 119, -42, 38, -31, 105, 20, 99, 85, 33, 12, 125
};

const char CAES::sm_rcon[31] =
{
	0, 1, 2, 4, 8, 16, 32, 
	64, -128, 27, 54, 108, -40, 
	-85, 77, -102, 47, 94, -68, 
	99, -58, -105, 53, 106, -44, 
	-77, 125, -6, -17, -59, -111
};

// Shift table for SR and ISR
const int CAES::sm_shifts[3][4][2] =
{
	{ {0, 0}, {1, 3}, {2, 2}, {3, 1} },
	{ {0, 0}, {1, 5}, {2, 4}, {3, 3} },
	{ {0, 0}, {1, 7}, {3, 5}, {4, 4} }
};

// CONSTRUCTOR
CAES::CAES(void)
{
	m_bKeyInit = false;
	m_iKeyLength = 0;
	m_eLayer = NC;
	m_eState = STATE_IDLE;
	m_iRound = 0;

	for (int i = 0; i < BLOCK_SIZE; i++)
		m_byData[i/BC][i%4] = 0;
}

// DESTRUCTOR
CAES::~CAES(void)
{
}

// Get the current Data Elements
void CAES::GetData(int data[])
{
	int i;

	// Set Data
/*	for (i = 0; i < BC; i++)
	{
		data[i] = 0;
		data[i] |= (m_byData[i][0] << 32);
		data[i] |= (m_byData[i][1] << 16);
		data[i] |= (m_byData[i][2] << 8);
		data[i] |= m_byData[i][3];
	}*/
	for (i = 0; i < BLOCK_SIZE; i++)
		data[i] = (int)m_byData[i/BC][i%4];
}

// Set the current Data Elements
bool CAES::SetData(int data[])
{
	if (m_eState != STATE_IDLE && m_bKeyInit)
		return false;

	/*for (int i = 0; i < BC; i++)
	{
		m_byData[i][0] = (MBYTE)((data[i] >> 24)&0xff);
		m_byData[i][1] = (MBYTE)((data[i] >> 16)&0xff);
		m_byData[i][2] = (MBYTE)((data[i] >> 8)&0xff);
		m_byData[i][3] = (MBYTE)(data[i]&0xff);
	}*/
	for (int i = 0; i < BLOCK_SIZE; i++)
		m_byData[i/BC][i%4] = data[i];

	return true;
}

// Get the Encryption Round Keys
bool CAES::GetEncKey(int Ke[][BC])
{
	if (!m_bKeyInit)
		return false;

	for (int i = 0; i < m_iNumRounds + 1; i++)
		for (int j = 0; j < BC; j++)
			Ke[i][j] = m_iKe[i][j];

	return true;
}

// Get the Decryption Round Keys
bool CAES::GetDecKey(int Kd[][BC])
{
	if (!m_bKeyInit)
		return false;

	for (int i = 0; i < m_iNumRounds + 1; i++)
		for (int j = 0; j < BC; j++)
			Kd[i][j] = m_iKd[i][j];

	return true;
}

// Expand a user-supplied key material into a session key.
//  key        - The 128/192/256-bit user-key to use.
//  keylength  - 16, 24 or 32 bytes (defaults to 16 bytes)
int CAES::MakeKey (const int* key, int keylength /*= BLOCK_SIZE*/)
{
	// Sanity checks
	if (NULL == key)
		return STATE_ERR;
	if (16 != keylength && 24 != keylength && 32 != keylength)
		return STATE_ERR;

	// Set the keylength
	m_iKeyLength = keylength;

	// Set the number of rounds
	m_iNumRounds = (keylength == 16) ? 10 : ((keylength == 24) ? 12 : 14);

	// Number of Key Columns
	int KC = m_iKeyLength / 4;

	// Clear the Encryption and Decryption round keys
	int i, j;
	for (i = 0; i < m_iNumRounds; i++)
	{
		for (j = 0; j < BC; j++)
		{
			m_iKe[i][j] = 0;
			m_iKd[i][j] = 0;
		}
	}

	// Create and initialize the Key Work Array
	MWORD w[BC*(MAX_ROUNDS+1)];
	MWORD tmp;		// Temporary word
	int wi = 0;		// Word index

	while (wi < KC)
	{
		w[wi] = MakeWord((MBYTE)(key[4*wi]&0xff), (MBYTE)(key[4*wi+1]&0xff), 
			            (MBYTE)(key[4*wi+2]&0xff), (MBYTE)(key[4*wi+3]&0xff));
		wi++;
	}

	// Generate the remaining Key Words
	while (wi < (BC * (m_iNumRounds + 1)))
	{
		tmp = w[wi-1];
		if (0 == (wi%KC))
		{
			//tmp = SubWord(RotWord(tmp)) ^ (sm_rcon[wi/KC]<<24);
			tmp = RotWord(tmp);
			tmp = SubWord(tmp);
			tmp ^= (sm_rcon[wi/KC]&0xff) << 24;
		}
		else if ((KC > 6) && (wi%KC == 4))
			tmp = SubWord(tmp);
		w[wi] = w[wi-KC] ^ tmp;
		wi++;
	}

	// Set the encryption and decryption round keys
	for (wi = 0; wi < (BC * (m_iNumRounds + 1)); wi++)
	{
		m_iKe[wi/BC][wi%BC] = (int)w[wi];
		m_iKd[m_iNumRounds-(wi/BC)][wi%BC] = (int)w[wi];
	}

	// Apply the InvMixColumns on Decryption Round Keys for
	// rounds 1 through 1-m_iNumRounds
	for (wi = 1; wi < m_iNumRounds; wi++)
	{
		for (int j = 0; j < BC; j++)
			m_iKd[wi][j] = InvMixColumnWord(m_iKd[wi][j]);
	}

	m_bKeyInit = true;
	m_iRound = 0;

	return STATE_OK;
}

// Step through one layer
int CAES::Step ()
{
	// Make sure the key is initialized first
	if (!m_bKeyInit)
		return STATE_IDLE;

	if (STATE_ENCRYPT == m_eState)
	{
		// The zeroth round
		if (0 == m_iRound)
		{
			AddRoundKey(m_eState);
			m_eLayer = ARK;
			m_iRound++;
		}
		else // The middle rounds 
		if (1 <= m_iRound && m_iNumRounds > m_iRound)
		{
			// Operate depending on last rounds layer
			switch (m_eLayer)
			{
			case ARK:	// Do BS
				ByteSub();
				m_eLayer = BS;
				break;
			case BS:	// Do SR
				ShiftRow();
				m_eLayer = SR;
				break;
			case SR:	// Do MC
				MixColumn();
				m_eLayer = MC;
				break;
			case MC:	// Do ARK
				AddRoundKey(m_eState);
				m_eLayer = ARK;
				m_iRound++;
				break;
			default:
				return STATE_ERR;
			}
		}
		else // The last round
		{
			// Operate depending on last rounds layer
			switch (m_eLayer)
			{
			case ARK:	// Do BS
				ByteSub();
				m_eLayer = BS;
				break;
			case BS:	// Do SR
				ShiftRow();
				m_eLayer = SR;
				break;
			case SR:	// Do ARK
				AddRoundKey(m_eState);
				m_eLayer = ARK;
				m_iRound++;
				break;
			default:
				return STATE_ERR;
			}
		}
	}
	else 
	if (STATE_DECRYPT == m_eState)
	{
		// The zeroth round
		if (0 == m_iRound)
		{
			AddRoundKey(m_eState);
			m_eLayer = ARK;
			m_iRound++;
		}
		else // The middle rounds 
		if (1 <= m_iRound && m_iNumRounds > m_iRound)
		{
			// Operate depending on last rounds layer
			switch (m_eLayer)
			{
			case ARK:	// Do IBS
				InvByteSub();
				m_eLayer = IBS;
				break;
			case IBS:	// Do ISR
				InvShiftRow();
				m_eLayer = ISR;
				break;
			case ISR:	// Do IMC
				InvMixColumn();
				m_eLayer = IMC;
				break;
			case IMC:	// Do ARK
				AddRoundKey(m_eState);
				m_eLayer = ARK;
				m_iRound++;
				break;
			default:
				return STATE_ERR;
			}
		}
		else // The last round
		{
			switch (m_eLayer)
			{
			case ARK:	// Do IBS
				InvByteSub();
				m_eLayer = IBS;
				break;
			case IBS:	// Do ISR
				InvShiftRow();
				m_eLayer = ISR;
				break;
			case ISR:	// Do ARK
				AddRoundKey(m_eState);
				m_eLayer = ARK;
				m_iRound++;
				break;
			default:
				return STATE_ERR;
			}
		}
	}
	else
		return STATE_IDLE;

	if (m_iNumRounds < m_iRound) // We are done
	{
		m_iRound = 0;
		m_eState = STATE_IDLE;
		return STATE_DONE;

⌨️ 快捷键说明

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