Crypt::Mode::ECB(3)

πŸ’… Crypt::Mode::ECB β€” the manual, but make it make sense

okay so bestie, you're reading the Crypt::Mode::ECB(3) page from the User Contributed Perl Documentation section, and honestly? you're in the right place. let's get into it πŸŽ€


✨ NAME ✨

Crypt::Mode::ECB β€” it's the Block cipher mode ECB a.k.a. [Electronic codebook]. yes the square brackets are part of the vibe. no we don't know why either. πŸ¦‹


🌸 SYNOPSIS 🌸

here's the whole thing in one go, copy-paste ready, no cap:

use Crypt::Mode::ECB;
my $m = Crypt::Mode::ECB->new('AES');
my $key = '1234567890123456';
my $plaintext = 'example plaintext';
my $chunk1 = 'example ';
my $chunk2 = 'plaintext';

# encrypt or decrypt in one call
my $single_ciphertext = $m->encrypt($plaintext, $key);
my $single_plaintext = $m->decrypt($single_ciphertext, $key);

# encrypt more chunks
$m->start_encrypt($key);
my $chunked_ciphertext = '';
$chunked_ciphertext .= $m->add($chunk1);
$chunked_ciphertext .= $m->add($chunk2);
$chunked_ciphertext .= $m->finish;

# decrypt more chunks
$m->start_decrypt($key);
my $chunked_plaintext = '';
$chunked_plaintext .= $m->add($chunked_ciphertext);
$chunked_plaintext .= $m->finish;

like… it literally does the whole thing. one-shot call or chunked stream, your choice, both are iconic πŸ’…


πŸ’– DESCRIPTION πŸ’–

so this module implements ECB cipher mode. that's the whole job. ✨

quick note though: it only works with ciphers that come from CryptX (Crypt::Cipher::NNNN). no random ciphers, it's not gonna happen.

⚠️ BEWARE, bestie, for real: ECB is insecure by design. that's not me being dramatic, that's just the truth of it. if you're not 100% sure what you're doing, go for Crypt::Mode::CBC instead. please πŸ™ i'm begging you. 😭

πŸ”₯ METHODS πŸ”₯

unless the docs say otherwise, just assume $m is a mode object you already made with "new", like this:

my $m = Crypt::Mode::ECB->new('AES');

🧚 new

my $m = Crypt::Mode::ECB->new($name);
#or
my $m = Crypt::Mode::ECB->new($name, $padding);
#or
my $m = Crypt::Mode::ECB->new($name, $padding, $cipher_rounds);

# $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
# $padding .... [integer] 0 no padding (plaintext size has to be multiple of block length)
#               1 PKCS5 padding, Crypt::CBC's "standard" - DEFAULT
#               2 Crypt::CBC's "oneandzeroes"
#               3 ANSI X.923 padding
#               4 zero padding
#               5 zero padding (+a block of zeros if the output length is divisible by the blocksize)
# $cipher_rounds ... [integer] optional, number of rounds for given cipher

let me break down the tea β˜•:

  • $name β€” a [string], and it's 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 literally any <NAME> that has a Crypt::Cipher::<NAME> module out there. pick your fighter πŸ’…
  • $padding β€” an [integer], and it's giving choices:
    • 0 β€” no padding (heads up: plaintext size has to be multiple of block length, no exceptions)
    • 1 β€” PKCS5 padding, Crypt::CBC's "standard" β€” this one's the DEFAULT πŸ‘‘
    • 2 β€” Crypt::CBC's "oneandzeroes"
    • 3 β€” ANSI X.923 padding
    • 4 β€” zero padding
    • 5 β€” zero padding (+a block of zeros if the output length is divisible by the blocksize)
  • $cipher_rounds β€” an [integer], optional, number of rounds for the given cipher. fancy ✨

πŸ¦‹ encrypt

encrypts the plaintext in a single call. returns the ciphertext as a binary string. simple, clean, slay.

the plaintext scalar gets converted to bytes using Perl's usual scalar stringification. defined scalars are accepted β€” including numbers and string-overloaded objects, no judgment here. "undef" is treated as an empty string and may emit Perl's usual "uninitialized value" warning. so like… define your vars, bestie πŸ’€

my $ciphertext = $m->encrypt($plaintext, $key);

πŸ“ decrypt

decrypts the ciphertext in a single call. returns the plaintext as a binary string. the reverse of the above, obviously.

the ciphertext scalar gets converted to bytes using Perl's usual scalar stringification. defined scalars are accepted β€” numbers and string-overloaded objects included. "undef" is treated as an empty string and may emit Perl's usual "uninitialized value" warning.

my $plaintext = $m->decrypt($ciphertext, $key);

🌈 start_encrypt

initializes encryption mode. returns the object itself (so you can chain, iykyk).

$m->start_encrypt($key);

⭐ start_decrypt

initializes decryption mode. returns the object itself. twin of the one above 🫢

$m->start_decrypt($key);

πŸ’œ add

feeds data to the encryption or decryption stream. returns a binary string. this is the "chunk by chunk" girlie πŸ’…

each argument gets converted to bytes using Perl's usual scalar stringification. defined scalars are accepted, including numbers and string-overloaded objects. "undef" is treated as an empty string and may emit Perl's usual "uninitialized value" warning.

# in encrypt mode
my $ciphertext = $m->add($plaintext);

# in decrypt mode
my $plaintext = $m->add($ciphertext);

πŸŽ€ finish

finalizes the stream. in encryption mode it pads and encrypts the final block; in decryption mode it decrypts and removes padding from the final block. returns the result as a binary string β€” and yes, it may be empty if there's no data to flush. that's not a bug, that's just an empty vibe πŸ™ˆ

# encrypt more chunks
$m->start_encrypt($key);
my $chunk1 = 'example ';
my $chunk2 = 'plaintext';
my $ciphertext = '';
$ciphertext .= $m->add($chunk1);
$ciphertext .= $m->add($chunk2);
$ciphertext .= $m->finish;

# decrypt more chunks
$m->start_decrypt($key);
my $plaintext = '';
$plaintext .= $m->add($ciphertext);
$plaintext .= $m->finish;

πŸ‘‘ SEE ALSO πŸ‘‘

go read these, they're literally so good:


okay that's the whole page, you ate that πŸ’…βœ¨ this little masterpiece is perl v5.42.0, dated 2026-05-11, and it lives under Crypt::Mode::ECB(3) β€” go forth and maybe use CBC instead, ily πŸ«ΆπŸ’–

πŸ“¦ where this page comes from