📄 aesengine.h
字号:
// AESEngine.h: interface for the AESEngine class.
//
//////////////////////////////////////////////////////////////////////
//=============================================================================
// This file contains the implimentation of class AES Engine class.
//
// @Copy right: C-Sam Incorporated, Chicago (c) 2002 All Rights Reserved
//=============================================================================
//
//-------------------------------------
// Version History
//-------------------------------------
// Version : 1.0
// Author : arun.kumar
// Date : 12.12.2005
// Comment : Initial release
// ------------------------------------
// Version : 1.1
// Author : arun.kumar
// Date : 26.02.2008
// Comment : Support for CBC and ECB mode being implemented
//=============================================================================
/**
* an implementation of the AES (Rijndael)), from FIPS-197.
* <p>
* For further details see: <a href="http://csrc.nist.gov/encryption/aes/">http://csrc.nist.gov/encryption/aes/</a>.
*
* This implementation is based on optimizations from Dr. Brian Gladman's paper and C code at
* <a href="http://fp.gladman.plus.com/cryptography_technology/rijndael/">http://fp.gladman.plus.com/cryptography_technology/rijndael/</a>
*
* There are three levels of tradeoff of speed vs memory
* Because java has no preprocessor), they are written as three separate classes from which to choose
*
* The fastest uses 8Kbytes of static tables to precompute round calculations), 4 256 word tables for encryption
* and 4 for decryption.
*
* The middle performance version uses only one 256 word table for each), for a total of 2Kbytes),
* adding 12 rotate operations per round to compute the values contained in the other tables from
* the contents of the first
*
* The slowest version uses no static tables at all and computes the values in each round
* <p>
* This file contains the fast version with 8Kbytes of static tables for round precomputation
*
*/
#include <STDIO.H>
#ifndef AFX_AESENGINE_H__
#define AFX_AESENGINE_H__
typedef unsigned char byte;
typedef unsigned int uint;
#define MAXROUNDS 14
#define MAXKC 64
#define BLOCK_SIZE 64
#define BLOCK_BITS 128
#define AES_BLOCK_SIZE 16
#define CBC_MODE 151
#define ECB_MODE 152
class AESEngine
{
public:
AESEngine();
~AESEngine();
int encrypt( byte plainData[], int plainDataLength, byte*& encryptedData );
int decrypt( byte encryptedData[], int encryptedDataLength, byte*& decryptedData );
void init( byte key[], int keyLength, int mode );
private:
//--------------------------------------------------------------------------
// cipher params
//--------------------------------------------------------------------------
static const byte Logtable[];
static const byte Alogtable[];
static const byte S[];
static const byte Si[];
static const int rcon[];
//--------------------------------------------------------------------------
// instance variables
//--------------------------------------------------------------------------
int ROUNDS;
unsigned int workingKey_[ MAXROUNDS + 1 ] [ 4 ];
int A0;
int A1;
int A2;
int A3;
bool forEncryption_;
int keyLength_;
byte cbcV_[ AES_BLOCK_SIZE ];
int mode_;
//--------------------------------------------------------------------------
private:
//--------------------------------------------------------------------------
// cipher methods
//--------------------------------------------------------------------------
byte mul0x2( int b );
byte mul0x3( int b );
byte mul0x9( int b );
byte mul0xb( int b );
byte mul0xd( int b );
byte mul0xe( int b );
void KeyAddition( int rk[] );
int shift( int r, int shift );
void ShiftRow();
void InvShiftRow();
int applyS( int r, const byte* box );
void Substitution( const byte* box );
void MixColumn();
void InvMixColumn();
void generateWorkingKey( byte key[] );
int processBlock( byte in[], int inOff, byte out[], int outOff);
void unpackBlock( byte bytes[], int off );
void packBlock( byte bytes[], int off );
void encryptBlock( int rk[][4] );
void decryptBlock( int rk[][4] );
};
#endif // AFX_AESENGINE_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -