QuickCrypt Library API

The following sample demonstrates how to use the DES API to perform encryption/decryption of a memory buffer in ECB mode of operation.

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

/* performs DES encryption/decryption of memory buffer in ECB mode */
void EncryptDecryptBuffer( unsigned char* buff, const unsigned int numblocks, int dir, const unsigned char* key )
{
    /* dir: SLC_ENCRYPT - perform encryption */
    /*      SLC_DECRYPT - perform decryption */

    unsigned int i = 0;

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

    unsigned char context[SLC_DES_CONTEXTSIZE];

    SL_DES_Init( context, dir, key, SLC_DES_DEFAULTKEYSIZE );

    /***** Encrypt/Decrypt memory buffer ****/
   
    for( ; i < numblocks; ++i )
    {
        SL_DES_ProcessBlock( context, buff + i * SLC_DES_BLOCKSIZE, buff + i * SLC_DES_BLOCKSIZE ); /* in-place encryption/decryption */
    }
}

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

int main()
{
    /***** Define key & blocks ***************/

    char key[SLC_DES_DEFAULTKEYSIZE] = 
    {
        's', 'e', 'c', 'r', 'e', 't', 'l', 'y'
    };

    const unsigned int NUM_BLOCKS = 3;

    unsigned char blocks[24] = /* SLC_DES_BLOCKSIZE * NUM_BLOCKS */
    {
        0xA6, 0x7E, 0x40, 0x80, 0x0F, 0xCF, 0x24, 0xC5, 
        0x8C, 0x5B, 0x3C, 0x42, 0xDE, 0xB1, 0x11, 0xA0, 
        0x97, 0xE9, 0xA7, 0x87, 0x99, 0x91, 0x1B, 0x3B
    };

    printf( "Initial blocks:\n" );
    ShowBuffer( blocks, NUM_BLOCKS );

    /***** Encrypt blocks ********************/

    EncryptDecryptBuffer( blocks, NUM_BLOCKS, SLC_ENCRYPT, (const unsigned char*)key );

    printf( "Encrypted blocks:\n" );
    ShowBuffer( blocks, NUM_BLOCKS );

    /***** Decrypt blocks ********************/

    EncryptDecryptBuffer( blocks, NUM_BLOCKS, SLC_DECRYPT, (const unsigned char*)key );

    printf( "Decrypted blocks:\n" );
    ShowBuffer( blocks, NUM_BLOCKS );

    return 0;
}

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

    for( ; i < numblocks; ++i )
    {
        unsigned int j = 0;

        for( ; j < SLC_DES_BLOCKSIZE; ++j )
        {
            printf( "%.2x ", (int)buff[ i * SLC_DES_BLOCKSIZE + j ] );
        }

        printf( "\n" );
    }
}

        

 


 

 

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