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

📄 aesengine.cpp

📁 algorithm provide basic encryption/decryption using AES for with Secret Key.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
				cbcV_[k] ^= tempData[k];
			}

			// encrypt block
			processBlock( cbcV_, 0, tempEncData, 0 );

			// copy block into cbc
			for( j = i, k = 0; j < i + AES_BLOCK_SIZE; k++, j++ )
				cbcV_[k] = tempEncData[j-i];
		}
		else
		{
			// encrypt block
			processBlock( tempData, 0, tempEncData, 0 );
		}

		// copy block
		for( j = i; j < i + AES_BLOCK_SIZE; j++ )
			encryptedData[j] = tempEncData[j-i];

		encryptedDataLength += AES_BLOCK_SIZE;
	}
	
	delete[] dataWithPad;

	return encryptedDataLength;
}

//////////////////////////////////////////////////////////////////////////
// Public Interface - DECRYPT
// -----------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////


int AESEngine::decrypt( byte encryptedData[], int encryptedDataLength, byte*& decryptedData )
{

	int i,j,k;
	byte*	data  = new byte[ encryptedDataLength ];

	byte	tempData[AES_BLOCK_SIZE];
	byte	tempEncData[AES_BLOCK_SIZE];

	int dataLength = 0, lenDecrypted = 0;

	forEncryption_ = false;

	for( i = 0; i < encryptedDataLength; i += 16 ) 
	{
		for( j = i; j < i + AES_BLOCK_SIZE; j++ )
			tempEncData[j-i] = encryptedData[j];
		
		if ( mode_ == CBC_MODE ) 
		{
			processBlock( tempEncData, 0, tempData, 0 );
			/*
			* XOR the cbcV and the output
			*/
			for ( k = 0; k < AES_BLOCK_SIZE; k++)
			{
				tempData[k] ^= cbcV_[k];
			}
			// copy encData into CBCV for next usage
			memcpy( cbcV_, tempEncData, AES_BLOCK_SIZE );
		}
		else
		{
			processBlock( tempEncData, 0, tempData, 0 );
		}
		
		
		for( j = i; j < i + AES_BLOCK_SIZE; j++)
			data[j] = tempData[j-i];		

		dataLength += AES_BLOCK_SIZE;
	}
	
	int paddedBytes = data[dataLength - 1] & 0xff;

	if ( paddedBytes <= AES_BLOCK_SIZE )
	{
		for ( i = dataLength - 1 ; i > dataLength - paddedBytes - 1; --i )
		{
			if ( data[i] != paddedBytes )
			{
				paddedBytes = 17;
				break;
			}
		}
    }
	else
    {
        paddedBytes = 17;
    }

    if ( paddedBytes != 17 )
	{
	    lenDecrypted	= dataLength - paddedBytes;
		decryptedData	= data;				
    }
	else
	{
        decryptedData	= data;
		lenDecrypted	= 0;
    }	

	return lenDecrypted;
}



//--------------------------------------------------------------------------
// key generation
//--------------------------------------------------------------------------
//////////////////////////////////////////////////////////////////////////
//	Calculate the necessary round keys
//	The number of calculations depends on keyBits and BLOCK_BITS
//////////////////////////////////////////////////////////////////////////

void AESEngine::generateWorkingKey( byte key[] )
{
	int			i,j;
    int         KC;
    int         t, rconpointer = 0;
    int         keyBits = keyLength_ * 8;
    byte		tk[4][MAXKC];
    

    KC = keyBits / 32;

    if (keyBits >= BLOCK_BITS)
        ROUNDS = KC + 6;
    else
        ROUNDS = 4 + 6;
    
//
// copy the key into the processing area
//
    int index = 0;

    for ( i = 0; i < keyLength_; i++ )
        tk[i % 4][i / 4] = key[index++];

    t = 0;

//
// copy values into round key array
//
    for ( j = 0; ( j < KC ) && ( t < (ROUNDS+1 ) * 4 ); j++, t++ )
    {
        for ( i = 0; i < 4; i++ )
            workingKey_[t / 4][i] |= (int)( tk[i][j] & 0xff ) << ( ( t * 8 ) % 32 );
    }

//
// while not enough round key material calculated
// calculate new values
//
    while ( t < ( ROUNDS + 1 ) * 4 )
    {
        for ( i = 0; i < 4; i++ )
            tk[i][0] ^= S[tk[(i+1)%4][KC-1] & 0xff];

        tk[0][0] ^= rcon[rconpointer++];

        if ( KC <= 6 )
        {
            for ( j = 1; j < KC; j++ )
            {
                for ( i = 0; i < 4; i++ )
                    tk[i][j] ^= tk[i][j-1];
            }
        }
        else
        {
            for ( j = 1; j < 4; j++ )
            {
                for ( i = 0; i < 4; i++ )
                    tk[i][j] ^= tk[i][j-1];
            }

            for ( i = 0; i < 4; i++ )
                tk[i][4] ^= S[tk[i][3] & 0xff];
            
			for ( j = 5; j < KC; j++ )
            {
                for ( i = 0; i < 4; i++ )
                    tk[i][j] ^= tk[i][j-1];
            }
        }

//
// copy values into round key array
//

        for ( j = 0; ( j < KC ) && ( t < ( ROUNDS+1 ) * ( 32 / 8 ) ); j++, t++ )
        {
            for ( i = 0; i < 4; i++ )
                workingKey_[t / (32/8)][i] |= (int)(tk[i][j] & 0xff) << ((t * 8) % (32));
        }
    }


}



int AESEngine::processBlock( byte in[],	int inOff, byte out[], int outOff)
{
	if ( forEncryption_ )
	{
		unpackBlock( in, inOff );
		encryptBlock( workingKey_ );
		packBlock( out, outOff );
	}
	else
	{
		unpackBlock( in, inOff );
		decryptBlock( workingKey_ );
		packBlock( out, outOff );
	}
	
	return BLOCK_SIZE;
}


void AESEngine::unpackBlock( byte bytes[], int index )
{
	A0 = ( bytes[index++] & 0xff );
	A1 = ( bytes[index++] & 0xff );
	A2 = ( bytes[index++] & 0xff );
	A3 = ( bytes[index++] & 0xff );
	
	for ( int j = 8; j != 32; j += 8 )
	{
		A0 |= ( bytes[index++] & 0xff ) << j;
		A1 |= ( bytes[index++] & 0xff ) << j;
		A2 |= ( bytes[index++] & 0xff ) << j;
		A3 |= ( bytes[index++] & 0xff ) << j;
	}
}

void AESEngine::packBlock( byte bytes[], int index )
{	
	for ( int j = 0; j != 32; j += 8 )
	{
		bytes[index++] = (byte)( A0 >> j );
		bytes[index++] = (byte)( A1 >> j );
		bytes[index++] = (byte)( A2 >> j );
		bytes[index++] = (byte)( A3 >> j );
	}
}

void AESEngine::encryptBlock( int rk[][4] )
{
	int r;

//
// begin with a key addition
//
	KeyAddition( rk[0] );
	
//
// ROUNDS-1 ordinary rounds
//
	for ( r = 1; r < ROUNDS; r++ )

⌨️ 快捷键说明

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