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 #11 (QuickCrypt API - C)  

All

Previous Next


The following sample demonstrates how to use the Blowfish and CTR (Counter mode) API to perform encryption/decryption of a file in Counter mode of operation.

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

#define BUFF_SIZE  1024

/* performs Blowfish file encryption/decryption in Counter mode */
int EncryptDecryptFile( const char* filenamefrom, const char* filenameto, const unsigned char* iv, const unsigned char* key )
{
    FILE *filefrom, *fileto;
    unsigned char buff[ BUFF_SIZE ];
    unsigned char context[ SLC_BLOWFISH_CONTEXTSIZE ];

    /***** Open files ***********************/

    filefrom = fopen( filenamefrom, "rb" );

    if( filefrom == NULL )
    {
        printf( "\nCould not open file: %s", filenamefrom );
        return 0;
    }

    fileto = fopen( filenameto, "wb" );

    if( fileto == NULL )
    {
        printf( "\nCould not open file: %s", filenameto );
        fclose( filefrom );
        return 0;
    }

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

    SL_BLOWFISH_Init( context, SLC_ENCRYPT, key, SLC_BLOWFISH_DEFAULTKEYSIZE );
    SL_CTR_Init( context, iv );

    /***** Encrypt/Decrypt file **************/

    while( !feof( filefrom ) )
    {
        unsigned int buffsize = fread( buff, sizeof( char ), BUFF_SIZE, filefrom );
        if( ferror( filefrom ) )
        {
            printf( "\nAn error occurred when accessing the file: %s", filenamefrom );
            fclose( filefrom );
            fclose( fileto );
            return 0;
        }

        SL_CTR_Process( context, buff, buff, buffsize ); /* in-place encryption/decryption */

        fwrite( buff, sizeof( char ), buffsize, fileto );
    }

    /***** Close files ***********************/

    fclose( filefrom );
    fclose( fileto );

    return 1;
}

int main()
{
    char buff[10];

    char filename[256];     /* Initial file   */
    char filenameencr[256]; /* Encrypted file */
    char filenamedecr[256]; /* Decrypted file */

    /***** Define key & iv ********************************************************/

    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',
        '*', '-', '*', '-', '*', '-', '*', '-'
    };

    unsigned char iv[SLC_BLOWFISH_BLOCKSIZE] =
    {
        0x41, 0x3E, 0xF0, 0xA1, 0xC6, 0x11, 0xE5, 0x50
    };

    do
    {
        /***** Get file to encrypt (Initial file) *********************************/

        printf( "Enter the full name of the file to encrypt:\n" );
        gets( filename );

        /***** Get file to store the result of encryption (Encrypted file) ********/

        printf( "Enter the full name of the file to store the result of encryption:\n" );
        gets( filenameencr );

        /***** Encrypt file *******************************************************/

        if( EncryptDecryptFile( filename, filenameencr, iv, (const unsigned char*)key ) )
        {
            /***** Get file to store the result of decryption (Decrypted file) ****/

            printf( "Enter the full name of the file to store the result of decryption:\n" );
            gets( filenamedecr );

            /***** Decrypt file ***************************************************/

            EncryptDecryptFile( filenameencr, filenamedecr, iv, (const unsigned char*)key );

            /* At this point compare Initial file with Decrypted file. They must be identical. */
        }

        /***** Continue? **********************************************************/
       
        printf( "\nContinue (Y/N)?" );
        gets( buff );

    } while( *buff == 'Y' || *buff == 'y' );

    return 0;
}
 
  Copyright © SlavaSoft Inc. All rights reserved.