QuickCrypt Library C++ Interface

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

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

using namespace std;
using namespace QuickCrypt;

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

    //Instantiate CBlowfish object

    CBlowfish cipher( dir, key );

    //Encrypt/Decrypt memory buffer
   
    for( int i = 0; i < numblocks; ++i )
    {
        cipher.ProcessBlock( buff + i * CBlowfish::BLOCKSIZE ); //in-place encryption/decryption
    }
}

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

int main()
{
    //Define key & blocks

    char key[CBlowfish::DEFAULTKEYSIZE] = 
    {
        's', 'e', 'c', 'r', 'e', 't', 'l', 'y',
        '2', '9', '5', '1', '7', '4', '%', '*',
        '^', '&', ')', '2', '$', ')', '-', '7',
        '0', '0', '5', '8', '1', '0', '5', '3',
        '2', '9', '5', '1', '7', '4', '%', '*',
        '^', '&', ')', '2', '$', ')', '-', '7',
        '0', '0', '5', '8', '1', '0', '5', '3'
    };

    const unsigned int NUM_BLOCKS = 3;

    unsigned char blocks[CBlowfish::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
    };

    cout.setf( ios::hex, ios::basefield );
    cout << "Initial blocks:" << endl;
    ShowBuffer( blocks, NUM_BLOCKS );

    //Encrypt blocks

    EncryptDecryptBuffer( blocks, NUM_BLOCKS, SLC_ENCRYPT, (const unsigned char*)key );
    
    cout << "Encrypted blocks:" << endl;
    ShowBuffer( blocks, NUM_BLOCKS );

    //Decrypt blocks

    EncryptDecryptBuffer( blocks, NUM_BLOCKS, SLC_DECRYPT, (const unsigned char*)key );

    cout << "Decrypted blocks:" << endl;
    ShowBuffer( blocks, NUM_BLOCKS );

    return 0;
}

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

            cout << (int)buff[ i * CBlowfish::BLOCKSIZE + j ] << " ";
        }

        cout << endl;
    }
}

 


 

 

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