Click here to Skip to main content
13,197,271 members (47,914 online)
Click here to Skip to main content
Add your own
alternative version


86 bookmarked
Posted 9 Feb 2010

A Fast and Easy to Use AES Library

, 28 Dec 2011
Rate this:
Please Sign up or sign in to vote.
A fast and easy to use AES library.


EfAesLib is a highly optimized Advanced Encryption Standard (AES) library for the Windows platform 32-bit architecture. The Extreme Fast AES Library is implemented based on the official document:

The library is actually my personal work. I have decided to put it in the public domain and make it free. The size is a little on the higher side because of some optimization to use space in exchange of time.

I have provided the compiled DLL in VS2008, and the project files; or you can use the source in any other platform, it is just plain 'C'.

Using the code

AES is a 128-bit block encrypt/decrypt algorithm. That means you need to carefully handle the last block which is not 16 bytes aligned. Otherwise, you might be unable to decrypt correctly.

There are many block modes defined in the cipher realm. Different block modes have different characteristics. For example, the CRT mode only needs encryption logic, so it is suitable for low cost hardware implementations. The PCBC mode provides better error propagation. As for CFB, OFB modes, there is an extra parameter: 'feedback size'. You can treat it as the result size of each AES block process. That means, CFB 8-bits mode should be about 16 times slower than CFB 128-bits mode. And also, you can do stream ciphers by using the CFB 8-bits mode.

You can reference the EfAesLib.pdf in the package for details about how the different block modes work.

Encode/Decode with same processNeed Initial VectorChain process

























AES always needs a 128-bit key to encrypt/decrypt. But it is also combined with an initial vector to work with, except in ECB mode. Each bit of the initial vector you use will double the possibilities of encrypted text from a given plain text, which means more safety.

EfAesLib supports ECB, CBC, PCBC, OFB, CFB, CRT block modes, and support OFB,CFB mode with [1..16] bytes feedback size. It also supports in-place encryption/decryption in each mode (source and destination buffer are the same).

The following sample uses Counter mode to encode a file:

#include "EfAes.h"
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h> 
int main(int argc , char * argv[])
    unsigned char key[16]={
    unsigned char vector[16]={
    unsigned char buff[4096];
    int rd_fd,wr_fd, rdsz;
    AesCtx context;
    AesSetKey( &context , AES_KEY_128BIT ,BLOCKMODE_CRT, key , vector );
    rd_fd = open("test.dat", O_RDONLY);
    wr_fd = open("test.encoded",O_WRONLY | O_CREAT);
    while( (rdsz = read(rd_fd, buff ,4096)) > 0 )
      // before last block , the block size
      // should always be the multiply of 16
      // the last block should be handled
      // if the size is not a multiply of 16
      AesEncryptCRT(&context , buff, buff, rdsz );
      rdsz = AesRoundSize( rdsz, 16);
      write( wr_fd , buff , rdsz );

The use of the AesCtx structure is mainly designed for thread issues. Each encryption session should have its own AesCtx. The EfAesLib APIs will always pad 0 to input data whose size is not a multiple of 16, or a multiple of the feedback size in the CFB, OFB modes.


There are pre-defined functions in the AES algorithm. The first step, also proposed in the Wiki, is to combine SubBytes, ShiftRows with MixColumns. The follow is my sample implementation:

void SubAndShiftAndMixRound(uint8 * pState ,uint32 * pRoundKey , uint32 * pOutput)
    uint32 a1,a2,a3,a4;


    *pOutput++ = 
        ((SboxXTime2[a1] ^ SboxXTime3[a2] ^        FSB[a3] ^        
                                                   FSB[a4])        |
               ((FSB[a1] ^ SboxXTime2[a2] ^ SboxXTime3[a3] ^        
                                            FSB[a4]) << 8)  |
               ((FSB[a1] ^        FSB[a2] ^ SboxXTime2[a3] ^ 
                                  SboxXTime3[a4]) << 16 )|
        ((SboxXTime3[a1] ^        FSB[a2] ^        FSB[a3] ^ 
                                  SboxXTime2[a4]) << 24))^ *pRoundKey++;


In the second step, notice the horizontal direction of a1, a2, a3, a4. We can reduce this by using a pre-build lookup table for each column.

    TestTable1[i]=SboxXTime2[i] | FSB_8[i]        | FSB_16[i]        | SboxXTime3_24[i];
    TestTable2[i]=SboxXTime3[i] | SboxXTime2_8[i] | FSB_16[i]        | FSB_24[i];
    TestTable3[i]=FSB[i]        | SboxXTime3_8[i] | SboxXTime2_16[i] | FSB_24[i];
    TestTable4[i]=FSB[i]        | FSB_8[i]        | SboxXTime3_16[i] | SboxXTime2_24[i];

The code in step one will be optimized to:

void SubAndShiftAndMixRound(uint8 * pState ,uint32 * pRoundKey , uint32 * pOutput)
    uint32 a1,a2,a3,a4;


     *pOutput++ = TestTable1[a1] ^ TestTable2[a2] ^ TestTable3[a3] ^ 
                                   TestTable4[a4] ^ *pRoundKey++;

In the third step, notice a1=pState[0],a2=pState[5],a3=pState[10],a4=pState[15]; it is slow in the 32-bit architecture. We can change it to a 32-bit access and XOR the sequence.


The best performance EfAesLib has is 10M bytes in 78 milliseconds with my Pentium IV 3.0Ghz computer.


The official document:

The Wiki


v2.0: Extended the library to 128/192/256 bits key length, and also added a 64 bit DLL in addition.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Engineer I will tell you after my death.
Taiwan Taiwan
No Biography provided

Comments and Discussions

BugCompilation difficulties Pin
Cameron Caturria30-Jan-16 10:46
memberCameron Caturria30-Jan-16 10:46 
QuestionThere are a number of bugs in version 2.0 Pin
Ben3216-Jan-16 16:00
memberBen3216-Jan-16 16:00 
AnswerRe: There are a number of bugs in version 2.0 Pin
Ben3216-Jan-16 16:22
memberBen3216-Jan-16 16:22 
QuestionWhat's the AesCtx * pContext parameter? Pin
Ben3216-Jan-16 3:36
memberBen3216-Jan-16 3:36 
AnswerRe: What's the AesCtx * pContext parameter? Pin
Ben3216-Jan-16 15:44
memberBen3216-Jan-16 15:44 
Generalspot on. Pin
KenSands29-Oct-14 8:50
memberKenSands29-Oct-14 8:50 
Bugnot "just plain 'C'." Pin
Member 104430953-Dec-13 17:18
memberMember 104430953-Dec-13 17:18 
QuestionGood Pin
microbio756-Oct-13 21:12
membermicrobio756-Oct-13 21:12 
Questionbuggy v2.0 Pin
PepekNamornik1-Dec-12 2:03
memberPepekNamornik1-Dec-12 2:03 
AnswerRe: buggy v2.0 Pin
Ben3216-Jan-16 3:39
memberBen3216-Jan-16 3:39 
GeneralMy vote of 5 Pin
sukumarchandran24-Oct-12 23:20
membersukumarchandran24-Oct-12 23:20 
QuestionAESEncode.cpp seems to be corrupt Pin
RobsterNZ11-Sep-12 13:37
memberRobsterNZ11-Sep-12 13:37 
Hi robertguan,

Thanks for this article - really good work, and especially nice that you've taken the time to provide some documentation!

A couple of comments:
1) I can use your DLL, but I can't compile the project because AESEncode.cpp isn't all there. If you look around line 2487, there's an incomplete line in the declaration of a data table (presumably RevRawTable3). Could you please look into that?

2) In section 3.1 of the PDF, which describes AesSetKey, the comment for pKey is "Pointer to 128 bit key". Should this be changed to "pointer to key where length is determined by AesKeyLength" or something similar?

AnswerRe: AESEncode.cpp seems to be corrupt Pin
microbio756-Oct-13 21:07
membermicrobio756-Oct-13 21:07 
Questionpretty good Pin
chenzhe6220-Apr-12 17:37
memberchenzhe6220-Apr-12 17:37 
QuestionCompilation errors Pin
tinarajraj6-Feb-12 21:25
membertinarajraj6-Feb-12 21:25 
GeneralMy vote of 5 Pin
Monjurul Habib28-Dec-11 6:10
memberMonjurul Habib28-Dec-11 6:10 
Questionaes 256 cbc Pin
zby8472213-Dec-11 5:46
memberzby8472213-Dec-11 5:46 
GeneralExtension to use 256 bit keys Pin
MemberPhillip18-Feb-10 16:24
memberMemberPhillip18-Feb-10 16:24 
GeneralRe: Extension to use 256 bit keys Pin
MemberPhillip18-Feb-10 16:33
memberMemberPhillip18-Feb-10 16:33 
GeneralGood Job ! Pin
m.moestl12-Feb-10 2:00
memberm.moestl12-Feb-10 2:00 
GeneralRe: Good Job ! Pin
robertguan12-Feb-10 4:59
memberrobertguan12-Feb-10 4:59 
GeneralCompleteness Pin
Panic2k310-Feb-10 10:35
memberPanic2k310-Feb-10 10:35 
GeneralRe: Completeness Pin
robertguan10-Feb-10 14:30
memberrobertguan10-Feb-10 14:30 
Generalmy vote of 5 Pin
Rozis9-Feb-10 12:56
memberRozis9-Feb-10 12:56 
GeneralRe: my vote of 5 Pin
robertguan9-Feb-10 19:16
memberrobertguan9-Feb-10 19:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 28 Dec 2011
Article Copyright 2010 by robertguan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid