www.pudn.com > TestAes.rar > Aes1.h


// Aes1.h: interface for the Aes class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#if !defined(AFX_AES1_H__17D9C8A6_399D_4034_9618_1106169469A1__INCLUDED_) 
#define AFX_AES1_H__17D9C8A6_399D_4034_9618_1106169469A1__INCLUDED_ 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
 
#include "stdafx.h" 
 
class AES 
{ 
public: 
typedef enum ENUM_KeySize_ 
{ 
 BIT128 = 0, 
 BIT192, 
 BIT256 
}ENUM_KEYSIZE; 
public: 
 AES( ENUM_KEYSIZE keysize, BYTE *key ); 
 ~AES(void); 
 void Cipher( BYTE *input, BYTE *output ); 
 void InvCipher( BYTE *input, BYTE *output ); 
protected: 
 BYTE *RotWord( BYTE *word ); 
 BYTE *SubWord( BYTE *word ); 
 void AddRoundKey(int round); 
 void SubBytes(); 
 void InvSubBytes(); 
 void ShiftRows(); 
 void InvShiftRows(); 
 void MixColumns(); 
 void InvMixColumns(); 
  static BYTE gfmultby01(BYTE b)  //乘1 
    { 
      return b; 
    } 
 
    static BYTE gfmultby02(BYTE b) //乘2 
    { 
      if (b < 0x80) 
        return (BYTE)(int)(b <<1); 
      else 
        return (BYTE)( (int)(b << 1) ^ (int)(0x1b) ); 
    } 
 
    static BYTE gfmultby03(BYTE b) 
    { 
      return (BYTE) ( (int)gfmultby02(b) ^ (int)b );//GF域的加法运算就是异或 
    } 
 
    static BYTE gfmultby09(BYTE b) 
    { 
      return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^ 
                     (int)b ); 
    } 
 
    static BYTE gfmultby0b(BYTE b) 
    { 
      return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^ 
                     (int)gfmultby02(b) ^ 
                     (int)b ); 
    } 
 
    static BYTE gfmultby0d(BYTE b) 
    { 
      return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^ 
                     (int)gfmultby02(gfmultby02(b)) ^ 
                     (int)(b) ); 
    } 
 
    static BYTE gfmultby0e(BYTE b) 
    { 
      return (BYTE)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^ 
                     (int)gfmultby02(gfmultby02(b)) ^ 
                     (int)gfmultby02(b) ); 
    } 
 int Nb; //代表以字为单位的块长, 
 int Nk;//代表以字为单位的密钥长度 
 int Nr;//轮数 ,轮数是10、12或14中的任意一个并且是基于密码分析学理论的。 
        //它直接取决于密钥长度。 
 BYTE *key;// the seed key. size will be 4 * keySize from ctor. 
 typedef struct BYTE4_ 
 { 
  BYTE w[4]; 
 }BYTE4; 
 BYTE4 *w; 
 LPBYTE State[4]; 
 /* 
    private byte[,] iSbox;  // inverse Substitution box  
    private byte[,] w;      // key schedule array.  
    private byte[,] Rcon;   // Round constants. 
    private byte[,] State;  // State matrix*/ 
 
}; 
 
#endif // !defined(AFX_AES1_H__17D9C8A6_399D_4034_9618_1106169469A1__INCLUDED_)