Blowfish Cipher Tutorials - Herong's Tutorial Examples - Version 2.00, by Dr. Herong Yang
What is Crypt::CBC
A quick introduction is provided for the Crypt::CBC Perl module, which can be used to perform encryption and decryption in the CBC (Cipher Block Chaining) operation mode with a given block cipher for any number of blocks.
What Is Crypt::CBC? Crypt::CBC is a Perl module providing CBC (Cipher Block Chaining) block cipher operation mode functionality. Crypt::CBC was developed by Lincoln Stein.
Here is the description of Crypt::CBC on the cpan.org Web site: "This module is a Perl-only implementation of the cryptographic cipher block chaining mode (CBC). In combination with a block cipher such as DES or IDEA, you can encrypt and decrypt messages of arbitrarily long length. The encrypted messages are compatible with the encryption format used by the OpenSSL package."
"To use this module, you will first create a Crypt::CBC cipher object with new(). At the time of cipher creation, you specify an encryption key to use and, optionally, a block encryption algorithm. You will then call the start() method to initialize the encryption or decryption process, crypt() to encrypt or decrypt one or more blocks of data, and lastly finish(), to pad and encrypt the final block. For your convenience, you can call the encrypt() and decrypt() methods to operate on a whole data value at once.
The above description does not mention Blowfish, but Crypt::CBC can be used together with Crypt::Blowfish to encrypt and decrypt messages with Blowfish algorithm in CBC operation mode.
In case you forgot how CBC (Cipher Block Chaining) works, here a shortest version of the CBC algorithm:
Input: P: The plaintext in multiple blocks K: The secret key IV: The Initialization Vector E(K,B): The block encryption function Output: C: The ciphertext in multiple blocks Algorithm - CBC (Cipher Block Chaining) Operation Mode: (P, P, P[3},...) = P : Split plaintext into blocks C = E(K, P XOR IV) Loop i over 2,3,... C[i] = E(K, P[i] XOR C[i-1]) End Loop C = (C, C, C[3},...) : Concatenate ciphertext blocks
The CBC algorithm can also be illustrated by this simple diagram:
Algorithm - CBC (Cipher Block Chaining) Operation Mode: IV ----->| ----->| ----->| | / | / | P--XOR / P--XOR / P--XOR | / | / | E(K) / E(K) / E(K) | / | / | C C C ...
Crypt::CBC module offers the following major methods to perform the CBC operation algorithm described above. Other methods will be discussed later as needed.
$c = Crypt::CBC->new(param-list) - Returns a new Crypt::CBC cipher operation object with a given list of parameters. Two most important parameters are listed below. Other parameters will be discussed later as needed.
$c->encrypt($plaintext) - Returns the ciphertext for the given plaintext of any size, using the cipher algorithm including the secret key specified in the cipher operation object $c and the CBC operation mode.
$c->decrypt($ciphertext) - Returns the plaintext for the given ciphertext of any size, using the cipher algorithm including the secret key specified in the cipher operation object $c and the CBC operation mode.
$c->start('encrypting|decrypting') - Initialize the cipher operation object $c to start a new encryption or decryption operation.
$c->crypt($text) - Returns the next output block from the current encryption or decryption operation with the given extra input. This method is designed to be used in a loop to encrypt or decrypt large input in chunks.
$c->finish() - Returns the last output block from the current encryption or decryption operation.
$c->encrypt($plaintext) - Returns the ciphertext as the encryption output from the specified plaintext.
$c->decrypt($ciphertext) - Returns the plaintext as the decryption output from the specified ciphertext.
See "Crypt::CBC" Web page at http://search.cpan.org/~lds/Crypt-CBC-2.33/CBC.pm for more information.
Last update: 2018.
Table of Contents