摘自POLARSSL

 #pragma once

 #define AES_ENCRYPT     1
#define AES_DECRYPT 0 struct aes_context
{
int nr; /*!< number of rounds */
unsigned int* rk; /*!< AES round keys */
unsigned int buf[]; /*!< unaligned data */
}; void aes_setkey_enc( aes_context* ctx, const unsigned char* key, int keysize );
void aes_setkey_dec( aes_context* ctx, const unsigned char* key, int keysize );
void aes_crypt_ecb_update( aes_context* ctx, int mode, const unsigned char input[], unsigned char output[] );
unsigned char* aes_crypt_ecb( aes_context* ctx, int mode, const unsigned char* input, int slen, int* dlen );
 #include "aes.h"

 /*
* Forward S-box
*/
static const unsigned char FSb[] =
{
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
}; /*
* Forward tables
*/
#define FT \
\
V(A5,,,C6), V(,7C,7C,F8), V(,,,EE), V(8D,7B,7B,F6), \
V(0D,F2,F2,FF), V(BD,6B,6B,D6), V(B1,6F,6F,DE), V(,C5,C5,), \
V(,,,), V(,,,), V(A9,,,CE), V(7D,2B,2B,), \
V(,FE,FE,E7), V(,D7,D7,B5), V(E6,AB,AB,4D), V(9A,,,EC), \
V(,CA,CA,8F), V(9D,,,1F), V(,C9,C9,), V(,7D,7D,FA), \
V(,FA,FA,EF), V(EB,,,B2), V(C9,,,8E), V(0B,F0,F0,FB), \
V(EC,AD,AD,), V(,D4,D4,B3), V(FD,A2,A2,5F), V(EA,AF,AF,), \
V(BF,9C,9C,), V(F7,A4,A4,), V(,,,E4), V(5B,C0,C0,9B), \
V(C2,B7,B7,), V(1C,FD,FD,E1), V(AE,,,3D), V(6A,,,4C), \
V(5A,,,6C), V(,3F,3F,7E), V(,F7,F7,F5), V(4F,CC,CC,), \
V(5C,,,), V(F4,A5,A5,), V(,E5,E5,D1), V(,F1,F1,F9), \
V(,,,E2), V(,D8,D8,AB), V(,,,), V(3F,,,2A), \
V(0C,,,), V(,C7,C7,), V(,,,), V(5E,C3,C3,9D), \
V(,,,), V(A1,,,), V(0F,,,0A), V(B5,9A,9A,2F), \
V(,,,0E), V(,,,), V(9B,,,1B), V(3D,E2,E2,DF), \
V(,EB,EB,CD), V(,,,4E), V(CD,B2,B2,7F), V(9F,,,EA), \
V(1B,,,), V(9E,,,1D), V(,2C,2C,), V(2E,1A,1A,), \
V(2D,1B,1B,), V(B2,6E,6E,DC), V(EE,5A,5A,B4), V(FB,A0,A0,5B), \
V(F6,,,A4), V(4D,3B,3B,), V(,D6,D6,B7), V(CE,B3,B3,7D), \
V(7B,,,), V(3E,E3,E3,DD), V(,2F,2F,5E), V(,,,), \
V(F5,,,A6), V(,D1,D1,B9), V(,,,), V(2C,ED,ED,C1), \
V(,,,), V(1F,FC,FC,E3), V(C8,B1,B1,), V(ED,5B,5B,B6), \
V(BE,6A,6A,D4), V(,CB,CB,8D), V(D9,BE,BE,), V(4B,,,), \
V(DE,4A,4A,), V(D4,4C,4C,), V(E8,,,B0), V(4A,CF,CF,), \
V(6B,D0,D0,BB), V(2A,EF,EF,C5), V(E5,AA,AA,4F), V(,FB,FB,ED), \
V(C5,,,), V(D7,4D,4D,9A), V(,,,), V(,,,), \
V(CF,,,8A), V(,F9,F9,E9), V(,,,), V(,7F,7F,FE), \
V(F0,,,A0), V(,3C,3C,), V(BA,9F,9F,), V(E3,A8,A8,4B), \
V(F3,,,A2), V(FE,A3,A3,5D), V(C0,,,), V(8A,8F,8F,), \
V(AD,,,3F), V(BC,9D,9D,), V(,,,), V(,F5,F5,F1), \
V(DF,BC,BC,), V(C1,B6,B6,), V(,DA,DA,AF), V(,,,), \
V(,,,), V(1A,FF,FF,E5), V(0E,F3,F3,FD), V(6D,D2,D2,BF), \
V(4C,CD,CD,), V(,0C,0C,), V(,,,), V(2F,EC,EC,C3), \
V(E1,5F,5F,BE), V(A2,,,), V(CC,,,), V(,,,2E), \
V(,C4,C4,), V(F2,A7,A7,), V(,7E,7E,FC), V(,3D,3D,7A), \
V(AC,,,C8), V(E7,5D,5D,BA), V(2B,,,), V(,,,E6), \
V(A0,,,C0), V(,,,), V(D1,4F,4F,9E), V(7F,DC,DC,A3), \
V(,,,), V(7E,2A,2A,), V(AB,,,3B), V(,,,0B), \
V(CA,,,8C), V(,EE,EE,C7), V(D3,B8,B8,6B), V(3C,,,), \
V(,DE,DE,A7), V(E2,5E,5E,BC), V(1D,0B,0B,), V(,DB,DB,AD), \
V(3B,E0,E0,DB), V(,,,), V(4E,3A,3A,), V(1E,0A,0A,), \
V(DB,,,), V(0A,,,0C), V(6C,,,), V(E4,5C,5C,B8), \
V(5D,C2,C2,9F), V(6E,D3,D3,BD), V(EF,AC,AC,), V(A6,,,C4), \
V(A8,,,), V(A4,,,), V(,E4,E4,D3), V(8B,,,F2), \
V(,E7,E7,D5), V(,C8,C8,8B), V(,,,6E), V(B7,6D,6D,DA), \
V(8C,8D,8D,), V(,D5,D5,B1), V(D2,4E,4E,9C), V(E0,A9,A9,), \
V(B4,6C,6C,D8), V(FA,,,AC), V(,F4,F4,F3), V(,EA,EA,CF), \
V(AF,,,CA), V(8E,7A,7A,F4), V(E9,AE,AE,), V(,,,), \
V(D5,BA,BA,6F), V(,,,F0), V(6F,,,4A), V(,2E,2E,5C), \
V(,1C,1C,), V(F1,A6,A6,), V(C7,B4,B4,), V(,C6,C6,), \
V(,E8,E8,CB), V(7C,DD,DD,A1), V(9C,,,E8), V(,1F,1F,3E), \
V(DD,4B,4B,), V(DC,BD,BD,), V(,8B,8B,0D), V(,8A,8A,0F), \
V(,,,E0), V(,3E,3E,7C), V(C4,B5,B5,), V(AA,,,CC), \
V(D8,,,), V(,,,), V(,F6,F6,F7), V(,0E,0E,1C), \
V(A3,,,C2), V(5F,,,6A), V(F9,,,AE), V(D0,B9,B9,), \
V(,,,), V(,C1,C1,), V(,1D,1D,3A), V(B9,9E,9E,), \
V(,E1,E1,D9), V(,F8,F8,EB), V(B3,,,2B), V(,,,), \
V(BB,,,D2), V(,D9,D9,A9), V(,8E,8E,), V(A7,,,), \
V(B6,9B,9B,2D), V(,1E,1E,3C), V(,,,), V(,E9,E9,C9), \
V(,CE,CE,), V(FF,,,AA), V(,,,), V(7A,DF,DF,A5), \
V(8F,8C,8C,), V(F8,A1,A1,), V(,,,), V(,0D,0D,1A), \
V(DA,BF,BF,), V(,E6,E6,D7), V(C6,,,), V(B8,,,D0), \
V(C3,,,), V(B0,,,), V(,2D,2D,5A), V(,0F,0F,1E), \
V(CB,B0,B0,7B), V(FC,,,A8), V(D6,BB,BB,6D), V(3A,,,2C) #define V(a,b,c,d) 0x##a##b##c##d
static const unsigned int FT0[] = { FT };
#undef V #define V(a,b,c,d) 0x##b##c##d##a
static const unsigned int FT1[] = { FT };
#undef V #define V(a,b,c,d) 0x##c##d##a##b
static const unsigned int FT2[] = { FT };
#undef V #define V(a,b,c,d) 0x##d##a##b##c
static const unsigned int FT3[] = { FT };
#undef V #undef FT /*
* Reverse S-box
*/
static const unsigned char RSb[] =
{
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
}; /*
* Reverse tables
*/
#define RT \
\
V(,A7,F4,), V(,,,7E), V(C3,A4,,1A), V(,5E,,3A), \
V(CB,6B,AB,3B), V(F1,,9D,1F), V(AB,,FA,AC), V(,,E3,4B), \
V(,FA,,), V(F6,6D,,AD), V(,,CC,), V(,4C,,F5), \
V(FC,D7,E5,4F), V(D7,CB,2A,C5), V(,,,), V(8F,A3,,B5), \
V(,5A,B1,DE), V(,1B,BA,), V(,0E,EA,), V(E1,C0,FE,5D), \
V(,,2F,C3), V(,F0,4C,), V(A3,,,8D), V(C6,F9,D3,6B), \
V(E7,5F,8F,), V(,9C,,), V(EB,7A,6D,BF), V(DA,,,), \
V(2D,,BE,D4), V(D3,,,), V(,,E0,), V(,C8,C9,8E), \
V(6A,,C2,), V(,,8E,F4), V(6B,3E,,), V(DD,,B9,), \
V(B6,4F,E1,BE), V(,AD,,F0), V(,AC,,C9), V(B4,3A,CE,7D), \
V(,4A,DF,), V(,,1A,E5), V(,,,), V(,7F,,), \
V(E0,,,B1), V(,AE,6B,BB), V(1C,A0,,FE), V(,2B,,F9), \
V(,,,), V(,FD,,8F), V(,6C,DE,), V(B7,F8,7B,), \
V(,D3,,AB), V(E2,,4B,), V(,8F,1F,E3), V(2A,AB,,), \
V(,,EB,B2), V(,C2,B5,2F), V(9A,7B,C5,), V(A5,,,D3), \
V(F2,,,), V(B2,A5,BF,), V(BA,6A,,), V(5C,,,ED), \
V(2B,1C,CF,8A), V(,B4,,A7), V(F0,F2,,F3), V(A1,E2,,4E), \
V(CD,F4,DA,), V(D5,BE,,), V(1F,,,D1), V(8A,FE,A6,C4), \
V(9D,,2E,), V(A0,,F3,A2), V(,E1,8A,), V(,EB,F6,A4), \
V(,EC,,0B), V(AA,EF,,), V(,9F,,5E), V(,,6E,BD), \
V(F9,8A,,3E), V(3D,,DD,), V(AE,,3E,DD), V(,BD,E6,4D), \
V(B5,8D,,), V(,5D,C4,), V(6F,D4,,), V(FF,,,), \
V(,FB,,), V(,E9,BD,D6), V(CC,,,), V(,9E,D9,), \
V(BD,,E8,B0), V(,8B,,), V(,5B,,E7), V(DB,EE,C8,), \
V(,0A,7C,A1), V(E9,0F,,7C), V(C9,1E,,F8), V(,,,), \
V(,,,), V(,ED,2B,), V(AC,,,1E), V(4E,,5A,6C), \
V(FB,FF,0E,FD), V(,,,0F), V(1E,D5,AE,3D), V(,,2D,), \
V(,D9,0F,0A), V(,A6,5C,), V(D1,,5B,9B), V(3A,2E,,), \
V(B1,,0A,0C), V(0F,E7,,), V(D2,,EE,B4), V(9E,,9B,1B), \
V(4F,C5,C0,), V(A2,,DC,), V(,4B,,5A), V(,1A,,1C), \
V(0A,BA,,E2), V(E5,2A,A0,C0), V(,E0,,3C), V(1D,,1B,), \
V(0B,0D,,0E), V(AD,C7,8B,F2), V(B9,A8,B6,2D), V(C8,A9,1E,), \
V(,,F1,), V(4C,,,AF), V(BB,DD,,EE), V(FD,,7F,A3), \
V(9F,,,F7), V(BC,F5,,5C), V(C5,3B,,), V(,7E,FB,5B), \
V(,,,8B), V(DC,C6,,CB), V(,FC,ED,B6), V(,F1,E4,B8), \
V(CA,DC,,D7), V(,,,), V(,,,), V(,,C6,), \
V(7D,,4A,), V(F8,3D,BB,D2), V(,,F9,AE), V(6D,A1,,C7), \
V(4B,2F,9E,1D), V(F3,,B2,DC), V(EC,,,0D), V(D0,E3,C1,), \
V(6C,,B3,2B), V(,B9,,A9), V(FA,,,), V(,,E9,), \
V(C4,8C,FC,A8), V(1A,3F,F0,A0), V(D8,2C,7D,), V(EF,,,), \
V(C7,4E,,), V(C1,D1,,D9), V(FE,A2,CA,8C), V(,0B,D4,), \
V(CF,,F5,A6), V(,DE,7A,A5), V(,8E,B7,DA), V(A4,BF,AD,3F), \
V(E4,9D,3A,2C), V(0D,,,), V(9B,CC,5F,6A), V(,,7E,), \
V(C2,,8D,F6), V(E8,B8,D8,), V(5E,F7,,2E), V(F5,AF,C3,), \
V(BE,,5D,9F), V(7C,,D0,), V(A9,2D,D5,6F), V(B3,,,CF), \
V(3B,,AC,C8), V(A7,7D,,), V(6E,,9C,E8), V(7B,BB,3B,DB), \
V(,,,CD), V(F4,,,6E), V(,B7,9A,EC), V(A8,9A,4F,), \
V(,6E,,E6), V(7E,E6,FF,AA), V(,CF,BC,), V(E6,E8,,EF), \
V(D9,9B,E7,BA), V(CE,,6F,4A), V(D4,,9F,EA), V(D6,7C,B0,), \
V(AF,B2,A4,), V(,,3F,2A), V(,,A5,C6), V(C0,,A2,), \
V(,BC,4E,), V(A6,CA,,FC), V(B0,D0,,E0), V(,D8,A7,), \
V(4A,,,F1), V(F7,DA,EC,), V(0E,,CD,7F), V(2F,F6,,), \
V(8D,D6,4D,), V(4D,B0,EF,), V(,4D,AA,CC), V(DF,,,E4), \
V(E3,B5,D1,9E), V(1B,,6A,4C), V(B8,1F,2C,C1), V(7F,,,), \
V(,EA,5E,9D), V(5D,,8C,), V(,,,FA), V(2E,,0B,FB), \
V(5A,1D,,B3), V(,D2,DB,), V(,,,E9), V(,,D6,6D), \
V(8C,,D7,9A), V(7A,0C,A1,), V(8E,,F8,), V(,3C,,EB), \
V(EE,,A9,CE), V(,C9,,B7), V(ED,E5,1C,E1), V(3C,B1,,7A), \
V(,DF,D2,9C), V(3F,,F2,), V(,CE,,), V(BF,,C7,), \
V(EA,CD,F7,), V(5B,AA,FD,5F), V(,6F,3D,DF), V(,DB,,), \
V(,F3,AF,CA), V(3E,C4,,B9), V(2C,,,), V(5F,,A3,C2), \
V(,C3,1D,), V(0C,,E2,BC), V(8B,,3C,), V(,,0D,FF), \
V(,,A8,), V(DE,B3,0C,), V(9C,E4,B4,D8), V(,C1,,), \
V(,,CB,7B), V(,B6,,D5), V(,5C,6C,), V(,,B8,D0) #define V(a,b,c,d) 0x##a##b##c##d
static const unsigned int RT0[] = { RT };
#undef V #define V(a,b,c,d) 0x##b##c##d##a
static const unsigned int RT1[] = { RT };
#undef V #define V(a,b,c,d) 0x##c##d##a##b
static const unsigned int RT2[] = { RT };
#undef V #define V(a,b,c,d) 0x##d##a##b##c
static const unsigned int RT3[] = { RT };
#undef V #undef RT /*
* Round constants
*/
static const unsigned int RCON[] =
{
0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x0000001B, 0x00000036
}; #define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
{ \
X0 = *RK++ ^ FT0[ ( Y0 ) & 0xFF ] ^ \
FT1[ ( Y1 >> ) & 0xFF ] ^ \
FT2[ ( Y2 >> ) & 0xFF ] ^ \
FT3[ ( Y3 >> ) & 0xFF ]; \
\
X1 = *RK++ ^ FT0[ ( Y1 ) & 0xFF ] ^ \
FT1[ ( Y2 >> ) & 0xFF ] ^ \
FT2[ ( Y3 >> ) & 0xFF ] ^ \
FT3[ ( Y0 >> ) & 0xFF ]; \
\
X2 = *RK++ ^ FT0[ ( Y2 ) & 0xFF ] ^ \
FT1[ ( Y3 >> ) & 0xFF ] ^ \
FT2[ ( Y0 >> ) & 0xFF ] ^ \
FT3[ ( Y1 >> ) & 0xFF ]; \
\
X3 = *RK++ ^ FT0[ ( Y3 ) & 0xFF ] ^ \
FT1[ ( Y0 >> ) & 0xFF ] ^ \
FT2[ ( Y1 >> ) & 0xFF ] ^ \
FT3[ ( Y2 >> ) & 0xFF ]; \
} #define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \
{ \
X0 = *RK++ ^ RT0[ ( Y0 ) & 0xFF ] ^ \
RT1[ ( Y3 >> ) & 0xFF ] ^ \
RT2[ ( Y2 >> ) & 0xFF ] ^ \
RT3[ ( Y1 >> ) & 0xFF ]; \
\
X1 = *RK++ ^ RT0[ ( Y1 ) & 0xFF ] ^ \
RT1[ ( Y0 >> ) & 0xFF ] ^ \
RT2[ ( Y3 >> ) & 0xFF ] ^ \
RT3[ ( Y2 >> ) & 0xFF ]; \
\
X2 = *RK++ ^ RT0[ ( Y2 ) & 0xFF ] ^ \
RT1[ ( Y1 >> ) & 0xFF ] ^ \
RT2[ ( Y0 >> ) & 0xFF ] ^ \
RT3[ ( Y3 >> ) & 0xFF ]; \
\
X3 = *RK++ ^ RT0[ ( Y3 ) & 0xFF ] ^ \
RT1[ ( Y2 >> ) & 0xFF ] ^ \
RT2[ ( Y1 >> ) & 0xFF ] ^ \
RT3[ ( Y0 >> ) & 0xFF ]; \
} void aes_setkey_enc( aes_context* ctx, const unsigned char* key, int keysize )
{
int i;
unsigned int* RK; switch ( keysize )
{
case :
ctx->nr = ;
break;
case :
ctx->nr = ;
break;
case :
ctx->nr = ;
break;
default :
keysize = ;
ctx->nr = ;
break;
} ctx->rk = RK = ctx->buf; memcpy( RK, key, keysize >> ); switch ( ctx->nr )
{
case :
for ( i = ; i < ; i++, RK += )
{
RK[] = RK[] ^ RCON[i] ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] ) & 0xFF ] << ); RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
}
break;
case :
for ( i = ; i < ; i++, RK += )
{
RK[] = RK[] ^ RCON[i] ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] ) & 0xFF ] << ); RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
}
break;
case :
for ( i = ; i < ; i++, RK += )
{
RK[] = RK[] ^ RCON[i] ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] ) & 0xFF ] << ); RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[]; RK[] = RK[] ^
( ( unsigned int ) FSb[ ( RK[] ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( RK[] >> ) & 0xFF ] << ); RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
RK[] = RK[] ^ RK[];
}
break;
}
} void aes_setkey_dec( aes_context* ctx, const unsigned char* key, int keysize )
{
int i, j;
aes_context cty;
unsigned int* RK;
unsigned int* SK; ctx->rk = RK = ctx->buf; aes_setkey_enc( &cty, key, keysize );
ctx->nr = cty.nr;
SK = cty.rk + ( cty.nr << ); memcpy( RK, SK, sizeof( unsigned int ) << );
RK += ;
SK -= ; for ( i = ctx->nr - ; i > ; i--, SK -= )
{
for ( j = ; j < ; j++, SK++ )
{
*RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^
RT1[ FSb[ ( *SK >> ) & 0xFF ] ] ^
RT2[ FSb[ ( *SK >> ) & 0xFF ] ] ^
RT3[ FSb[ ( *SK >> ) & 0xFF ] ];
}
} memcpy( RK, SK, sizeof( unsigned int ) << );
} void aes_crypt_ecb_update( aes_context* ctx, int mode, const unsigned char input[], unsigned char output[] )
{
int i;
unsigned int* RK;
unsigned int X[], Y[]; RK = ctx->rk; for ( i = ; i < ; i ++ )
{
X[i] = ( *( ( unsigned int* )input + i ) ) ^ ( *RK++ );
} if ( mode == AES_DECRYPT )
{
for ( i = ( ctx->nr >> ) - ; i > ; i-- )
{
AES_RROUND( Y[], Y[], Y[], Y[], X[], X[], X[], X[] );
AES_RROUND( X[], X[], X[], X[], Y[], Y[], Y[], Y[] );
} AES_RROUND( Y[], Y[], Y[], Y[], X[], X[], X[], X[] ); X[] = *RK++ ^ \
( ( unsigned int ) RSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ); X[] = *RK++ ^ \
( ( unsigned int ) RSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ); X[] = *RK++ ^ \
( ( unsigned int ) RSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ); X[] = *RK++ ^ \
( ( unsigned int ) RSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) RSb[ ( Y[] >> ) & 0xFF ] << );
}
else /* AES_ENCRYPT */
{
for ( i = ( ctx->nr >> ) - ; i > ; i-- )
{
AES_FROUND( Y[], Y[], Y[], Y[], X[], X[], X[], X[] );
AES_FROUND( X[], X[], X[], X[], Y[], Y[], Y[], Y[] );
} AES_FROUND( Y[], Y[], Y[], Y[], X[], X[], X[], X[] ); X[] = *RK++ ^ \
( ( unsigned int ) FSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ); X[] = *RK++ ^ \
( ( unsigned int ) FSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ); X[] = *RK++ ^ \
( ( unsigned int ) FSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ); X[] = *RK++ ^ \
( ( unsigned int ) FSb[ ( Y[] ) & 0xFF ] ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << ) ^
( ( unsigned int ) FSb[ ( Y[] >> ) & 0xFF ] << );
} memcpy( output, X, );
} // free later
unsigned char* aes_crypt_ecb( aes_context* ctx, int mode, const unsigned char* input, int slen, int* dlen )
{
register int i;
register int n;
unsigned char* output;
unsigned char buff[]; if ( mode == AES_ENCRYPT )
{
n = - ( slen & );
*dlen = slen + n; output = ( unsigned char* )malloc( *dlen );
if ( !output )
{
return NULL;
} memset( buff, n, sizeof( buff ) );
if ( slen & )
{
memcpy( buff, input + ( slen & ~ ), slen & );
} n = ( slen >> );
for ( i = ; i < n; i ++ )
{
aes_crypt_ecb_update( ctx, AES_ENCRYPT, \
input + ( i << ), output + ( i << ) );
}
aes_crypt_ecb_update( ctx, AES_ENCRYPT, buff, output + ( i << ) );
}
else
{
output = ( unsigned char* )malloc( slen );
if ( !output )
{
return NULL;
} n = ( slen >> );
for ( i = ; i < n; i ++ )
{
aes_crypt_ecb_update( ctx, AES_DECRYPT, \
input + ( i << ), output + ( i << ) );
} *dlen = slen - ( int )output[slen - ];
} return output;
}

与BASE64经常一起用

 #include "base64.h"

 static const unsigned char base64_enc_map[] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '', '', '', '', '', '', '', '',
'', '', '+', '/'
}; static const unsigned char base64_dec_map[] =
{
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , , , , ,
, , , , , , ,
}; int base64_encode( char* dst, int* dlen, const unsigned char* src, int slen )
{
int i, n;
int C1, C2, C3;
char* p; if ( slen == )
{
return ;
} n = ( slen << ) / ; switch ( ( slen << ) - ( n * ) )
{
case :
n += ;
break;
case :
n += ;
break;
default:
break;
} if ( *dlen < n + )
{
*dlen = n + ;
return -;
} n = ( slen / ) * ; for ( i = , p = dst; i < n; i += )
{
C1 = *src++;
C2 = *src++;
C3 = *src++; *p++ = base64_enc_map[( C1 >> ) & 0x3F];
*p++ = base64_enc_map[( ( ( C1 & ) << ) + ( C2 >> ) ) & 0x3F];
*p++ = base64_enc_map[( ( ( C2 & ) << ) + ( C3 >> ) ) & 0x3F];
*p++ = base64_enc_map[C3 & 0x3F];
} if ( i < slen )
{
C1 = *src++;
C2 = ( ( i + ) < slen ) ? *src++ : ; *p++ = base64_enc_map[( C1 >> ) & 0x3F];
*p++ = base64_enc_map[( ( ( C1 & ) << ) + ( C2 >> ) ) & 0x3F]; if ( ( i + ) < slen )
{
*p++ = base64_enc_map[( ( C2 & ) << ) & 0x3F];
}
else
{
*p++ = '=';
} *p++ = '=';
} *dlen = p - dst;
*p = ; return ;
} int base64_decode( unsigned char* dst, int* dlen, const char* src, int slen )
{
int i, j, n;
unsigned int x;
unsigned char* p; if ( slen == )
{
return ;
} for ( i = j = n = ; i < slen; i++ )
{
if ( ( slen - i ) >= &&
src[i] == '\r' && src[i + ] == '\n' )
{
continue;
} if ( src[i] == '\n' )
{
continue;
} if ( src[i] == '=' && ++j > )
{
return -;
} if ( src[i] < || base64_dec_map[( int )src[i]] == )
{
return -;
} if ( base64_dec_map[( int )src[i]] < && j != )
{
return -;
} n++;
} if ( n == )
{
return ;
} n = ( ( n * ) + ) >> ; if ( *dlen < n )
{
*dlen = n;
return -;
} for ( j = , n = x = , p = dst; i > ; i--, src++ )
{
if ( *src == '\r' || *src == '\n' )
{
continue;
} j -= ( base64_dec_map[( int ) * src] == );
x = ( x << ) | ( base64_dec_map[( int ) * src] & 0x3F ); if ( ++ n == )
{
n = ; if ( j > ) *p++ = ( unsigned char )( x >> ); if ( j > ) *p++ = ( unsigned char )( x >> ); if ( j > ) *p++ = ( unsigned char )( x );
}
} *dlen = p - dst; return ;
}

C写的AES(ECB/PKCS5Padding)的更多相关文章

  1. php中签名公钥、私钥(SHA1withRSA签名)以及AES(AES/ECB/PKCS5Padding)加密解密详解

    由于http请求是无状态,所以我们不知道请求方到底是谁.于是就诞生了签名,接收方和请求方协商一种签名方式进行验证,来取得互相信任,进行下一步业务逻辑交流. 其中签名用得很多的就是公钥私钥,用私钥签名, ...

  2. Python3 AES加解密(AES/ECB/PKCS5Padding)

    class AesEncry(object): key = "wwwwwwwwwwwwwwww" # aes秘钥 def encrypt(self, data): data = j ...

  3. 使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密

    东篱 使用 PyCrypto 进行 AES/ECB/PKCS#5(7) 加密 2013/06/05 · tech PyCrypto 是流行的 Python 加密/解密库.但是其 AES 的 ECB 模 ...

  4. golang AES/ECB/PKCS5 加密解密 url-safe-base64

    因为项目的需要用到golang的一种特殊的加密解密算法AES/ECB/PKCS5,但是算法并没有包含在标准库中,经过多次失败的尝试,终于解码成功,特此分享: /* 描述 : golang AES/EC ...

  5. 关于“Cannot find any provider supporting AES/ECB/PKCS7Padding”问题的解决方案

    出现这个问题的原因是:java自带的是PKCS5Padding填充,不支持PKCS7Padding填充 参考:https://stackoverflow.com/questions/20770072/ ...

  6. AES ECB PKCS5/PKCS7 加解密 python实现 支持中文

    目录 ECB模式介绍 pkcs5padding和pkcs7padding的区别 python实现 注意事项 ECB模式介绍 电码本模式(Electronic Codebook Book (ECB) 这 ...

  7. JAVA AES CBC PKCS5Padding加解密

    package com.hzxc.groupactivity.util; /** * Created by hdwang on 2019/1/17. */ import org.slf4j.Logge ...

  8. Java DESede 加解密("DESede/ECB/PKCS5Padding")

    private static final Cipher DES_CIPHER; static { try { DES_CIPHER = Cipher.getInstance("DESede/ ...

  9. php实现AES/CBC/PKCS5Padding加密解密(又叫:对称加密)

    今天在做一个和java程序接口的架接,java那边需要我这边(PHP)对传过去的值进行AES对称加密,接口返回的结果也是加密过的(就要用到解密),然后试了很多办法,也一一对应了AES的key密钥值,偏 ...

随机推荐

  1. 如何的退出无响应的 SSH 连接

    ~. 具体操作是Shift+-,然后松开按.. tips如果无效,可以先按下Enter,然后进行上面的操作.

  2. tflearn alexnet iter 10

    他会自己下载数据: # -*- coding: utf-8 -*- """ AlexNet. Applying 'Alexnet' to Oxford's 17 Cate ...

  3. java问题解读,String类为什么是final的

    一.理解final 望文生义,final意为“最终的,最后的”,我理解为“不能被改变的”,它可以修饰类.变量和方法. 所以我是否可以理解为被它所修饰的类.变量和方法都不能被改变呢?答案是”是“,因为有 ...

  4. P4111 [HEOI2015]小Z的房间 生成树计数

    这个题是生成树计数的裸题,中间构造基尔霍夫矩阵,然后构成行列式,再用高斯消元就行了.这里高斯消元有一些区别,交换两行行列式的值变号,且消元只能将一行的数 * k 之后加到别的行上. 剩下就没啥了... ...

  5. 让谷歌浏览器(chrome)保存调试代码workspace

    方法/步骤  chrome浏览器早期版本的操作方法与我现在要讲的方法有所不同,因此操作前请注意浏览器的版本号. 示例中的版本号: 53.0.2785.116 m  任意打开一个需要调试的html文件 ...

  6. js 计算时间差

    function GetDateDiff(startDate,endDate) { var startTime = new (<any>Date)(Date.parse(startDate ...

  7. Unity Toast插件(UGUI版)

    简介 介于自己之前经历的一些开发过程,非常希望unity能有类似Toast的功能用于一些简单的信息显示.但是找来找去找了半天,都木有发现,实在是憋不住了,自己写了个,感觉还可以用,发出来共享一下... ...

  8. 【Spring】IOC

    浅谈IOC IOC的理论背景 图1:传统系统中,对象之间相互引用的一幅图,在采用面向对象方法设计的软件系统中,它的底层的实现都是由n个对象所组成的,所有的对象通彼此之间的合作最终实现系统的业务逻辑,如 ...

  9. 【1】Jdk1.8中的HashMap实现原理

    HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提供所有可选的映射操作,并允许使用null值和null键.此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 内部实现 ...

  10. CF414B Mashmokh and ACM

    思路: dp. 实现: 1.O(n5/2) #include <iostream> #include <cstdio> using namespace std; ; ][]; ...