Home    | Products    | Downloads    | Purchase    | Support   

 Products

 Paint Express

 PrivyPad

 HashCalc

 FSUM

 QuickCrypt Library

    Download

    Purchase

    Samples

    License Agreement

    Related Links

    F.A.Q.

    Overview

 QuickHash Library

 FastCRC Library

 Company

 About Us

 Contact Us

 Miscellaneous

 Affiliate Program

 Site Map

SlavaSoft QuickCrypt Library Samples
Sample #7 (QuickCrypt API - C)  

All

Previous Next


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

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

/* performs Blowfish 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_BLOWFISH_CONTEXTSIZE];

    SL_BLOWFISH_Init( context, dir, key, SLC_BLOWFISH_DEFAULTKEYSIZE );

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

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

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

    char key[SLC_BLOWFISH_DEFAULTKEYSIZE] =
    {
        's', 'e', 'c', 'r', 'e', 't', 'l', 'y',
        '9', '6', '7', '1', '2', '9', '2', '7',
        '0', '1', '2', '2', '0', '0', '5', '7',
        '*', '-', '*', '-', '*', '-', '*', '-',
        '9', '6', '7', '1', '2', '9', '2', '7',
        '0', '1', '2', '2', '0', '0', '5', '7',
        '*', '-', '*', '-', '*', '-', '*', '-'
    };

    const unsigned int NUM_BLOCKS = 3;

    unsigned char blocks[24] = /* SLC_BLOWFISH_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_BLOWFISH_BLOCKSIZE; ++j )
        {
            printf( "%.2x ", (int)buff[ i * SLC_BLOWFISH_BLOCKSIZE + j ] );
        }

        printf( "\n" );
    }
}
 
  Copyright © SlavaSoft Inc. All rights reserved.