How to do AES-128 bit CBC mode encryption in c programming code with OpenSSL

Here is the simple “How to do AES-128 bit CBC mode encryption in c programming code with OpenSSL”

First you need to download standard cryptography library called OpenSSL to perform robust AES(Advanced Encryption Standard) encryption, But before that i will tell you to take a look at simple C code for AES encryption and decryption, so that you are familiar with AES cryptography APIs which is quite simple. Here i use AES-128 bit CBC mode Encryption, where 128 bit is AES key length. We can also use 192 and 256 bit AES key for encryption in which size and length of key is increased with minor modification in following code.



Compiling and Installing OpenSSL

Before compiling this code, you need OpenSSL library which you can download from here

i am using openssl-1.0.1i which i have downloaded in form of tar file because my development OS is Linux(Ubuntu). So after downloading tar file we have to compile and install OpenSSL. To do so follow instruction below.


that’s it ! you have successfully compiled and installed OpenSSL.

Now create the file with above sample code and compile that with gcc compiler by using this gcc directives :


and run the binary you will see following output


Original     : 00 01 02 03 04 05

Encrypted : D5 40 D0 BB 16 1D

Decrypted : 00 01 02 03 04 05



More Reading

Suggested Reading

  1. How to do Triple-DES CBC mode encryption example in c programming with OpenSSL


  • Вася Пупкин

    Error in example

    enc_out must be greater size because AES_cbc_encrypt padding bytes.