Crypt::Mode::CTR(3)
π Crypt::Mode::CTR β the perl manual page, bestie edition π
okay so you're reading the Crypt::Mode::CTR(3) page (yes that's the manual name, we're not copying the header line, we're folding it in like a good little intro, you're welcome π«Ά). It's about CTR mode, aka Counter mode, and it is so useful. Sit down, hydrate, let's go β¨
β¨ NAME β¨
Crypt::Mode::CTR β Block cipher mode CTR [Counter mode]. That's it, that's the whole name. Short, iconic, to the point π
πΈ SYNOPSIS πΈ
here's the whole vibe in code, no notes:
use Crypt::Mode::CTR;
my $m = Crypt::Mode::CTR->new('AES');
my $key = '1234567890123456';
my $iv = '1234567890123456';
my $plaintext = 'example plaintext';
my $chunk1 = 'example ';
my $chunk2 = 'plaintext';
# encrypt or decrypt in one call
my $single_ciphertext = $m->encrypt($plaintext, $key, $iv);
my $single_plaintext = $m->decrypt($single_ciphertext, $key, $iv);
# encrypt more chunks
$m->start_encrypt($key, $iv);
my $chunked_ciphertext = '';
$chunked_ciphertext .= $m->add($chunk1);
$chunked_ciphertext .= $m->add($chunk2);
# decrypt more chunks
$m->start_decrypt($key, $iv);
my $chunked_plaintext = '';
$chunked_plaintext .= $m->add($chunked_ciphertext);
likeβ¦ one call or many chunks, both slay π
π¦ DESCRIPTION π¦
So this module implements CTR cipher mode. That's the whole thing. But there's a catch, bestie, and it's a big one:
β οΈ Note: it works only with ciphers from CryptX (Crypt::Cipher::NNNN). Don't show up with some random other cipher, it's not gonna work, ngl.
π METHODS π
Unless noted otherwise, assume $m is an existing mode object created via "new", for example:
my $m = Crypt::Mode::CTR->new('AES');
like, that's your main character. everything below is done to $m. periodt π
π« new
okay so this is where it all begins. three flavors of constructor, pick your fighter:
my $m = Crypt::Mode::CTR->new($cipher_name);
#or
my $m = Crypt::Mode::CTR->new($cipher_name, $ctr_mode, $ctr_width);
#or
my $m = Crypt::Mode::CTR->new($cipher_name, $ctr_mode, $ctr_width, $cipher_rounds);
# $cipher_name .. [string] one of 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE',
# 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6',
# 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64',
# 'SEED', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent'
# or any <NAME> for which there is a Crypt::Cipher::<NAME> module
# $ctr_mode ..... [integer] CTR_COUNTER_LITTLE_ENDIAN (0) - little-endian counter (DEFAULT)
# CTR_COUNTER_BIG_ENDIAN (1) - big-endian counter
# CTR_COUNTER_LITTLE_ENDIAN | 2 (2) - little-endian + RFC 3686 initial-counter-incrementing
# CTR_COUNTER_BIG_ENDIAN | 2 (3) - big-endian + RFC 3686 initial-counter-incrementing
# $ctr_width .... [integer] counter width in bytes (DEFAULT = full block width, e.g. 16 for AES)
# $cipher_rounds ... [integer] optional, number of rounds for given cipher
bestie look at that cipher list. 'AES', 'Anubis', 'Blowfish', 'CAST5', 'Camellia', 'DES', 'DES_EDE', 'KASUMI', 'Khazad', 'MULTI2', 'Noekeon', 'RC2', 'RC5', 'RC6', 'SAFERP', 'SAFER_K128', 'SAFER_K64', 'SAFER_SK128', 'SAFER_SK64', 'SEED', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent' β or literally any <NAME> that has a Crypt::Cipher::<NAME> module. so many icons, all valid π
And the $ctr_mode values, spelled out with their numbers so you can't mess it up:
CTR_COUNTER_LITTLE_ENDIAN(0)β little-endian counter. this is the DEFAULT πCTR_COUNTER_BIG_ENDIAN(1)β big-endian counterCTR_COUNTER_LITTLE_ENDIAN | 2(2)β little-endian plus RFC 3686 initial-counter-incrementingCTR_COUNTER_BIG_ENDIAN | 2(3)β big-endian plus RFC 3686 initial-counter-incrementing
$ctr_width is [integer] β counter width in bytes, and the DEFAULT = full block width (so like 16 for AES, iykyk).
$cipher_rounds is [integer], optional β number of rounds for the given cipher.
π₯ encrypt
Encrypts the plaintext in a single call. Returns the ciphertext as a binary string. easy.
But here's the tea on how the scalar gets handled π: the plaintext scalar is converted to bytes using Perl's usual scalar stringification. Defined scalars β including numbers and string-overloaded objects β are accepted. undef is treated as an empty string and may emit Perl's usual "uninitialized value" warning. so like, defined scalars only, unless you want the warning, in which case, live your truth π
my $ciphertext = $m->encrypt($plaintext, $key, $iv);
π decrypt
Decrypts the ciphertext in a single call. Returns the plaintext as a binary string.
Same deal as encrypt, fr fr: the ciphertext scalar is converted to bytes using Perl's usual scalar stringification. Defined scalars, including numbers and string-overloaded objects, are accepted. undef is treated as an empty string and may emit Perl's usual "uninitialized value" warning.
my $plaintext = $m->decrypt($ciphertext, $key, $iv);
π
start_encrypt
Initializes encryption mode. Returns the object itself (so it chains, cute).
$m->start_encrypt($key, $iv);
π§ start_decrypt
Initializes decryption mode. Returns the object itself (same energy).
$m->start_decrypt($key, $iv);
β add
Feeds data to the encryption or decryption stream. Returns a binary string. this is the streaming girlie of the family π
Every argument is converted to bytes using Perl's usual scalar stringification. Defined scalars, including numbers and string-overloaded objects, are accepted. undef is treated as an empty string and may emit Perl's usual "uninitialized value" warning. (yes, same warning discourse, we've been over this π)
# in encrypt mode
my $ciphertext = $m->add($plaintext);
# in decrypt mode
my $plaintext = $m->add($ciphertext);
π finish
okay so this is lowkey the funniest one: CTR is a streaming mode and does not use padding, so "finish" returns an empty string. It exists purely for API consistency with Crypt::Mode::CBC and Crypt::Mode::ECB, and may be safely called or omitted. it's giving "I'm here for moral support" π
$m->start_encrypt($key, $iv);
my $ciphertext = '';
$ciphertext .= $m->add($chunk1);
$ciphertext .= $m->add($chunk2);
$ciphertext .= $m->finish; # returns ''
π SEE ALSO π
- CryptX, Crypt::Cipher
- Crypt::Cipher::AES, Crypt::Cipher::Blowfish, ... (you get the idea, the whole squad)
- <https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_.28CTR.29>
okay that's the whole page, you're officially edu-macated πβ¨
and for the cute sign-off: this glorious thing is perl v5.42.0, dated 2026-05-11, and it's the Crypt::Mode::CTR(3) manpage β go forth and encrypt responsibly, bestie ππ¦πΈ
π¦ where this page comes from
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- opensuse-leap 16.0: perl-CryptX 0.89.0-bp160.1.1 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms