# man > Crypt::Mode::CTR

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

## Quick Reference
- `my $m = Crypt::Mode::CTR->new('AES');` — create CTR mode object for AES  
- `my $ct = $m->encrypt($pt, $key, $iv);` — encrypt a complete message  
- `my $pt = $m->decrypt($ct, $key, $iv);` — decrypt a complete message  
- `$m->start_encrypt($key, $iv); $ct = $m->add($pt);` — incremental encryption  
- `$m->start_decrypt($key, $iv); $pt = $m->add($ct);` — incremental decryption  
- `my $m = Crypt::Mode::CTR->new('Blowfish', 1, 8);` — big‑endian counter, 8‑byte width  
- `my $m = Crypt::Mode::CTR->new('AES', 0, 8, 12);` — specify cipher rounds  

## Name
Crypt::Mode::CTR – Block cipher mode CTR (Counter mode)

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

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

# incremental usage
$m->start_encrypt($key, $iv);
my $ct1 = $m->add('some data');
my $ct2 = $m->add('more data');

$m->start_decrypt($key, $iv);
my $pt1 = $m->add($ct1);
my $pt2 = $m->add($ct2);
## Options

### Constructor
- `new($cipher_name)`  
- `new($cipher_name, $ctr_mode, $ctr_width)`  
- `new($cipher_name, $ctr_mode, $ctr_width, $cipher_rounds)`  

`$cipher_name` – any name for which `Crypt::Cipher::<NAME>` exists (e.g. `'AES'`, `'Blowfish'`, `'DES'`, …).  
`$ctr_mode` – counter endianness and increment style:  
  - `0` little‑endian (default)  
  - `1` big‑endian  
  - `2` little‑endian + RFC 3686 increment  
  - `3` big‑endian + RFC 3686 increment  
`$ctr_width` – counter width in bytes (default = full block size).  
`$cipher_rounds` – optional number of rounds for the underlying cipher.

### Methods
- `encrypt($plaintext, $key, $iv)` – returns ciphertext  
- `decrypt($ciphertext, $key, $iv)` – returns plaintext  
- `start_encrypt($key, $iv)` – begin incremental encryption  
- `start_decrypt($key, $iv)` – begin incremental decryption  
- `add($data)` – process the next chunk. In encrypt mode returns ciphertext; in decrypt mode returns plaintext. Call repeatedly after `start_*`.

## Examples
perl
# Basic single‑step encryption with DES‑EDE
my $m  = Crypt::Mode::CTR->new('DES_EDE');
my $ct = $m->encrypt("secret", $key, $iv);
perl
# Incremental encryption with explicit counter width
my $m = Crypt::Mode::CTR->new('AES', 1, 4);
$m->start_encrypt($key, $iv);
my $ct = $m->add("part1") . $m->add("part2");
## See Also
- [CryptX](http://localhost/phpMan.php/perldoc/CryptX/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), …  
- [Wikipedia: CTR mode](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29)