Spread the love

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.

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>

/* AES key for Encryption and Decryption */
const static unsigned char aes_key[]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};

/* Print Encrypted and Decrypted data packets */
void print_data(const char *tittle, const void* data, int len);

int main( )
{
	/* Input data to encrypt */
	unsigned char aes_input[]={0x0,0x1,0x2,0x3,0x4,0x5};
	
	/* Init vector */
	unsigned char iv[AES_BLOCK_SIZE];
	memset(iv, 0x00, AES_BLOCK_SIZE);
	
	/* Buffers for Encryption and Decryption */
	unsigned char enc_out[sizeof(aes_input)];
	unsigned char dec_out[sizeof(aes_input)];
	
	/* AES-128 bit CBC Encryption */
	AES_KEY enc_key, dec_key;
	AES_set_encrypt_key(aes_key, sizeof(aes_key)*8, &enc_key);
	AES_cbc_encrypt(aes_input, enc_out, sizeof(aes_input), &enc_key, iv, AES_ENCRYPT);
	/* AES-128 bit CBC Decryption */
	memset(iv, 0x00, AES_BLOCK_SIZE); // don't forget to set iv vector again, else you can't decrypt data properly
	AES_set_decrypt_key(aes_key, sizeof(aes_key)*8, &dec_key); // Size of key is in bits
	AES_cbc_encrypt(enc_out, dec_out, sizeof(aes_input), &dec_key, iv, AES_DECRYPT);
	
	/* Printing and Verifying */
	print_data("\n Original ",aes_input, sizeof(aes_input)); // you can not print data as a string, because after Encryption its not ASCII
	
	print_data("\n Encrypted",enc_out, sizeof(enc_out));
	
	print_data("\n Decrypted",dec_out, sizeof(dec_out));
	
	return 0;
}

void print_data(const char *tittle, const void* data, int len)
{
	printf("%s : ",tittle);
	const unsigned char * p = (const unsigned char*)data;
	int i = 0;
	
	for (; i<len; ++i)
		printf("%02X ", *p++);
	
	printf("\n");
}

 

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.

/* Go to the folder where your openssl-1.0.1i.tar.gz file is located, type following command : */

tar -xvf openssl-1.0.1i.tar.gz

cd openssl-1.0.1i

./config shared --prefix=<FULL_PATH_OF_INSTALL_DIRECTORY>

make

make install

 

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 :

gcc -o <OUTPUT_BINARY_FILE_NAME> <AES_CODE_FILE_NAME.c> -I <FULL_PATH_TO_INCLUDE_FOLDER_OF_OPENSSL’s_INSTALL_DIRECTORY> -L <FULL_PATH_TO_LIB_FOLDER_OF_OPENSSL’s_INSTALL_DIRECTORY> -lcrypto

and run the binary you will see following output

$ ./<OUTPUT_BINARY_FILE_NAME>

Original     : 00 01 02 03 04 05

Encrypted : D5 40 D0 BB 16 1D

Decrypted : 00 01 02 03 04 05

 

 

More Reading

http://en.wikipedia.org/wiki/Block_cipher_mode_of_operation

Suggested Reading

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