随笔-23  评论-73  文章-3  trackbacks-0
/*-------------------------------------

SetKey函数提供设置加密长度还有密钥功能
Encrypt函数实现文件加密
Decrypt函数实现文件解密


-------------------------------------*/


 


#include <stdio.h>
#include <stdlib.h>
#include <ntifs.h>
#include <assert.h>


 


// 这些函数是外界需要调用的


 


//AES初始化
void AES_Constructor(void);


void AES_Destructor(void);


 


//设置AES密钥
BOOLEAN SetKey(const unsigned char *key, int keylen);



//AES加密函数
BOOLEAN Encrypt(const unsigned char *in, int inlen,
        unsigned char *out, int *outlen);



//AES解密函数
BOOLEAN Decrypt(const unsigned char *in, int inlen,
        unsigned char *out, int *outlen);


 


void Clear();


 


typedef unsigned char byte;
typedef unsigned long word;
typedef byte (State)[4][4];


void  Cipher(byte *in, byte *out);


void AddRoundKey(State state, word *w, int wstart);


void SubBytes(State state);


void ShiftRows(State state);


void MixColumns(State state);


void InvSubBytes(State state);


void InvShiftRows(State state);


void InvMixColumns(State state);


 


__inline word SubWord(word in);
__inline word RotWord(word in);


 


void InvCipher(byte *in, byte *out);


void KeyExpansion(byte *key, int keylen /* 4 * Nk */);


void EqKeyExpansion(byte *key, int keylen /* 4 * Nk */);


void EqInvCipher(byte *in, byte *out);


void  SetDecryptOption( BOOLEAN bEquivalent);


 


const byte m_Mul_02[] =
{
 0x1b,0x19,0x1f,0x1d,0x13,0x11,0x17,0x15,0x0b,0x09,0x0f,0x0d,0x03,0x01,0x07,0x05,
  0x3b,0x39,0x3f,0x3d,0x33,0x31,0x37,0x35,0x2b,0x29,0x2f,0x2d,0x23,0x21,0x27,0x25,
  0x5b,0x59,0x5f,0x5d,0x53,0x51,0x57,0x55,0x4b,0x49,0x4f,0x4d,0x43,0x41,0x47,0x45,
  0x7b,0x79,0x7f,0x7d,0x73,0x71,0x77,0x75,0x6b,0x69,0x6f,0x6d,0x63,0x61,0x67,0x65,
  0x9b,0x99,0x9f,0x9d,0x93,0x91,0x97,0x95,0x8b,0x89,0x8f,0x8d,0x83,0x81,0x87,0x85,
  0xbb,0xb9,0xbf,0xbd,0xb3,0xb1,0xb7,0xb5,0xab,0xa9,0xaf,0xad,0xa3,0xa1,0xa7,0xa5,
  0xdb,0xd9,0xdf,0xdd,0xd3,0xd1,0xd7,0xd5,0xcb,0xc9,0xcf,0xcd,0xc3,0xc1,0xc7,0xc5,
  0xfb,0xf9,0xff,0xfd,0xf3,0xf1,0xf7,0xf5,0xeb,0xe9,0xef,0xed,0xe3,0xe1,0xe7,0xe5,
  0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
  0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
  0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
  0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
  0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
  0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
  0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
  0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
};
const byte m_Mul_03[] =
{
 0x1b,0x18,0x1d,0x1e,0x17,0x14,0x11,0x12,0x03,0x00,0x05,0x06,0x0f,0x0c,0x09,0x0a,
  0x2b,0x28,0x2d,0x2e,0x27,0x24,0x21,0x22,0x33,0x30,0x35,0x36,0x3f,0x3c,0x39,0x3a,
  0x7b,0x78,0x7d,0x7e,0x77,0x74,0x71,0x72,0x63,0x60,0x65,0x66,0x6f,0x6c,0x69,0x6a,
  0x4b,0x48,0x4d,0x4e,0x47,0x44,0x41,0x42,0x53,0x50,0x55,0x56,0x5f,0x5c,0x59,0x5a,
  0xdb,0xd8,0xdd,0xde,0xd7,0xd4,0xd1,0xd2,0xc3,0xc0,0xc5,0xc6,0xcf,0xcc,0xc9,0xca,
  0xeb,0xe8,0xed,0xee,0xe7,0xe4,0xe1,0xe2,0xf3,0xf0,0xf5,0xf6,0xff,0xfc,0xf9,0xfa,
  0xbb,0xb8,0xbd,0xbe,0xb7,0xb4,0xb1,0xb2,0xa3,0xa0,0xa5,0xa6,0xaf,0xac,0xa9,0xaa,
  0x8b,0x88,0x8d,0x8e,0x87,0x84,0x81,0x82,0x93,0x90,0x95,0x96,0x9f,0x9c,0x99,0x9a,
  0x80,0x83,0x86,0x85,0x8c,0x8f,0x8a,0x89,0x98,0x9b,0x9e,0x9d,0x94,0x97,0x92,0x91,
  0xb0,0xb3,0xb6,0xb5,0xbc,0xbf,0xba,0xb9,0xa8,0xab,0xae,0xad,0xa4,0xa7,0xa2,0xa1,
  0xe0,0xe3,0xe6,0xe5,0xec,0xef,0xea,0xe9,0xf8,0xfb,0xfe,0xfd,0xf4,0xf7,0xf2,0xf1,
  0xd0,0xd3,0xd6,0xd5,0xdc,0xdf,0xda,0xd9,0xc8,0xcb,0xce,0xcd,0xc4,0xc7,0xc2,0xc1,
  0x40,0x43,0x46,0x45,0x4c,0x4f,0x4a,0x49,0x58,0x5b,0x5e,0x5d,0x54,0x57,0x52,0x51,
  0x70,0x73,0x76,0x75,0x7c,0x7f,0x7a,0x79,0x68,0x6b,0x6e,0x6d,0x64,0x67,0x62,0x61,
  0x20,0x23,0x26,0x25,0x2c,0x2f,0x2a,0x29,0x38,0x3b,0x3e,0x3d,0x34,0x37,0x32,0x31,
  0x10,0x13,0x16,0x15,0x1c,0x1f,0x1a,0x19,0x08,0x0b,0x0e,0x0d,0x04,0x07,0x02,0x01,
};
const byte m_Mul_04[] =
{
 0x2d,0x29,0x25,0x21,0x3d,0x39,0x35,0x31,0x0d,0x09,0x05,0x01,0x1d,0x19,0x15,0x11,
  0x6d,0x69,0x65,0x61,0x7d,0x79,0x75,0x71,0x4d,0x49,0x45,0x41,0x5d,0x59,0x55,0x51,
  0xad,0xa9,0xa5,0xa1,0xbd,0xb9,0xb5,0xb1,0x8d,0x89,0x85,0x81,0x9d,0x99,0x95,0x91,
  0xed,0xe9,0xe5,0xe1,0xfd,0xf9,0xf5,0xf1,0xcd,0xc9,0xc5,0xc1,0xdd,0xd9,0xd5,0xd1,
  0x36,0x32,0x3e,0x3a,0x26,0x22,0x2e,0x2a,0x16,0x12,0x1e,0x1a,0x06,0x02,0x0e,0x0a,
  0x76,0x72,0x7e,0x7a,0x66,0x62,0x6e,0x6a,0x56,0x52,0x5e,0x5a,0x46,0x42,0x4e,0x4a,
  0xb6,0xb2,0xbe,0xba,0xa6,0xa2,0xae,0xaa,0x96,0x92,0x9e,0x9a,0x86,0x82,0x8e,0x8a,
  0xf6,0xf2,0xfe,0xfa,0xe6,0xe2,0xee,0xea,0xd6,0xd2,0xde,0xda,0xc6,0xc2,0xce,0xca,
  0x1b,0x1f,0x13,0x17,0x0b,0x0f,0x03,0x07,0x3b,0x3f,0x33,0x37,0x2b,0x2f,0x23,0x27,
  0x5b,0x5f,0x53,0x57,0x4b,0x4f,0x43,0x47,0x7b,0x7f,0x73,0x77,0x6b,0x6f,0x63,0x67,
  0x9b,0x9f,0x93,0x97,0x8b,0x8f,0x83,0x87,0xbb,0xbf,0xb3,0xb7,0xab,0xaf,0xa3,0xa7,
  0xdb,0xdf,0xd3,0xd7,0xcb,0xcf,0xc3,0xc7,0xfb,0xff,0xf3,0xf7,0xeb,0xef,0xe3,0xe7,
  0x00,0x04,0x08,0x0c,0x10,0x14,0x18,0x1c,0x20,0x24,0x28,0x2c,0x30,0x34,0x38,0x3c,
  0x40,0x44,0x48,0x4c,0x50,0x54,0x58,0x5c,0x60,0x64,0x68,0x6c,0x70,0x74,0x78,0x7c,
  0x80,0x84,0x88,0x8c,0x90,0x94,0x98,0x9c,0xa0,0xa4,0xa8,0xac,0xb0,0xb4,0xb8,0xbc,
  0xc0,0xc4,0xc8,0xcc,0xd0,0xd4,0xd8,0xdc,0xe0,0xe4,0xe8,0xec,0xf0,0xf4,0xf8,0xfc,
};
const byte m_Mul_08[] =
{
 0x41,0x49,0x51,0x59,0x61,0x69,0x71,0x79,0x01,0x09,0x11,0x19,0x21,0x29,0x31,0x39,
  0xc1,0xc9,0xd1,0xd9,0xe1,0xe9,0xf1,0xf9,0x81,0x89,0x91,0x99,0xa1,0xa9,0xb1,0xb9,
  0x5a,0x52,0x4a,0x42,0x7a,0x72,0x6a,0x62,0x1a,0x12,0x0a,0x02,0x3a,0x32,0x2a,0x22,
  0xda,0xd2,0xca,0xc2,0xfa,0xf2,0xea,0xe2,0x9a,0x92,0x8a,0x82,0xba,0xb2,0xaa,0xa2,
  0x77,0x7f,0x67,0x6f,0x57,0x5f,0x47,0x4f,0x37,0x3f,0x27,0x2f,0x17,0x1f,0x07,0x0f,
  0xf7,0xff,0xe7,0xef,0xd7,0xdf,0xc7,0xcf,0xb7,0xbf,0xa7,0xaf,0x97,0x9f,0x87,0x8f,
  0x6c,0x64,0x7c,0x74,0x4c,0x44,0x5c,0x54,0x2c,0x24,0x3c,0x34,0x0c,0x04,0x1c,0x14,
  0xec,0xe4,0xfc,0xf4,0xcc,0xc4,0xdc,0xd4,0xac,0xa4,0xbc,0xb4,0x8c,0x84,0x9c,0x94,
  0x2d,0x25,0x3d,0x35,0x0d,0x05,0x1d,0x15,0x6d,0x65,0x7d,0x75,0x4d,0x45,0x5d,0x55,
  0xad,0xa5,0xbd,0xb5,0x8d,0x85,0x9d,0x95,0xed,0xe5,0xfd,0xf5,0xcd,0xc5,0xdd,0xd5,
  0x36,0x3e,0x26,0x2e,0x16,0x1e,0x06,0x0e,0x76,0x7e,0x66,0x6e,0x56,0x5e,0x46,0x4e,
  0xb6,0xbe,0xa6,0xae,0x96,0x9e,0x86,0x8e,0xf6,0xfe,0xe6,0xee,0xd6,0xde,0xc6,0xce,
  0x1b,0x13,0x0b,0x03,0x3b,0x33,0x2b,0x23,0x5b,0x53,0x4b,0x43,0x7b,0x73,0x6b,0x63,
  0x9b,0x93,0x8b,0x83,0xbb,0xb3,0xab,0xa3,0xdb,0xd3,0xcb,0xc3,0xfb,0xf3,0xeb,0xe3,
  0x00,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40,0x48,0x50,0x58,0x60,0x68,0x70,0x78,
  0x80,0x88,0x90,0x98,0xa0,0xa8,0xb0,0xb8,0xc0,0xc8,0xd0,0xd8,0xe0,0xe8,0xf0,0xf8,
};
const byte m_Mul_09[] =
{
 0x41,0x48,0x53,0x5a,0x65,0x6c,0x77,0x7e,0x09,0x00,0x1b,0x12,0x2d,0x24,0x3f,0x36,
  0xd1,0xd8,0xc3,0xca,0xf5,0xfc,0xe7,0xee,0x99,0x90,0x8b,0x82,0xbd,0xb4,0xaf,0xa6,
  0x7a,0x73,0x68,0x61,0x5e,0x57,0x4c,0x45,0x32,0x3b,0x20,0x29,0x16,0x1f,0x04,0x0d,
  0xea,0xe3,0xf8,0xf1,0xce,0xc7,0xdc,0xd5,0xa2,0xab,0xb0,0xb9,0x86,0x8f,0x94,0x9d,
  0x37,0x3e,0x25,0x2c,0x13,0x1a,0x01,0x08,0x7f,0x76,0x6d,0x64,0x5b,0x52,0x49,0x40,
  0xa7,0xae,0xb5,0xbc,0x83,0x8a,0x91,0x98,0xef,0xe6,0xfd,0xf4,0xcb,0xc2,0xd9,0xd0,
  0x0c,0x05,0x1e,0x17,0x28,0x21,0x3a,0x33,0x44,0x4d,0x56,0x5f,0x60,0x69,0x72,0x7b,
  0x9c,0x95,0x8e,0x87,0xb8,0xb1,0xaa,0xa3,0xd4,0xdd,0xc6,0xcf,0xf0,0xf9,0xe2,0xeb,
  0xad,0xa4,0xbf,0xb6,0x89,0x80,0x9b,0x92,0xe5,0xec,0xf7,0xfe,0xc1,0xc8,0xd3,0xda,
  0x3d,0x34,0x2f,0x26,0x19,0x10,0x0b,0x02,0x75,0x7c,0x67,0x6e,0x51,0x58,0x43,0x4a,
  0x96,0x9f,0x84,0x8d,0xb2,0xbb,0xa0,0xa9,0xde,0xd7,0xcc,0xc5,0xfa,0xf3,0xe8,0xe1,
  0x06,0x0f,0x14,0x1d,0x22,0x2b,0x30,0x39,0x4e,0x47,0x5c,0x55,0x6a,0x63,0x78,0x71,
  0xdb,0xd2,0xc9,0xc0,0xff,0xf6,0xed,0xe4,0x93,0x9a,0x81,0x88,0xb7,0xbe,0xa5,0xac,
  0x4b,0x42,0x59,0x50,0x6f,0x66,0x7d,0x74,0x03,0x0a,0x11,0x18,0x27,0x2e,0x35,0x3c,
  0xe0,0xe9,0xf2,0xfb,0xc4,0xcd,0xd6,0xdf,0xa8,0xa1,0xba,0xb3,0x8c,0x85,0x9e,0x97,
  0x70,0x79,0x62,0x6b,0x54,0x5d,0x46,0x4f,0x38,0x31,0x2a,0x23,0x1c,0x15,0x0e,0x07,
};
const byte m_Mul_0b[] =
{
 0x5a,0x51,0x4c,0x47,0x76,0x7d,0x60,0x6b,0x02,0x09,0x14,0x1f,0x2e,0x25,0x38,0x33,
  0xea,0xe1,0xfc,0xf7,0xc6,0xcd,0xd0,0xdb,0xb2,0xb9,0xa4,0xaf,0x9e,0x95,0x88,0x83,
  0x21,0x2a,0x37,0x3c,0x0d,0x06,0x1b,0x10,0x79,0x72,0x6f,0x64,0x55,0x5e,0x43,0x48,
  0x91,0x9a,0x87,0x8c,0xbd,0xb6,0xab,0xa0,0xc9,0xc2,0xdf,0xd4,0xe5,0xee,0xf3,0xf8,
  0xac,0xa7,0xba,0xb1,0x80,0x8b,0x96,0x9d,0xf4,0xff,0xe2,0xe9,0xd8,0xd3,0xce,0xc5,
  0x1c,0x17,0x0a,0x01,0x30,0x3b,0x26,0x2d,0x44,0x4f,0x52,0x59,0x68,0x63,0x7e,0x75,
  0xd7,0xdc,0xc1,0xca,0xfb,0xf0,0xed,0xe6,0x8f,0x84,0x99,0x92,0xa3,0xa8,0xb5,0xbe,
  0x67,0x6c,0x71,0x7a,0x4b,0x40,0x5d,0x56,0x3f,0x34,0x29,0x22,0x13,0x18,0x05,0x0e,
  0xad,0xa6,0xbb,0xb0,0x81,0x8a,0x97,0x9c,0xf5,0xfe,0xe3,0xe8,0xd9,0xd2,0xcf,0xc4,
  0x1d,0x16,0x0b,0x00,0x31,0x3a,0x27,0x2c,0x45,0x4e,0x53,0x58,0x69,0x62,0x7f,0x74,
  0xd6,0xdd,0xc0,0xcb,0xfa,0xf1,0xec,0xe7,0x8e,0x85,0x98,0x93,0xa2,0xa9,0xb4,0xbf,
  0x66,0x6d,0x70,0x7b,0x4a,0x41,0x5c,0x57,0x3e,0x35,0x28,0x23,0x12,0x19,0x04,0x0f,
  0x5b,0x50,0x4d,0x46,0x77,0x7c,0x61,0x6a,0x03,0x08,0x15,0x1e,0x2f,0x24,0x39,0x32,
  0xeb,0xe0,0xfd,0xf6,0xc7,0xcc,0xd1,0xda,0xb3,0xb8,0xa5,0xae,0x9f,0x94,0x89,0x82,
  0x20,0x2b,0x36,0x3d,0x0c,0x07,0x1a,0x11,0x78,0x73,0x6e,0x65,0x54,0x5f,0x42,0x49,
  0x90,0x9b,0x86,0x8d,0xbc,0xb7,0xaa,0xa1,0xc8,0xc3,0xde,0xd5,0xe4,0xef,0xf2,0xf9,
};
const byte m_Mul_0d[] =
{
 0x6c,0x61,0x76,0x7b,0x58,0x55,0x42,0x4f,0x04,0x09,0x1e,0x13,0x30,0x3d,0x2a,0x27,
  0xbc,0xb1,0xa6,0xab,0x88,0x85,0x92,0x9f,0xd4,0xd9,0xce,0xc3,0xe0,0xed,0xfa,0xf7,
  0xd7,0xda,0xcd,0xc0,0xe3,0xee,0xf9,0xf4,0xbf,0xb2,0xa5,0xa8,0x8b,0x86,0x91,0x9c,
  0x07,0x0a,0x1d,0x10,0x33,0x3e,0x29,0x24,0x6f,0x62,0x75,0x78,0x5b,0x56,0x41,0x4c,
  0x01,0x0c,0x1b,0x16,0x35,0x38,0x2f,0x22,0x69,0x64,0x73,0x7e,0x5d,0x50,0x47,0x4a,
  0xd1,0xdc,0xcb,0xc6,0xe5,0xe8,0xff,0xf2,0xb9,0xb4,0xa3,0xae,0x8d,0x80,0x97,0x9a,
  0xba,0xb7,0xa0,0xad,0x8e,0x83,0x94,0x99,0xd2,0xdf,0xc8,0xc5,0xe6,0xeb,0xfc,0xf1,
  0x6a,0x67,0x70,0x7d,0x5e,0x53,0x44,0x49,0x02,0x0f,0x18,0x15,0x36,0x3b,0x2c,0x21,
  0xb6,0xbb,0xac,0xa1,0x82,0x8f,0x98,0x95,0xde,0xd3,0xc4,0xc9,0xea,0xe7,0xf0,0xfd,
  0x66,0x6b,0x7c,0x71,0x52,0x5f,0x48,0x45,0x0e,0x03,0x14,0x19,0x3a,0x37,0x20,0x2d,
  0x0d,0x00,0x17,0x1a,0x39,0x34,0x23,0x2e,0x65,0x68,0x7f,0x72,0x51,0x5c,0x4b,0x46,
  0xdd,0xd0,0xc7,0xca,0xe9,0xe4,0xf3,0xfe,0xb5,0xb8,0xaf,0xa2,0x81,0x8c,0x9b,0x96,
  0xdb,0xd6,0xc1,0xcc,0xef,0xe2,0xf5,0xf8,0xb3,0xbe,0xa9,0xa4,0x87,0x8a,0x9d,0x90,
  0x0b,0x06,0x11,0x1c,0x3f,0x32,0x25,0x28,0x63,0x6e,0x79,0x74,0x57,0x5a,0x4d,0x40,
  0x60,0x6d,0x7a,0x77,0x54,0x59,0x4e,0x43,0x08,0x05,0x12,0x1f,0x3c,0x31,0x26,0x2b,
  0xb0,0xbd,0xaa,0xa7,0x84,0x89,0x9e,0x93,0xd8,0xd5,0xc2,0xcf,0xec,0xe1,0xf6,0xfb,
};
const byte m_Mul_0e[] =
{
 0x77,0x79,0x6b,0x65,0x4f,0x41,0x53,0x5d,0x07,0x09,0x1b,0x15,0x3f,0x31,0x23,0x2d,
  0x97,0x99,0x8b,0x85,0xaf,0xa1,0xb3,0xbd,0xe7,0xe9,0xfb,0xf5,0xdf,0xd1,0xc3,0xcd,
  0xac,0xa2,0xb0,0xbe,0x94,0x9a,0x88,0x86,0xdc,0xd2,0xc0,0xce,0xe4,0xea,0xf8,0xf6,
  0x4c,0x42,0x50,0x5e,0x74,0x7a,0x68,0x66,0x3c,0x32,0x20,0x2e,0x04,0x0a,0x18,0x16,
  0xda,0xd4,0xc6,0xc8,0xe2,0xec,0xfe,0xf0,0xaa,0xa4,0xb6,0xb8,0x92,0x9c,0x8e,0x80,
  0x3a,0x34,0x26,0x28,0x02,0x0c,0x1e,0x10,0x4a,0x44,0x56,0x58,0x72,0x7c,0x6e,0x60,
  0x01,0x0f,0x1d,0x13,0x39,0x37,0x25,0x2b,0x71,0x7f,0x6d,0x63,0x49,0x47,0x55,0x5b,
  0xe1,0xef,0xfd,0xf3,0xd9,0xd7,0xc5,0xcb,0x91,0x9f,0x8d,0x83,0xa9,0xa7,0xb5,0xbb,
  0x36,0x38,0x2a,0x24,0x0e,0x00,0x12,0x1c,0x46,0x48,0x5a,0x54,0x7e,0x70,0x62,0x6c,
  0xd6,0xd8,0xca,0xc4,0xee,0xe0,0xf2,0xfc,0xa6,0xa8,0xba,0xb4,0x9e,0x90,0x82,0x8c,
  0xed,0xe3,0xf1,0xff,0xd5,0xdb,0xc9,0xc7,0x9d,0x93,0x81,0x8f,0xa5,0xab,0xb9,0xb7,
  0x0d,0x03,0x11,0x1f,0x35,0x3b,0x29,0x27,0x7d,0x73,0x61,0x6f,0x45,0x4b,0x59,0x57,
  0x9b,0x95,0x87,0x89,0xa3,0xad,0xbf,0xb1,0xeb,0xe5,0xf7,0xf9,0xd3,0xdd,0xcf,0xc1,
  0x7b,0x75,0x67,0x69,0x43,0x4d,0x5f,0x51,0x0b,0x05,0x17,0x19,0x33,0x3d,0x2f,0x21,
  0x40,0x4e,0x5c,0x52,0x78,0x76,0x64,0x6a,0x30,0x3e,0x2c,0x22,0x08,0x06,0x14,0x1a,
  0xa0,0xae,0xbc,0xb2,0x98,0x96,0x84,0x8a,0xd0,0xde,0xcc,0xc2,0xe8,0xe6,0xf4,0xfa,
};



const byte m_Sbox[] =
{
 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
  0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
  0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
  0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
  0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
  0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
  0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
  0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
  0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
  0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
  0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
  0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
  0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
  0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
  0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
  0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16,
};


const byte m_InvSbox[] =
{
 0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,
  0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,
  0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e,
  0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,
  0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,
  0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,
  0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06,
  0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,
  0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
  0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,
  0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,
  0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,
  0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,
  0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,
  0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
  0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d,
};


const static word m_Rcon[11] =
{
 0xffffffff,
  0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
  0x20000000, 0x40000000, 0x80000000, 0x1b000000, 0x36000000,


};


int m_Nk;
int m_Nr;
// Key Expansion
word m_w[60]; // Max Nb * (Nr + 1)
// Eq Key Expansion
word m_dw[60];


BOOLEAN m_bEq;



VOID AES_Constructor(VOID)
{
 m_Nk = 6;
 m_Nr = 12;
 m_bEq = FALSE;


 memset(m_w, 0, sizeof(m_w));
 memset(m_dw, 0, sizeof(m_dw));
}


VOID AES_Destructor(VOID)
{
 
}



void Cipher(byte *in, byte *out)
{
 int r;
 int c;
 int round;


 State state;
 
 for(r = 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   state[r][c] = in[r + 4 * c];
  }
 }


 AddRoundKey(state, m_w, 0);


 for(round = 1; round < m_Nr; round++)
 {
  SubBytes(state);
  ShiftRows(state);
  MixColumns(state);
  AddRoundKey(state, m_w, round * 4);
 }


 SubBytes(state);
 ShiftRows(state);
 AddRoundKey(state, m_w, m_Nr * 4);


 for(r = 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   out[r + 4 * c] = state[r][c];
  }
 }
}


void AddRoundKey(State state, word *w, int wstart)
{
 int c ;


 for(c = 0; c < 4; c++)
 {
  state[0][c] ^= w[wstart + c] >> 24;
  state[1][c] ^= (w[wstart + c] & 0xff0000) >> 16;
  state[2][c] ^= (w[wstart + c] & 0xff00) >> 8;
  state[3][c] ^= w[wstart + c] & 0xff;
 }
}



void SubBytes(State state)
{
 int r ;
 int c;


 for(r = 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   state[r][c] = m_Sbox[state[r][c]];
  }
 }


}


void ShiftRows(State state)
{
 byte tmp[3];
 int c ;
 int r ;
 int i ;
 int k;


 for( r = 1; r < 4; r++)
 {
  for(i = 0; i < r; i++)
  {
   tmp = state[r];
  }


  for(c = r; c < 4; c++)
  {
   state[r][c - r] = state[r][c];
  }


  for(i = 0; i < r; i++)
  {
   state[r][i + 4 - r] = tmp;
  }


 }


}


void MixColumns(State state)
{
 byte tmp[4];


 int c ;


 for(c = 0; c < 4; c++)
 {
  tmp[0] = m_Mul_02[state[0][c]] ^ m_Mul_03[state[1][c]] ^
   state[2][c] ^ state[3][c];
  tmp[1] = state[0][c] ^ m_Mul_02[state[1][c]] ^
   m_Mul_03[state[2][c]] ^ state[3][c];
  tmp[2] = state[0][c] ^ state[1][c] ^
   m_Mul_02[state[2][c]] ^ m_Mul_03[state[3][c]];
  tmp[3] = m_Mul_03[state[0][c]] ^ state[1][c] ^
   state[2][c] ^ m_Mul_02[state[3][c]];
  state[0][c] = tmp[0];
  state[1][c] = tmp[1];
  state[2][c] = tmp[2];
  state[3][c] = tmp[3];
 }


}


void InvSubBytes(State state)
{
 int r ;
 int c ;


 for(r= 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   state[r][c] = m_InvSbox[state[r][c]];
  }
 }
}



void InvShiftRows(State state)
{
 byte tmp[3];


 int r;
 int i ;
 int c ;


 for(r = 1; r < 4; r++)
 {
  for(i = 0; i < r; i++)
  {
   tmp = state[r][4 - r + i];
  }
  for(c = 4 - r - 1; c >= 0; c--)
  {
   state[r][c + r] = state[r][c];
  }
  for(i = 0; i < r; i++)
  {
   state[r] = tmp;
  }
 }
}



void InvMixColumns(State state)
{
 byte tmp[4];
 int c ;


 for(c = 0; c < 4; c++)
 {
  tmp[0] = m_Mul_0e[state[0][c]] ^ m_Mul_0b[state[1][c]] ^
   m_Mul_0d[state[2][c]] ^ m_Mul_09[state[3][c]];
  tmp[1] = m_Mul_09[state[0][c]] ^ m_Mul_0e[state[1][c]] ^
   m_Mul_0b[state[2][c]] ^ m_Mul_0d[state[3][c]];
  tmp[2] = m_Mul_0d[state[0][c]] ^ m_Mul_09[state[1][c]] ^
   m_Mul_0e[state[2][c]] ^ m_Mul_0b[state[3][c]];
  tmp[3] = m_Mul_0b[state[0][c]] ^ m_Mul_0d[state[1][c]] ^
   m_Mul_09[state[2][c]] ^ m_Mul_0e[state[3][c]];
  state[0][c] = tmp[0];
  state[1][c] = tmp[1];
  state[2][c] = tmp[2];
  state[3][c] = tmp[3];
 }
}


 


__inline word SubWord(word in)
{
 return m_Sbox[in >> 24] << 24 | m_Sbox[(in & 0xff0000) >> 16] << 16 |
  m_Sbox[(in & 0xff00) >> 8] << 8 | m_Sbox[in & 0xff];
}


__inline word RotWord(word in)
{
 return (in << 8) | (in >> 24);
}


void KeyExpansion(byte *key, int keylen /* 4 * Nk */)
{


 word temp;


 int i = 0;


 assert(keylen == 4 * m_Nk);


 while (i < m_Nk)
 {
  m_w[i++] = key[4 * i] << 24 | key[4 * i + 1] << 16 |
   key[4 * i + 2] << 8 | key[4 * i + 3];
 }


 i = m_Nk;


 while (i < 4 * (m_Nr + 1))
 {
  temp = m_w[i - 1];
  if(i % m_Nk == 0)
  {
   temp = SubWord(RotWord(temp)) ^ m_Rcon[i / m_Nk];
  }
  else if(m_Nk > 6 && i % m_Nk == 4)
  {
   temp = SubWord(temp);
  }
  m_w = m_w[i - m_Nk] ^ temp;
  i++;
 }
}



void InvCipher(byte *in, byte *out)
{
 State state;


 int r ;
 int c ;
 int round ;


 for(r = 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   state[r][c] = in[r + 4 * c];
  }
 }


 AddRoundKey(state, m_w, m_Nr * 4);


 for(round = m_Nr - 1; round > 0; round--)
 {
  InvShiftRows(state);
  InvSubBytes(state);
  AddRoundKey(state, m_w, round * 4);
  InvMixColumns(state);
 }


 InvShiftRows(state);
 InvSubBytes(state);
 AddRoundKey(state, m_w, 0);


 for(r = 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   out[r + 4 * c] = state[r][c];
  }
 }
}



void EqKeyExpansion(byte *key, int keylen /* 4 * Nk */)
{
 State s;
 byte *in;
 int r ;
 int c ;
 int i;
 int round ;


 KeyExpansion(key, keylen);


 for( i= 0; i < 4 * (m_Nr + 1); i++)
 {
  m_dw = m_w;
 }


 for(round = 1; round < m_Nr; round++)
 {
  in = (byte*)(m_dw + round * 4);
  for(r = 0; r < 4; r++)
  {
   for(c = 0; c < 4; c++)
   {
    s[r][c] = in[4 * c + 4 - r - 1];
   }
  }
  
  InvMixColumns(s);


  for(r = 0; r < 4; r++)
  {
   for(c = 0; c < 4; c++)
   {
    in[4 * c + 4 - r - 1] = s[r][c];
   }
  }
 }
}


void EqInvCipher(byte *in, byte *out)
{
 State state;
 int r;
 int c;
 int round;


 for(r = 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   state[r][c] = in[r + 4 * c];
  }
 }


 AddRoundKey(state, m_dw, m_Nr * 4);


 for(round = m_Nr - 1; round > 0; round--)
 {
  InvSubBytes(state);
  InvShiftRows(state);
  InvMixColumns(state);
  AddRoundKey(state, m_dw, round * 4);
 }


 InvSubBytes(state);
 InvShiftRows(state);
 AddRoundKey(state, m_dw, 0);


 for(r = 0; r < 4; r++)
 {
  for(c = 0; c < 4; c++)
  {
   out[r + 4 * c] = state[r][c];
  }
 }
}


void  SetDecryptOption( BOOLEAN bEquivalent)
{
 m_bEq = bEquivalent;
}



BOOLEAN SetKey(const unsigned char *key, int keylen)
{
 switch(keylen)
 {
 case 16:
  m_Nk = 4;
  m_Nr = 10;
  break;
 case 24:
  m_Nk = 6;
  m_Nr = 12;
  break;
 case 32:
  m_Nk = 8;
  m_Nr = 14;
  break;
 default:
  return FALSE;
 }


 if(m_bEq)
 {
  EqKeyExpansion((byte*)key, keylen);
 }
 else
 {
  KeyExpansion((byte*)key, keylen);
 }


 return TRUE;
}



BOOLEAN Encrypt(const unsigned char *in, int inlen,
        unsigned char *out, int *outlen)
{
 int tmp;
 int i;


 if(!in)
  return FALSE;


 if(inlen <= 0)
  return FALSE;


 tmp = inlen % (16) ? inlen + (16) - inlen % (16) : inlen;
 
 if(!out || *outlen < tmp)
 {
  *outlen = tmp;
  return FALSE;
 }


 *outlen = tmp;


 while(inlen >= 16)
 {
  Cipher((byte*)in, out);
  in += 16;
  out += 16;
  inlen -= 16;
 }


 if(inlen > 0)
 {
  byte pad[16];


  for(i = 0; i < inlen; i++)
  {
   pad = in;
  }


  for( i = inlen; i < 16; i++)
  {
   pad = 0;
  }


  Cipher(pad, out);
 }


 return TRUE;
}


BOOLEAN Decrypt(const unsigned char *in, int inlen,
        unsigned char *out, int *outlen)
{
 if(!in)
  return FALSE;


 if(inlen <= 0 || inlen % (16) != 0)
  return FALSE;


 if(!out || *outlen < inlen)
 {
  *outlen = inlen;
  return FALSE;
 }


 *outlen = inlen;


 if(m_bEq)
 {
  while(inlen >= 16)
  {
   EqInvCipher((byte*)in, out);
   in += 16;
   out += 16;
   inlen -= 16;
  }
 }
 else
 {
  while(inlen >= 16)
  {
   InvCipher((byte*)in, out);
   in += 16;
   out += 16;
   inlen -= 16;
  }
 }


 return TRUE;
}


 


void Clear()

 memset(m_w, 0, sizeof(m_w));
 memset(m_w, 0, sizeof(m_dw));
}

posted on 2008-04-19 13:02 ViskerWong 阅读(1947) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理