QuickCrypt Library API

The following sample demonstrates how to use the Rijndael (AES) and CBC API to perform encryption/decryption of a memory buffer in CBC mode of operation.

#include <stdio.h>
#include <QuickCrypt.h>

/* performs Rijndael (AES) encryption/decryption of memory buffer in CBC mode */
int EncryptDecryptBuffer( unsigned char* buff, const unsigned int buffsize, int dir, const unsigned char* iv, const unsigned char* key )
{
    /* dir: SLC_ENCRYPT - perform encryption */
    /*      SLC_DECRYPT - perform decryption */

    /***** Initialize context ****************/

    unsigned char context[SLC_RIJNDAEL_CONTEXTSIZE];
    int padded = 1; /* true */

    SL_RIJNDAEL_Init( context, dir, key, SLC_RIJNDAEL_DEFAULTKEYSIZE );
    SL_CBC_Init( context, dir, iv, padded );

    /***** Encrypt/Decrypt memory buffer *****/

    return SL_CBC_Process( context, buff, buff, buffsize ); /* in-place encryption/decryption */
}

void ShowBuffer( const unsigned char* buff, const unsigned int buffsize );

int main()
{
    /***** Define key, iv, memory buffer *****/

    char key[SLC_RIJNDAEL_DEFAULTKEYSIZE] = 
    {
        's', 'e', 'c', 'r', 'e', 't', 'l', 'y',
        '9', '6', '7', '1', '2', '9', '2', '7'
    };

    unsigned char iv[SLC_RIJNDAEL_BLOCKSIZE] = 
    {
        0x41, 0x3E, 0xF0, 0xA1, 0xC6, 0x11, 0xE5, 0x50,
        0x35, 0x8E, 0x7C, 0x36, 0x20, 0xF4, 0xA1, 0x77
    };

    unsigned char buff[100] = 
    {
        0xA0, 0x76, 0xEE, 0x97, 0x8F, 0x12, 0x2C, 0x6F, 
        0xAA, 0x86, 0xC8, 0x13, 0x80, 0x80, 0xEA, 0xC4, 
        0x6B, 0xF0, 0x0D, 0x4A, 0x6A, 0xE7, 0x6E, 0x09, 
        0x12, 0x8B, 0xFE
    };

    unsigned int buffsize = 27;

    printf( "Initial memory buffer:\n" );
    ShowBuffer( buff, buffsize );

    /***** Encrypt memory buffer *************/
    
    buffsize = EncryptDecryptBuffer( buff, buffsize, SLC_ENCRYPT, iv, (const unsigned char*)key );

    printf( "Encrypted memory buffer:\n" );
    ShowBuffer( buff, buffsize );

    /***** Decrypt memory buffer *************/

    buffsize = EncryptDecryptBuffer( buff, buffsize, SLC_DECRYPT, iv, (const unsigned char*)key );

    printf( "Decrypted memory buffer:\n" );
    ShowBuffer( buff, buffsize );

    return 0;
}

void ShowBuffer( const unsigned char* buff, const unsigned int buffsize )
{
    unsigned int i = 0;

    for( ; i < buffsize; ++i )
    {
        printf( "%.2x ", (int)buff[ i ] );

        if( ( i + 1 ) % SLC_RIJNDAEL_BLOCKSIZE == 0 )
            printf( "\n" );
    }

    printf( "\n" );
}

 


 

 

Send Feedback to SlavaSoft Inc. Tell a friend about QuickCrypt Library