ByteQueue

From Crypto++ Wiki
Jump to navigation Jump to search
ByteQueue
Documentation
#include <cryptopp/queue.h>

ByteQueue is a data structure used to store byte strings. The queue is implemented as a linked list of byte arrays. Each byte array is stored in a ByteQueueNode, and each string is a SecByteBlock.

The default size of a ByteQueueNode is 256. You can change the size during construction or with SetNodeSize.

A ByteQueue inherits from BufferedTransformation, so it can participate in pipelining.

If your data is structured then a MessageQueue could be helpful to store messages instead of byte arrays.

Filter

Below is an example of using a ByteQueue and the filter interfaces.

#include "cryptlib.h"
#include "filters.h"
#include "files.h"
#include "modes.h"
#include "queue.h"
#include "hex.h"
#include "aes.h"

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
  using namespace CryptoPP;

  SecByteBlock key(AES::MAX_KEYLENGTH);
  SecByteBlock iv(AES::BLOCKSIZE);
  HexEncoder encoder(new FileSink(std::cout));

  std::memset(key, 0x00, key.size());
  std::memset(iv, 0x00, iv.size());
  
  ByteQueue plain, cipher, recover;
  std::string str("Attack at dawn!");
  plain.Put(reinterpret_cast<const byte*>(&str[0]), str.size());

  std::cout << "Plain text: ";
  plain.TransferTo(encoder);
  encoder.MessageEnd();
  std::cout << std::endl;
  
  /////////////////////////////////////////////////////////////
  
  CBC_Mode<AES>::Encryption enc;
  enc.SetKeyWithIV(key, key.size(), iv, iv.size());

  StreamTransformationFilter f1(enc, new Redirector(cipher));
  plain.TransferTo(f1);
  f1.MessageEnd();
  
  std::cout << "Cipher text: ";
  cipher.TransferTo(encoder);
  encoder.MessageEnd();
  std::cout << std::endl;
  
  /////////////////////////////////////////////////////////////
  
  CBC_Mode<AES>::Decryption dec;
  dec.SetKeyWithIV(key, key.size(), iv, iv.size());

  StreamTransformationFilter f2(dec, new Redirector(recover));
  cipher.TransferTo(f2);
  f2.MessageEnd();

  std::cout << "Recovered text: ";
  recover.TransferTo(encoder);
  encoder.MessageEnd();
  std::cout << std::endl;

  return 0;
}