Contents 

QuickCrypt Library
QuickCrypt Library Overview
QuickCrypt Library Installation
Register QuickCrypt Library
DES
CDES ( C++ Interface )
CDES Class Members
DES API ( QuickCrypt API )
DES API Functions
DES-EDE2
CDES_EDE2 ( C++ Interface )
CDES_EDE2 Class Members
DES-EDE2 API ( QuickCrypt API )
DES-EDE2 API Functions
DES-EDE3
CDES_EDE3 ( C++ Interface )
CDES_EDE3 Class Members
DES-EDE3 API ( QuickCrypt API )
DES-EDE3 API Functions
DESX
CDESX ( C++ Interface )
CDESX Class Members
DESX API ( QuickCrypt API )
DESX API Functions
Rijndael (AES)
CRijndael ( C++ Interface )
CRijndael Class Members
Rijndael (AES) API ( QuickCrypt API )
Rijndael (AES) API Functions
Blowfish
CBlowfish ( C++ Interface )
CBlowfish Class Members
Blowfish API ( QuickCrypt API )
Blowfish API Functions
GOST
CGOST ( C++ Interface )
CGOST Class Members
GOST API ( QuickCrypt API )
GOST API Functions
ECB Mode
C++ Interface
QuickCrypt API
CBC Mode
CCBCMode ( C++ Interface )
CCBCMode Class Members
CBC Mode API ( QuickCrypt API )
CBC Mode API Functions
CFB Mode
CCFBMode ( C++ Interface )
CCFBMode Class Members
CFB Mode API ( QuickCrypt API )
CFB Mode API Functions
OFB Mode
COFBMode ( C++ Interface )
COFBMode Class Members
OFB Mode API ( QuickCrypt API )
OFB Mode API Functions
CTR Mode
CCounterMode ( C++ Interface )
CCounterMode Class Members
CTR Mode API ( QuickCrypt API )
CTR Mode API Functions
QuickCrypt Library Run-Time Dynamic Linking
License Agreement
Samples

SlavaSoft QuickCrypt Library Online Help

Prev Page Next Page
QuickCrypt Library API

The following sample demonstrates how to use the DESX and CFB API to perform encryption/decryption of a file in CFB mode of operation.

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

#define BUFF_SIZE  1024

/* performs DESX file encryption/decryption in CFB mode */
int EncryptDecryptFile( const char* filenamefrom, const char* filenameto, int dir, const unsigned char* iv, const unsigned char* key )
{
    /* dir: SLC_ENCRYPT - perform encryption */
    /*      SLC_DECRYPT - perform decryption */

    FILE *filefrom, *fileto;
    unsigned char buff[ BUFF_SIZE ];
    unsigned char context[ SLC_DESX_CONTEXTSIZE ];
    int fs = 0;

    /***** 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_DESX_Init( context, SLC_ENCRYPT, key, SLC_DESX_DEFAULTKEYSIZE );
    SL_CFB_Init( context, dir, iv, fs );

    /***** 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_CFB_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_DESX_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'
    };

    unsigned char iv[SLC_DESX_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, SLC_ENCRYPT, 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, SLC_DECRYPT, 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;
}

 


 

 

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