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 #2 (C++ Interface)  

All

Previous Next


The following sample demonstrates how to use the CBlowfish and CCBCMode classes to perform encryption/decryption of a memory buffer in CBC mode of operation.

#include <iostream>
#include <QuickCrypt.h>

using namespace std;
using namespace QuickCrypt;

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

    //Instantiate CCBCMode object


    CCBCMode<CBlowfish> cipher( dir, iv, key );

    //Encrypt/Decrypt memory buffer
  
    return cipher.Process( 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[CBlowfish::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[CBlowfish::BLOCKSIZE] =
    {
        0x41, 0x3E, 0xF0, 0xA1, 0xC6, 0x11, 0xE5, 0x50
    };

    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;

    cout.setf( ios::hex, ios::basefield );
    cout << "Initial memory buffer:" << endl;
    ShowBuffer( buff, buffsize );


    //Encrypt memory buffer

    buffsize = EncryptDecryptBuffer( buff, buffsize, SLC_ENCRYPT, iv, (const unsigned char*)key );
   
    cout << "Encrypted memory buffer:" << endl;
    ShowBuffer( buff, buffsize );

    //Decrypt memory buffer

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

    cout << "Decrypted memory buffer:" << endl;
    ShowBuffer( buff, buffsize );

    return 0;
}

void ShowBuffer( const unsigned char* buff, const unsigned int buffsize )
{
    for( int i = 0; i < buffsize; ++i )
    {
        cout.fill( '0' );
        cout.width( 2 );

        cout << (int)buff[ i ] << " ";

        if( ( i + 1 ) % CBlowfish::BLOCKSIZE == 0 )
            cout << endl;
    }
    cout << endl;
}
 
  Copyright © SlavaSoft Inc. All rights reserved.