Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  ENCRYPTBUF (3)


FBB::EncryptBuf - Encrypts information using various methods into a std::ostream



#include <bobcat/encryptbuf>
Linking option: -lbobcat


FBB::EncryptBuf objects are std::streambuf objects that can be used to initialize std::ostream objects with.

All information inserted into such a std::ostream is encrypted and written into a std::ostream that is given as argument to EncryptBuf\(cqs constructor.

All encryption methods defined by the OpenSSL library that can be selected by name may be used in combination with EncryptBuf objects. To select a particular encryption method an identifier is passed to the constructor. E.g., \(dqaes-128-cbc\(dq indicating the AES (Rijndael) method, using 128 bit sized keys and blocks using ‘cbc\(cq mode (see below for an explanation).

When providing shorter keys than expected by the method the provided key is extended by adding the required number of 0-bytes. (zero valued bytes, not \(cq0\(cq characters).

Most modes use an initialization vector. Unless provided at construction time or explicitly set thereafter an initialization vector containg randomly selected values will be used. The initialization vector that is actually used can be obtained from the EncryptBuf object. This is important, as the matching decrypting object needs to know the initialization vector that was used when encrypting the data. Initialization vectors are not security sensitive in the sense that they can be sent in the clear to the decryption object. What is important, though, is that they contain random data when used ‘for real\(cq. When an initialization vector is specified that is shorter than expected by the method it will be extended with the required number of 0-bytes.

Block ciphers use one of the following four encryption modes:
o CBC (Cipher Block Chaining):
The first block is XOR-ed by the initialization vector and then encrypted using the specified method. Subsequent blocks are XOR-ed by the encrypted version of the preceding block. Due to the initialization vector dictionary attacks are infeasible, as long as the initialization vector is truly random.
o ECB (Electronic Code Book):
Each block is encrypted by itself, using the specified encryption method. Although an initialization vector may be specified, it is not used. This method is susceptible to dictionary attacks and should therefore be avoided, unless you know what you\(cqre doing.
o CFB (Cipher Feednack):
This method allows a block cipher to be used as a stream cipher. It uses an initialization vector, which should be unique and random for each new stream of data that is encrypted using the method. Encryption can only start after the first data block has been received.
o OFB (Output Feednack):
This is an alternative way to use a block cipher as a stream cipher. It is somewhat more susceptible to traditional data manipulation attacks, which can usually be thwarted when a message authentication code is added to the information as well. Like CFB it uses an initialization vector, which should again be unique and random for each new stream of data that is encrypted.

The following table presents an overview of methods that are currently available. Methods for which the block size is specified as N.A. are stream ciphers; other methods are block ciphers:

The RC4 stream cipher is subject to a well-known attack (cf. unless the initial 256 bytes produced by the cipher are discarded.


All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.




o EncryptBuf(std::ostream &outStream, char const *type, std::string const &key, std::string const &iv, size_t bufsize = 1024):
This constructor initializes the EncryptBuf object preparing it for the message encrypt algorithm specified with type. The encryption algorithms that can be used are listed in the table found in the DESCRIPTION section. E.g., to use the AES method on 24 bit keys and blocks in CBC mode specify \(dqaes-192-cbc\(dq. The key parameter refers to the key to be used, the iv parameter refers to the initialization vector to use. Both key and iv may contain non-displayable characters. When iv.length() is zero at the time encryption starts it will be filled by the EncryptBuf object with random data. When the key and/or the iv is too small for the requested method they will be expanded by adding the required number of zero valued bytes.
The constructor throws an FBB::Exception exception if an unknown encryption method was specified.
The constructor\(cqs first parameter refers to the std::ostream to receive the encrypted information. Be aware of the fact that the encrypted information most likely contains non-displayable characters.
The bufsize argument specifies the size in bytes of the internal buffer used by EncryptBuf to store incoming characters temporarily. The provided default argument should be OK in all normal cases. There is no copy constructor, nor move constructor (as std::streambuf doesn\(cqt support either).


All members of std::streambuf are available, as FBB::EncryptBuf inherits from this class. Some of the std::streambuf\(cqs member are overridden or are hidden by EncryptBuf. In normal situations these inherited members will not be used by programs using EncryptBuf objects.

o size_t blockLength() const:
This member returns the block size (in bytes) that are used by the specified method.
o size_t ivLength() const:
This member returns the size (in bytes) of the initialization vector that is used by the specified method.
o std::string iv() const:
This member returns a reference to the initialization vector that is used by the specified method. Be advised that the initialization vector may contain non-displayable characters.
o size_t keyLength() const:
This member returns the size of the key (in bytes) that are used by the specified method.
o size_t rounds() const:
This member can only be used with the RC5 encryption method to query the number of rounds of the algorithm. It returns the currently used number of rounds or 0 if the member is called for another encryption method than RC5.
o void setIv(std::string const &iv):
This member can be used to specify the initialization vector to use after construction time but before any data has been encrypted. When called after encryption has started an FBB::Exception exception will be thrown.
o void setKey(std::string const &key, size_t numberOfBytes = 0):
This member can be used to specify the key and its length after construction time but before any data has been encrypted. When called after encryption has started an FBB::Exception exception will be thrown. The size of the key is assumed to be the number of bytes in the key\(cqs data. If another key length is required the member function\(cqs second parameter can be used to specify the length of the key in bytes.
o bool setRounds(size_t nRounds):
This member can only be used with the RC5 encryption method to set the number of rounds of the algorithm to 8, 12 or 16. When the number of rounds were updated successfully the member returns true. It returns false in other cases (e.g., called for other encryption methods than RC5 or the requested number of rounds differ from 8, 12 or 16).


o EVP_CIPHER_CTX *cipherCtx():
Classes derived from EncryptBuf may use this member to gain direct access to the EVP_CIPHER_CTX pointer used by the EncryptBuf object. This pointer is a pointer to an opaque structure used by many OpenSSL functions to set or query parameters of an encryption method.


#include <iostream>
#include <fstream>
#include <bobcat/errno>
#include <bobcat/encryptbuf>
#include <bobcat/ohexstreambuf>

#include <openssl/evp.h>

using namespace std; using namespace FBB;

int main(int argc, char **argv) try { if (argc == 1) throw Errno(\(dq1st arg: method, 2nd arg: key, 3rd arg: (opt): iv, \(dq \(dqstdin: file to encrypt (to stdout)\(dq);

string key(argv[2]); string iv;

if (argc > 3) iv = argv[3];

EncryptBuf encryptbuf(cout, argv[1], key, iv); ostream out(&encryptbuf);

cerr << \(dqBlock length: \(dq << encryptbuf.blockLength() << \(cq\n\(cq << \(dqKey length: \(dq << encryptbuf.keyLength() << \(cq\n\(cq << \(dqMax Key length: \(dq << EVP_MAX_KEY_LENGTH << \(cq\n\(cq << \(dqIV length: \(dq << encryptbuf.ivLength() << endl; cerr << encryptbuf.iv().length() << \(cq \(cq;

OHexStreambuf ohsb(cerr);

ostream ohs(&ohsb); ohs.write(encryptbuf.iv().data(), encryptbuf.iv().length()) << flush; cerr << endl;

out << cin.rdbuf(); } catch(Errno const &err) { cout << err.why() << endl; return 1; }

To ignore the initial 256 bytes generated by the RC4 algorithm a simple wrapper class around the eventual output stream can be used. Here is an illustration:

#include <ostream> #include <bobcat/ofilterstreambuf> class Skip256: public FBB::OFilterStreambuf { size_t d_count; public: Skip256(std::ostream &os) : OFilterStreambuf(os), d_count(0) {} private: virtual int overflow(int c) { if (d_count == 256) out().put(c); else ++d_count; return c; } };

Next, an Skip256 object is used to define an intermediate std::ostream that is then passed to the EncryptBuf object. E.g., only showing the essential steps defining the EncryptBuf object:

Skip256 skip256(std::cout); std::ostream out(&skip256);

EncryptBuf encryptbuf(out, \(dqrc4\(dq, key, \(dq\(dq);


bobcat/encryptbuf - defines the class interface


bobcat(7), decryptbuf(3bobcat), ofilterstreambuf(3bobcat), std::streambuf


None reported


o bobcat_3.25.01-x.dsc: detached signature;
o bobcat_3.25.01-x.tar.gz: source archive;
o bobcat_3.25.01-x_i386.changes: change log;
o libbobcat1_3.25.01-x_*.deb: debian package holding the libraries;
o libbobcat1-dev_3.25.01-x_*.deb: debian package holding the libraries, headers and manual pages;
o public archive location;


Bobcat is an acronym of ‘Brokken\(cqs Own Base Classes And Templates\(cq.


This is free software, distributed under the terms of the GNU General Public License (GPL).


Frank B. Brokken (

Search for    or go to Top of page |  Section 3 |  Main Index

libbobcat-dev_3&.25&.01-x&.tar&.gz FBB::ENCRYPTBUF (3bobcat) 2005-2015

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.