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

All

Previous Next


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

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

using namespace std;
using namespace QuickCrypt;

// performs Blowfish encryption/decryption of a portion of the memory buffer in Counter mode
void EncryptDecryptBufferPortion( unsigned char* buff, const unsigned int buffsize, const unsigned char* iv, const unsigned char* key )
{
    //Instantiate CCounterMode object

    CCounterMode<CBlowfish> cipher( iv, key );

    //Encrypt/Decrypt the second half of the memory buffer

    unsigned int pos = buffsize / 2;

    cipher.Seek( pos );
    cipher.Process( buff + pos, buff + pos, buffsize - pos ); //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[] =
    {
        0xED, 0x39, 0x08, 0x1B, 0xCA, 0xBE, 0x94, 0xCC,
        0xA2, 0x04, 0x5F, 0x0C, 0x94, 0xCC, 0xAD, 0xE6,
        0x24, 0xCA, 0xFE, 0x33, 0xF9, 0x8B, 0x29, 0x1F,
        0x8B, 0xE9, 0x63, 0x1A, 0x96, 0xD9, 0x75, 0xDA
    };

    unsigned int buffsize = sizeof( buff ) / sizeof( unsigned char );

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

    //Encrypt memory buffer

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

    //Decrypt memory buffer

    EncryptDecryptBufferPortion( buff, buffsize, 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.