Example of Using Blowfish Functions

Blowfish Encryption and Decryption 

   // use of the CBC mode
void BF_sample(void){
   // size of Blowfish algorithm block is equal to 8
   const int bfBlkSize = 8;
 
   // get the size of the context needed for the encryption/decryption operation
   int ctxSize;
   ippsBlowfishGetSize(&ctxSize);
 
   // and allocate one
   IppsBlowfishSpec* pCtx = (IppsBlowfishSpec*)( new Ipp8u [ctxSize] );

  // define the key
   // note that the key length may vary from 1 to 56 bytes
   Ipp8u key[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
                  0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,
                  0x16,0x17};
 
   // and prepare the context for Blowfish usage
   ippsBlowfishInit(key,sizeof(key), pCtx);
   // define the message to be encrypted
   Ipp8u ptext[] = {"quick brown fox jupm over lazy dog"};
   // define an initial vector
   Ipp8u iv[bfBlkSize] = {0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00};
    // allocate enough memory for the ciphertext
   // note that
   // the size of the ciphertext is always multiple of the cipher block size
   Ipp8u ctext[(sizeof(ptext)+bfBlkSize-1) &~(bfBlkSize-1)];
   // encrypt (CBC mode) ptext message
   // pay attention to the 'length' parameter
   // it defines  the number of bytes to be encrypted
   ippsBlowfishEncryptCBC(ptext, ctext, sizeof(ptext),
                          pCtx,
                          iv,
                          IppsCPPaddingPKCS7);
// allocate memory for the decrypted message
   Ipp8u rtext[sizeof(ptext)];
   // decrypt (CBC mode) ctext message
   // pay attention to the 'length' parameter
   // it defines the number of bytes to be decrypted
   ippsBlowfishDecryptCBC(ctext, rtext, sizeof(ptext),
                          pCtx,
                          iv,
                          IppsCPPaddingPKCS7);
   delete (Ipp8u*)pCtx;
}
 

Submit feedback on this help topic

Copyright © 2000 - 2010, Intel Corporation. All rights reserved.