**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[1], P[2], P[3},...) = P : Split plaintext into blocks C[1] = E(K, P[1] XOR IV) Loop i over 2,3,... C[i] = E(K, P[i] XOR C[i-1]) End Loop C = (C[1], C[2], 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[1]--XOR / P[2]--XOR / P[3]--XOR | / | / | E(K) / E(K) / E(K) | / | / | C[1] C[2] C[3] ...

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.

- -key => $key - The encryption/decryption secret key or pass phrase. Value is required.
- -cipher => $cipher - The cipher algorithm identifier or a preexisting cipher object. Value is optional with DES as the default. When -cipher => 'Blowfish' is specified, it will require the Crypt::Blowfish module.

$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

Installing Crypt::CBC 2.33 with ActivePerl

Crypt::CBC Encryption with Literal Keys

Crypt::CBC Literal Key Error Cases

Crypt::CBC Encryption with Crypt::Blowfish Objects

Crypt::CBC Operation Simulation

Crypt::CBC Encryption Verification

Blowfish CBC 2-Block Test Vectors

Crypt::CBC Prepending IV to Ciphertext

Crypt::CBC Encryption with Salted Keys

Crypt::CBC Salted Key Test Cases

Crypt::CBC Secret Key and IV Algorithm

Crypt::CBC Encryption with Random Salt

Crypt::CBC Padding Option Tests

Crypt::CBC Blowfish Encryption Summary

OpenSSL "enc -bf-ecb" for Blowfish/ECB Encryption

OpenSSL "enc -bf-cbc" for Blowfish/CBC Encryption

OpenSSL "enc -bf-cfb" for Blowfish/CFB Encryption

OpenSSL "enc -bf-ofb" for Blowfish/OFB Encryption