Unit implementing blowfish encryption/decryption streams

The BlowFish implements a class to handle blowfish encryption/decryption of memory buffers, and 2 TStream descendents which decrypts any data that is read from it on the fly, as well as which encrypts the data that is written to it on the fly.

Exception support and formatting routines. TStream declaration Number of rounds in blowfish encryption. Pointer to TBlowFishKey array PBlowFishKey is a simple pointer to a array. Array to hold blowfish encryption keys TBlowFishKey is a data structure which keeps the encryption or decryption key for the , and classes. It should be filled with the encryption key and passed to the constructor of one of these classes. Internal blowfish data structure TBFBlock is the basic data structure used by the encrypting/decrypting routines in , and . It is the basic encryption/decryption block for all encrypting/decrypting: all encrypting/decrypting happens on a TBFBlock structure. Class implementing blowfish encryption/decription TBlowFish is a simple class that can be used to encrypt/decrypt a single data block with the Encrypt and Decrypt calls. It is used internally by the and classes to encrypt or decrypt the actual data. Create a new instance of the TBlowFish class Create initializes a new instance of the TBlowFish class: it stores the key Key in the internal data structures so it can be used in later calls to Encrypt and Decrypt. Encrypt Decrypt Encryption key Actual size of encryption key Encrypt a block Encrypt encrypts the data in Block (always 8 bytes) using the key specified when the TBlowFish instance was created. Decrypt Create Data to encrypt Decrypt a block ncrypt decrypts the data in Block (always 8 bytes) using the key specified when the TBlowFish instance was created. The data must have been encrypted with the same key and the Encrypt call. Encrypt Create Encrypted data to decrypt Exception used to report errors. EBlowFishError is used by the , and classes to report errors. Parent class for blowfish encryption/decryption streams

TBlowFishStream is an abstract class which is used as a parent class for and . It simply provides a constructor and storage for a instance and for the source or destination stream.

Do not create an instance of TBlowFishStream directly. Instead create one of the descendent classes TBlowFishEncryptStream or TBlowFishDecryptStream.

Create a new instance of the TBlowFishStream class

Create initializes a new instance of TBlowFishStream, and creates an internal instance of using AKey and AKeySize. The Dest stream is stored so the descendent classes can refer to it.

Do not create an instance of TBlowFishStream directly. Instead create one of the descendent classes TBlowFishEncryptStream or TBlowFishDecryptStream.

The overloaded version with the KeyPhrase string argument is used for easy access: it computes the blowfish key from the given string.

String to compute key from Encryption/Decryption key Actual key size. Destination stream Destroy the TBlowFishStream instance. Destroy cleans up the internal instance. Create Blowfish instance used when encrypting/decrypting BlowFish is the instance which is created when the TBlowFishStream class is initialized. Normally it should not be used directly, it's intended for access by the descencent classes and . On-the-fly Blowfish encryption stream

The TBlowFishEncryptStream provides On-the-fly Blowfish encryption: all data that is written to it is encrypted and then written to a destination stream, which must be specified when the TBlowFishEncryptStream instance is created. The encryption key must also be created when the stream instance is created.

This is a write-only stream: it is not seekable, and data can only be written to it, reading is not possible. For reading encrypted data, the stream must be used.

Create
Free the TBlowFishEncryptStream Destroy flushes the encryption buffer, and writes it to the destination stream. After that the Inherited destructor is called to clean up the TBlowFishEncryptStream instance. Flush Create Read data from the stream Read will raise an EBlowFishError exception: TBlowFishEncryptStream is a write-only stream. Calling this function always results in an exception. Actual number of bytes read Buffer in which to store read data Number of bytes to read. Write data to the stream

Write will encrypt and write Count bytes from Buffer to the destination stream. The function returns the actual number of bytes written. The data is not encrypted in-place, but placed in a special buffer for encryption.

Data is always written 4 bytes at a time, since this is the amount of bytes required by the Blowfish algorithm. If no multiple of 4 was written to the destination stream, the Flush mechanism can be used to write the remaining bytes.

Actual number of bytes written Buffer containing data to be written. Number of bytes to write. Set the position in the stream Read will raise an EBlowFishError exception: TBlowFishEncryptStream is a write-only stream, and cannot be positioned. Calling this function always results in an exception. New position in the stream Offset from origin Origin for seek operation Flush the encryption buffer

Flush writes the remaining data in the encryption buffer to the destination stream.

For efficiency, data is always written 4 bytes at a time, since this is the amount of bytes required by the Blowfish algorithm. If no multiple of 4 was written to the destination stream, the Flush mechanism can be used to write the remaining bytes.

Flush is called automatically when the stream is destroyed, so there is no need to call it after all data was written and the stream is no longer needed.

Write
On-the-fly Blowfish decryption stream

The TBlowFishDecryptStream provides On-the-fly Blowfish decryption: all data that is read from the source stream is decrypted before it is placed in the output buffer. The source stream must be specified when the TBlowFishDecryptStream instance is created. The Decryption key must also be created when the stream instance is created, and must be the same key as the one used when encrypting the data.

This is a read-only stream: it is seekable only in a forward direction, and data can only be read from it, writing is not possible. For writing data so it is encrypted, the stream must be used.

Create
Read data from the stream Read reads Count bytes from the source stream, decrypts them using the key provided when the TBlowFishDeCryptStream instance was created, and writes the decrypted data to Buffer Create Actual number of bytes read Memory buffer in which to place data Number of bytes to read from the stream Write data to the stream Write will raise an EBlowFishError exception: TBlowFishDecryptStream is a write-only stream. Calling this function always results in an exception. Number of bytes actually written. Buffer with data to be written. Number of bytes to write Set the stream position. Seek emulates a forward seek by reading and discarding data. The discarded data is lost. Since it is a forward seek, this means that only soFromCurrent can be specified for Origin with a positive (or zero) Offset value. All other values will result in an exception. The function returns the new position in the stream. If any other combination of Offset and Origin than the allowed combination is specified, then an exception will be raised. Read New position in the stream Offset. Only zero or positive values allowed. Origin. Only soFromCurrent allowed.