2024-03-22 12:07:32 +08:00
|
|
|
|
#pragma once
|
2021-10-07 00:34:29 +08:00
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2024-03-22 12:07:32 +08:00
|
|
|
|
class AES
|
|
|
|
|
{
|
2021-10-07 00:34:29 +08:00
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
AES();
|
|
|
|
|
AES(const unsigned char *key);
|
|
|
|
|
virtual ~AES();
|
|
|
|
|
void encrypt(const unsigned char data[16], unsigned char out[16]);
|
|
|
|
|
void decrypt(const unsigned char data[16], unsigned char out[16]);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
//
|
|
|
|
|
int mNb;
|
|
|
|
|
|
2024-03-22 12:07:32 +08:00
|
|
|
|
// word length of the secret key used in one turn
|
2021-10-07 00:34:29 +08:00
|
|
|
|
int mNk;
|
|
|
|
|
|
2024-03-22 12:07:32 +08:00
|
|
|
|
// number of turns
|
2021-10-07 00:34:29 +08:00
|
|
|
|
int mNr;
|
|
|
|
|
|
2024-03-22 12:07:32 +08:00
|
|
|
|
// the secret key,which can be 16bytes,24bytes or 32bytes
|
2021-10-07 00:34:29 +08:00
|
|
|
|
unsigned char mKey[32];
|
|
|
|
|
|
2024-03-22 12:07:32 +08:00
|
|
|
|
// the extended key,which can be 176bytes,208bytes,240bytes
|
2021-10-07 00:34:29 +08:00
|
|
|
|
unsigned char mW[60][4];
|
|
|
|
|
|
|
|
|
|
static unsigned char sBox[];
|
|
|
|
|
static unsigned char invSBox[];
|
2024-03-22 12:07:32 +08:00
|
|
|
|
// constant
|
2021-10-07 00:34:29 +08:00
|
|
|
|
static unsigned char rcon[];
|
|
|
|
|
void setKey(const unsigned char *key);
|
|
|
|
|
|
|
|
|
|
void subBytes(unsigned char state[][4]);
|
|
|
|
|
void shiftRows(unsigned char state[][4]);
|
|
|
|
|
void mixColumns(unsigned char state[][4]);
|
|
|
|
|
void addRoundKey(unsigned char state[][4], unsigned char w[][4]);
|
|
|
|
|
|
|
|
|
|
void invSubBytes(unsigned char state[][4]);
|
|
|
|
|
void invShiftRows(unsigned char state[][4]);
|
|
|
|
|
void invMixColumns(unsigned char state[][4]);
|
|
|
|
|
|
|
|
|
|
void keyExpansion();
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
unsigned char GF28Multi(unsigned char s, unsigned char a);
|
|
|
|
|
|
|
|
|
|
void rotWord(unsigned char w[]);
|
|
|
|
|
void subWord(unsigned char w[]);
|
|
|
|
|
|
2024-03-22 12:07:32 +08:00
|
|
|
|
// get the secret key
|
|
|
|
|
void getKeyAt(unsigned char key[][4], int i);
|
2021-10-07 00:34:29 +08:00
|
|
|
|
};
|