# man > Crypt::Mode::CBC

---
type: CommandReference
command: Crypt::Mode::CBC
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference
- `use Crypt::Mode::CBC;` — import the module
- `my $m = Crypt::Mode::CBC->new('AES');` — create a new CBC mode object (default PKCS5 padding)
- `my $ciphertext = $m->encrypt($plaintext, $key, $iv);` — encrypt all at once
- `my $plaintext = $m->decrypt($ciphertext, $key, $iv);` — decrypt all at once
- `$m->start_encrypt($key, $iv); $ct = $m->add('chunk1'); $ct .= $m->add('chunk2'); $ct .= $m->finish;` — streaming encryption
- `$m->start_decrypt($key, $iv); $pt = $m->add($ct_part1); $pt .= $m->add($ct_part2); $pt .= $m->finish;` — streaming decryption
- `$m = Crypt::Mode::CBC->new('AES', 0);` — no padding (plaintext must be a multiple of block length)
- `$m = Crypt::Mode::CBC->new('Blowfish', 4, 16);` — zero padding with optional cipher rounds

## Name
`Crypt::Mode::CBC` - Block cipher mode CBC [Cipher-block chaining]

## Synopsis
perl
use Crypt::Mode::CBC;
my $m = Crypt::Mode::CBC->new('AES');

# encrypt / decrypt at once
my $ciphertext = $m->encrypt($plaintext, $key, $iv);
my $plaintext  = $m->decrypt($ciphertext, $key, $iv);

# encrypt in chunks
$m->start_encrypt($key, $iv);
my $ciphertext = $m->add('some data');
$ciphertext .= $m->add('more data');
$ciphertext .= $m->finish;

# decrypt in chunks
$m->start_decrypt($key, $iv);
my $plaintext = $m->add($some_ciphertext);
$plaintext .= $m->add($more_ciphertext);
$plaintext .= $m->finish;
## Description
This module implements CBC cipher mode. It works only with ciphers from CryptX ([Crypt::Cipher::NNNN](http://localhost/phpMan.php/perldoc/Crypt%3A%3ACipher%3A%3ANNNN/markdown)).

## Methods
- `new($name, $padding, $cipher_rounds)` — Constructor. `$name` is the cipher name (e.g. 'AES', 'Blowfish', 'DES_EDE'). `$padding` (optional, default 1): 0 = no padding, 1 = PKCS5 (Crypt::CBC's "standard"), 2 = "oneandzeroes", 3 = ANSI X.923, 4 = zero padding, 5 = zero padding + extra zero block if needed. `$cipher_rounds` is an optional number of rounds.
- `encrypt($plaintext, $key, $iv)` — Returns ciphertext for the given key and IV.
- `decrypt($ciphertext, $key, $iv)` — Returns plaintext for the given key and IV.
- `start_encrypt($key, $iv)` — Initializes streaming encrypt mode.
- `start_decrypt($key, $iv)` — Initializes streaming decrypt mode.
- `add($data)` — Process the next chunk. In encrypt mode returns ciphertext; in decrypt mode returns plaintext.
- `finish()` — Finalizes the operation and returns any remaining output.

## Examples
The Synopsis above demonstrates both at-once and streaming encryption/decryption. A typical usage with different padding and rounds:
perl
my $m = Crypt::Mode::CBC->new('AES', 5, 12);   # zero padding + extra block, 12 rounds
my $ct = $m->encrypt($data, $key, $iv);
my $pt = $m->decrypt($ct, $key, $iv);
## See Also
- [CryptX](http://localhost/phpMan.php/perldoc/Crypt%3A%3AMode/markdown), [Crypt::Cipher](http://localhost/phpMan.php/perldoc/Crypt%3A%3ACipher/markdown)
- [Crypt::Cipher::AES](http://localhost/phpMan.php/perldoc/Crypt%3A%3ACipher%3A%3AAES/markdown), [Crypt::Cipher::Blowfish](http://localhost/phpMan.php/perldoc/Crypt%3A%3ACipher%3A%3ABlowfish/markdown), etc.
- [Wikipedia: CBC mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher-block_chaining_.28CBC.29)

## Exit Codes
Not documented.