next up previous contents index
Next: Authentication ( OMACCoder ) Up: Symmetric Key Cryptography Previous: Encryption and Decryption with   Contents   Index


Example for a Stream-Cipher ( CBCCoder )

Definition

A stream-cipher is a coder that encrypts and decrypts data streams. In this section we discuss the interface of such a coder by an example: CBCCoder< BlkCipher >. Every stream-cipher in LEDA uses a block-cipher BlkCipher which is specified as a template parameter. (More information about stream-ciphers and block-ciphers can be found in Section stream-ciphers.) The following stream-ciphers are available: ECBCoder, CBCCoder, CFBCoder and OFBCoder. All of them, except for OFBCoder, support fast seek operations (see Section decoding_ifstream). The available block-ciphers are: Blowfish, Twofish and Rijndael.

#include < LEDA/coding/stream_ciphers.h >

Creation

CBCCoder< BlkCipher > C(streambuf* src_stream = 0, streambuf* tgt_stream = 0, bool own_streams = false)
    creates an instance C which uses the given source and target streams. If own_streams is set, then C is responsible for the destruction of the streams, otherwise the pointers src_stream and tgt_stream must be valid during the life-time of C.

CBCCoder< BlkCipher > C(const char* src_file_name, const char* tgt_file_name)
    creates an instance C which uses file-streams for input and output.

Operations

Standard Operations

void C.encode() encodes the source stream and writes the output to the target stream.

void C.decode() decodes the source stream and writes the output to the target stream.

uint32 C.calculate_length_of_encoded_data(uint32 input_length)
    calculates the length (in bytes) of the output when encoding some input of the given length with the current settings. (This function is helpful for encoding memory chunks (see below).)

uint32 C.encode_memory_chunk(const char* in_buf, uint32 in_len, char* out_buf, uint32 out_len)
    encodes the memory chunk starting at in_buf with size in_len into the buffer starting at out_buf with size out_len. The function returns actual length of the encoded chunk which may be smaller than out_len. If the output buffer is too small for the encoded data the failure flag will be set (see below). Note: The output data is slightly longer than the input data due to padding and header data.

uint32 C.decode_memory_chunk(const char* in_buf, uint32 in_len, char* out_buf, uint32 out_len)
    decodes a memory chunk. The meaning of the parameters and the return value is the same as in the previous function.

streambuf* C.get_src_stream() returns the current source stream.

void C.set_src_stream(streambuf* src_stream, bool own_stream = false)
    sets the source stream (cf. constructor).

void C.set_src_file(const char* file_name)
    sets a file as source stream.

streambuf* C.get_tgt_stream() returns the current target stream.

void C.set_tgt_stream(streambuf* tgt_stream, bool own_Stream = false)
    sets the target stream (cf. constructor).

void C.set_tgt_file(const char* file_name)
    sets a file as target stream.

void C.reset(bool keep_key = true)
    puts C in the same state as the default constructor. If keep_key is false the key is set to the empty key. In any case the initialization vector is set to nil.

bool C.failed() returns true if an error occured.

bool C.finished() returns true if the coding is finished.

string C.get_description() provides a description for C.

Additional Operations

uint16 C.get_default_key_size() returns the default key size.

uint16 C.get_accepted_key_size(uint16& min, uint16& max)
    provides the minimum and maximum key size (in bytes). The return value is the default key size.

void C.set_key(const CryptKey& key)
    sets the key.

bool C.has_key() queries whether a key has been set.

CryptKey C.get_key() returns the current key.

BlkCipher* C.get_block_cipher() returns the underlying block-cipher.

void C.set_initialization_vector(const byte* iv)
    sets a user defined initialization vector (IV). The size of iv must be at least the block size of BlkCipher. (If no IV is provided by the user then C generates its own.)

const byte* C.get_initialization_vector()
    returns the user defined IV. (If no IV has been specified then nil is returned.)


next up previous contents index
Next: Authentication ( OMACCoder ) Up: Symmetric Key Cryptography Previous: Encryption and Decryption with   Contents   Index