FBB::EncryptBuf(3bobcat)
π NAME π
FBB::EncryptBuf β the Encrypt information page of the (3bobcat) manual. Bestie, this class literally encrypts your stuff using various methods and writes it into a std::ostream. π
β¨ SYNOPSIS β¨
#include <bobcat/encryptbuf>
Linking option: -lbobcat -lcrypto
πΈ DESCRIPTION πΈ
okay so. FBB::EncryptBuf objects are std::streambuf objects, and you use them to initialize std::ostream objects. That's the whole vibe. π¦
Everything you insert into one of those std::ostreams gets encrypted and written into an std::ostream that you hand over as an argument to EncryptBuf's constructor. No cap, it just does it.
Every encryption method the OpenSSL library supports that can be selected by name can be used by EncryptBuf objects. To pick a particular method you pass an identifier to the constructor. Like, say, "aes-128-cbc" β that one means the AES (Rijndael) method, using 128 bit sized keys and blocks, in cbc mode (there's a whole explanation below, don't worry π«Ά).
Most modes use initialization vectors. Unless you provide one at construction time, EncryptBuf objects create random initialization vectors all by themselves. The initialization vector that's actually used can be obtained from the EncryptBuf object. This matters a LOT, because the matching decryption object needs to know which initialization vector was used when the data was encrypted. Initialization vectors are not considered confidential β you can send them in the clear to the decryption object, it's fine. What is important, though, is that they contain random data when you use them for real. And if you specify an initialization vector that's shorter than the encryption method expects, it gets automatically extended with 0-bytes up to the required length. Easy. β¨
Block ciphers use one of these four encryption modes:
- CBC (Cipher Block Chaining): The first block gets XOR-ed by the initialization vector and then encrypted using the specified method. Every block after that gets XOR-ed by the encrypted version of the block right before it. Thanks to the initialization vector, dictionary attacks are infeasible β as long as the initialization vector is truly random. π₯
- ECB (Electronic Code Book): Every block gets encrypted all by itself, using the specified encryption method. Even if you specify an initialization vector, it is not used. This method is susceptible to dictionary attacks and should therefore be avoided, unless you know what you're doing. (iykyk, but fr fr, avoid it. π)
- CFB (Cipher Feednack): This method lets a block cipher be used as a stream cipher. It uses an initialization vector, which should be unique and random for each new stream of data encrypted using the method. Encryption can only start after the first data block has been received.
- OFB (Output Feednack): This is an alternative way to use a block cipher as a stream cipher. It's 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's encrypted.
Here's the whole overview of methods that are currently available, bestie. Methods where the block size is specified as N.A. are stream ciphers; all the other methods are block ciphers. π
π AES
- keysize
16(bytes), blocksize8(bytes):- CBC β
"aes-128-cbc" - EBC β
"aes-128-ecb" - CFB β
"aes-128-cfb" - OFB β
"aes-128-ofb"
- CBC β
- keysize
24(bytes), blocksize24(bytes):- CBC β
"aes-192-cbc" - EBC β
"aes-192-ecb" - CFB β
"aes-192-cfb" - OFB β
"aes-192-ofb"
- CBC β
- keysize
32(bytes), blocksize32(bytes):- CBC β
"aes-256-cbc" - EBC β
"aes-256-ecb" - CFB β
"aes-256-cfb" - OFB β
"aes-256-ofb"
- CBC β
π BLOWFISH
- keysize
16(bytes), blocksize8(bytes):- CBC β
"bf-cbc" - EBC β
"bf-ecb" - CFB β
"bf-cfb" - OFB β
"bf-ofb"
- CBC β
- max key length is
56bytes,16generally used
πΊ CAMELLIA
- keysize
16(bytes), blocksize16(bytes):- CBC β
"camellia-128-cbc" - EBC β
"camellia-128-ecb" - CFB β
"camellia-128-cfb" - OFB β
"camellia-128-ofb"
- CBC β
- keysize
24(bytes):- CBC β
"camellia-192-cbc" - EBC β
"camellia-192-ecb" - CFB β
"camellia-192-cfb" - OFB β
"camellia-192-ofb"
- CBC β
- keysize
32(bytes):- CBC β
"camellia-256-cbc" - EBC β
"camellia-256-ecb" - CFB β
"camellia-256-cfb" - OFB β
"camellia-256-ofb"
- CBC β
π CAST
- keysize
16(bytes), blocksize8(bytes):- CBC β
"cast-cbc" - EBC β
"cast-ecb" - CFB β
"cast-cfb" - OFB β
"cast-ofb"
- CBC β
- min key length is
5bytes, max is shown
π°οΈ DES
- keysize
8(bytes), blocksize8(bytes):- CBC β
"des-cbc" - EBC β
"des-ebc" - CFB β
"des-cfb" - OFB β
"des-ofb"
- CBC β
π°οΈ DESX
- keysize
8(bytes), blocksize8(bytes):- CBC β
"desx-cbc"
- CBC β
π§ 3DES
- keysize
16(bytes), blocksize8(bytes):- CBC β
"des-ede-cbc" - EBC β
"des-ede" - CFB β
"des-ede-cfb" - OFB β
"des-ede-ofb"
- CBC β
π§ 3DES
- keysize
24(bytes), blocksize8(bytes):- CBC β
"des-ede3-cbc" - EBC β
"des-ede3" - CFB β
"des-ede3-cfb" - OFB β
"des-ede3-ofb"
- CBC β
- Key bytes
9-16define the 2nd key, bytes17-24define the 3rd key
π RC2
- keysize
16(bytes), blocksize8(bytes):- CBC β
"rc2-cbc" - EBC β
"rc2-ecb" - CFB β
"rc2-cfb" - OFB β
"rc2-ofb"
- CBC β
- Key length variable, max.
128bytes, default length is shown
π RC2-40
- keysize
5(bytes), blocksize8(bytes), identifier"rc2-40-cbc" - obsolete: avoid
π RC2-64
- keysize
8(bytes), blocksize8(bytes), identifier"rc2-64-cbc" - obsolete: avoid
π RC4
- keysize
16(bytes), blocksizeN.A., identifier"rc4" - Key length is variable, max.
256bytes. default length is shown - Encrypt again to decrypt. Don't use
DecryptBuf
π RC4-40
- keysize
5(bytes), blocksizeN.A., identifier"rc4-40" - obsolete: avoid
π RC5
- keysize
16(bytes), blocksize8(bytes):- CBC β
"rc5-cbc" - EBC β
"rc5-ecb" - CFB β
"rc5-cfb" - OFB β
"rc5-ofb"
- CBC β
- Key length variable, max.
256bytes, rounds8,12or16, default # rounds is12
π¨ Heads up bestie: the RC4 stream cipher is subject to a well-known attack (cf.http://www.wisdom.weizmann.ac.il/~itsik/RC4/Papers/Mantin1.zip) unless the initial256bytes produced by the cipher are discarded.
π«Ά NAMESPACE π«Ά
FBB
All constructors, members, operators and manipulators mentioned in this man-page are defined in the namespace FBB. β¨
π¦ INHERITS FROM π¦
FBB::CryptBuf, which in turn inherits from std::streambuf. Family tree slay. πΈ
π CONSTRUCTOR/DESTRUCTOR π
EncryptBuf(std::ostream &outStream, char const *type, std::string const &key, std::string const &iv, size_t bufsize = 1024)β okay bestie, this constructor gets yourEncryptBufobject ready for the encryption algorithm you picked withtype. The encryption algorithms you can use are listed in that abovementioned table. E.g., to use the AES method on 24 bit keys and blocks in CBC mode, specify"aes-192-cbc". Thekeyparameter refers to the key to be used, and theivparameter refers to the initialization vector to use. Bothkeyandivmay contain non-displayable characters. Wheniv.length()is zero, it will be filled by theEncryptBufobject with random data. When theivis too small for the requested method, it is expanded by adding the required number of zero valued bytes.
The constructor throws an FBB::Exception exception if an encryption method is specified that is not supported by OpenSSL.
The constructorβs 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 temporarily storing incoming characters. The provided default argument can most likely be kept as-is.
~EncryptBuf()β Normally, once all information has been inserted into the encryption stream, the end manipulator (see below) is inserted to complete the encryption process. Alternatively, the encryption process ends once theEncryptBufβs destructor is called. E.g., ifencStreamis thestd::ostreamto receive the information to encrypt andinStreamis thestd::istreamcontaining the information to encrypt, then
endStream << inStream.rdbuf();
completes the decryption once EncryptBufβs destructor is called. Alternatively,
encStream << inStream.rdbuf() << end;
can be used to immediately complete the encryption process.
Copy and move constructors (and assignment operators) are not available.
β¨ MEMBER FUNCTIONS β¨
All members of std::streambuf are available, as FBB::EncryptBuf inherits from this class.
size_t blockLength() constβ this member returns the block size (in bytes) that are used by the specified method.size_t ivLength() constβ this member returns the size (in bytes) of the initialization vector that is used by the specified method.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.size_t keyLength() constβ this member returns the size of the key (in bytes) that are used by the specified method.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 or0if the member is called for another encryption method than RC5.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 to8,12or16. When the number of rounds were updated successfully, the member returnstrue. It returnsfalsein other cases (e.g., called for other encryption methods than RC5 or the requested number of rounds differ from8,12or16).
π PROTECTED MEMBER π
EVP_CIPHER_CTX *cipherCtx()β Classes derived fromEncryptBufmay use this member to gain direct access to theEVP_CIPHER_CTXpointer used by theEncryptBufobject. This pointer is a pointer to an opaque structure used by many OpenSSL functions to set or query parameters of an encryption method.
π EXAMPLE π
#include <iostream>
#include <iomanip>
#include <fstream>
#include <bobcat/exception>
#include <bobcat/ohexbuf>
#include "../encryptbuf"
#include <openssl/evp.h>
using namespace std;
using namespace FBB;
int main(int argc, char **argv)
try
{
if (argc == 1)
throw Exception(1) <<
"1st arg: method, 2nd arg: key, 3rd arg: (opt): iv, "
"stdin: file to encrypt (to stdout)\n"
"e.g., driver aes-128-cbc somekey < driver.cc > /tmp/enc\n";
string key(argv[2]);
string iv;
if (argc > 3)
iv = argv[3];
EncryptBuf encryptbuf(cout, argv[1], key, iv, 50);
ostream out(&encryptbuf);
size_t ivLength = encryptbuf.iv().length();
cerr << "Block length: " << encryptbuf.blockLength() << "\n"
"Key length: " << encryptbuf.keyLength() << "\n"
"Max Key length: " << EVP_MAX_KEY_LENGTH << "\n"
"actual IV length: " << ivLength << "\n"
"IV =\n";
OHexBuf ohb{ cerr, ivLength << 1 };
ostream outHex(&ohb);
outHex << encryptbuf.iv();
cerr << β\nβ << dec;
out << cin.rdbuf() << eoi;
}
catch(exception const &err)
{
cerr << err.what() << 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/ofilterbuf>
class Skip256: public FBB::OFilterBuf
{
size_t d_count;
public:
Skip256(std::ostream &os)
:
OFilterBuf(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, "rc4", key, "");
π FILES π
bobcat/encryptbuf- defines the class interface
π SEE ALSO π
bobcat(7)decryptbuf(3bobcat)ofilterbuf(3bobcat)std::streambuf
π BUGS π
None reported
π¦ BOBCAT PROJECT FILES π¦
https://fbb-git.gitlab.io/bobcat/: gitlab project page;bobcat_5.09.01-x.dsc: detached signature;bobcat_5.09.01-x.tar.gz: source archive;bobcat_5.09.01-x_i386.changes: change log;libbobcat1_5.09.01-x_*.deb: debian package containing the libraries;libbobcat1-dev_5.09.01-x_*.deb: debian package containing the libraries, headers and manual pages;
π BOBCAT π
Bobcat is an acronym of `Brokkenβs Own Base Classes And Templatesβ.
π COPYRIGHT π
This is free software, distributed under the terms of the GNU General Public License (GPL).
πΈ AUTHOR πΈ
Frank B. Brokken (f.b.brokken@rug.nl).
This lilβ page is from libbobcat-dev_5.09.01, dated 2005-2021, and itβs literally FBB::EncryptBuf(3bobcat). Stay encrypted, bestie. π
β¨π«Ά
π¦ where this page comes from
- ubuntu jammy: libbobcat-dev 5.09.01-2build1 Β· GPL-2.0-or-later WITH OpenSSL exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms