Example of Using Rijndael Functions

AES Encryption and Decryption

    // use of the CTR mode
void AES_sample(void){
   // size of Rijndael-128 algorithm block is equal to 16
   const int aesBlkSize = 16;
 
   // get the size of the context needed for the encryption/decryption operation
   int ctxSize;
   ippsRijndael128GetSize(&ctxSize);
 
   // and allocate one
   IppsRijndael128Spec* pCtx = (IppsRijndael128Spec*)( new Ipp8u [ctxSize] );
   // define the key
   Ipp8u key[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
                    0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15};
 
   // and prepare the context for Rijndael128 usage
   ippsRijndael128Init(key,IppsRijndaelKey128, pCtx);
 
   // define the message to be encrypted
   Ipp8u ptext[] = {"quick brown fox jupm over lazy dog"};
 
  // define an initial vector
   Ipp8u crt0[aesBlkSize] = {0xff,0xee,0xdd,0xcc,0xbb,0xaa,0x99,0x88,                         
   0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00};
   Ipp8u  crt[aesBlkSize];
 
   // counter the variable number of bits
   int ctrNumBitSize = 64;
 
 
    // allocate enough memory for the ciphertext
   // note that
   // the size of the ciphertext is always equal to that of the planetext
   Ipp8u ctext[sizeof(ptext)];
 
   // init the counter
   memcpy(crt, crt0, sizeof(crt0));
   // encrypt (CTR mode) ptext message
   // pay attention to the 'length' parameter
   // it defines the number of bytes to be encrypted
   memcpy(crt, crt0, sizeof(crt0));
   ippsRijndael128EncryptCTR(ptext, ctext, sizeof(ptext),
                             pCtx,
                             crt, ctrNumBitSize);
 
   // allocate memory for the decrypted message
   Ipp8u rtext[sizeof(ptext)];
 
   // init the counter
   memcpy(crt, crt0, sizeof(crt0));
 
 
   // decrypt (ECTR mode) ctext message
   // pay attention to the 'length' parameter
   // it defines the number of bytes to be decrypted
   ippsRijndael128DecryptCTR(ctext, rtext, sizeof(ptext),
                             pCtx,
                             crt, ctrNumBitSize);
   delete (Ipp8u*)pCtx;
}
 

Submit feedback on this help topic

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