📄 aes.h
字号:
// AES.h
#pragma once
#include <cstring>
using namespace std;
#ifndef MBYTE
typedef unsigned char MBYTE;
#endif
#ifndef MWORD
typedef unsigned int MWORD;
#endif
// The Advanced Encryption Standard (AES) specifies a FIPS-approved cryptographic
// algorithm that can be used to protect electronic data. The AES algorithm is a
// symmetric block cipher that can encrypt (encipher) and decrypt (decipher)
// information.
//
// AES is based on the Rijndael algorithm created by Joan Daemen and Vincent Rijmen,
// a symmetric block cipher that can process data blocks of 128 bits, using cipher
// keys with lengths of 128, 192, and 256 bits. Rijndael was designed to handle
// additional block sizes and key lengths, however they are not adopted in the AES
// standard nor in this implementation.
//
// This implementation is created by David Zier and Martin Held. It's purpose is to
// allow the user to step through the algorithm and view the results at specific
// layers. The main purpose is for an eductional experience.
class CAES
{
public:
// AES States
enum { STATE_IDLE = 0, STATE_ENCRYPT, STATE_DECRYPT, STATE_OK, STATE_DONE, STATE_ERR };
// AES Layers
enum { NC = -1, ARK = 0, BS, SR, MC, IBS, ISR, IMC };
// Block Size
enum { BLOCK_SIZE = 16, BC = BLOCK_SIZE/4 };
// Maximum Constants
enum { /*Max Rounds*/MAX_ROUNDS = 14, /*Max Key Columns*/MAX_KC = 8 };
public:
CAES(void);
~CAES(void);
//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 MakeKey (int const* key, int keylength = BLOCK_SIZE);
// Cipher Operations
int Step (); // Step through one layer
int Back (); // Go back one layer
int Complete (); // Complete the current cipher process
int Reset (); // Reset the current cipher process
// Data Access Functions
int GetState() { return m_eState; };
bool SetState(int state)
{
if (!(state == STATE_IDLE || state == STATE_ENCRYPT || state == STATE_DECRYPT)
|| (m_eState != STATE_IDLE && m_bKeyInit))
return false;
else
m_eState = state;
return true;
}
bool GetKeyInit() { return m_bKeyInit; };
int GetLayer() { return m_eLayer; };
int GetRound() { return m_iRound; };
int GetNumRounds() { return m_iNumRounds; };
int GetKeyLength() { return m_iKeyLength; };
void GetData(int data[]); // Get Data Elements
bool SetData(int data[]); // Set Data Elements
bool GetEncKey(int Ke[][BC]); // Get the Encryption Round Keys
bool GetDecKey(int Kd[][BC]); // Get the Decryption Round Keys
protected:
// Layer operations
void AddRoundKey(int mode);
void ByteSub();
void ShiftRow();
void MixColumn();
void InvByteSub();
void InvShiftRow();
void InvMixColumn();
private:
// GF(2^8) Multiplication Helper Functions
MBYTE xTime(MBYTE x); // GF(2^8) modulation for multiplication
MBYTE GFMul(MBYTE a, MBYTE b); // GF(2^8) multiplication
void GFMatMul (MBYTE *A, const MBYTE B[4][4], MBYTE *Res); // GF(2^8) Matrix Multiplication
// Miscellaneous Helper Functions
MWORD InvMixColumnWord(MWORD x);
MWORD MakeWord (MBYTE x1, MBYTE x2, MBYTE x3, MBYTE x4);
MWORD RotWord (MWORD x) {return ((x<<8) | ((x>>24)&0xff));};
MWORD SubWord (MWORD x);
protected:
// Look-up Tables
static const char sm_S[256]; // S-Box Matrix
static const char sm_Si[256]; // Inverse S-Box Matrix
static const char sm_rcon[31];
static const int sm_shifts[3][4][2]; // Shift table for SR and ISR
// AES State variables
bool m_bKeyInit; // Key initialization flag
int m_eState; // Current Cipher state
int m_eLayer; // Current Layer
int m_iRound; // Current Round
int m_iNumRounds; // Total Number of Rounds
int m_iKeyLength; // Key Length
// Data Variables
MBYTE m_byData[BC][4]; // The data being ciphered
int m_iKe[MAX_ROUNDS+1][BC]; // Encryption round keys
int m_iKd[MAX_ROUNDS+1][BC]; // Decryption round keys
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -