Scroll to navigation

Crypt::Cipher(3) User Contributed Perl Documentation Crypt::Cipher(3)

NAME

Crypt::Cipher - Generic interface to cipher functions

SYNOPSIS

   #### example 1 (encrypting single block)
   use Crypt::Cipher;
   my $key = '1234567890abcdef'; # 16 bytes, valid for AES-128
   my $plaintext_block = '1234567890123456'; # one AES block (16 bytes)
   my $c = Crypt::Cipher->new('AES', $key);
   my $blocksize  = $c->blocksize;
   my $ciphertext = $c->encrypt($plaintext_block);   # encrypt 1 block
   my $plaintext  = $c->decrypt($ciphertext);         #decrypt 1 block
   ### example 2 (using CBC mode)
   use Crypt::Mode::CBC;
   my $cbc_key = '1234567890abcdef'; # 16 bytes, valid for AES-128
   my $iv = 'fedcba0987654321';      # 16 bytes
   my $cbc = Crypt::Mode::CBC->new('AES');
   my $cbc_ciphertext = $cbc->encrypt("secret data", $cbc_key, $iv);
   #### example 3 (compatibility with Crypt::CBC)
   use Crypt::CBC;
   use Crypt::Cipher;
   my $compat_key = '1234567890abcdef'; # 16 bytes, valid for AES-128
   my $compat_iv = 'fedcba0987654321';  # 16 bytes
   my $cipher = Crypt::Cipher->new('AES', $compat_key);
   my $compat_cbc = Crypt::CBC->new( -cipher=>$cipher, -iv=>$compat_iv );
   my $compat_ciphertext = $compat_cbc->encrypt("secret data");

DESCRIPTION

Provides an interface to various symmetric cipher algorithms.

Note: This module only implements single-block encryption and decryption. For general data, use a block mode such as Crypt::Mode::CBC, Crypt::Mode::CTR, or Crypt::CBC (which is slower).

METHODS

Unless noted otherwise, assume $c is an existing cipher object created via "new", for example:

 my $c = Crypt::Cipher->new('AES', '1234567890abcdef');

new

Constructor. Returns a reference to the cipher object.

 ## basic scenario
 my $c = Crypt::Cipher->new($name, $key);
 # $name = 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', 'SM4', 'Skipjack', 'Twofish', 'XTEA', 'IDEA', 'Serpent'
 #                or any <NAME> for which there is a Crypt::Cipher::<NAME> module
 # $key = binary key (keysize should comply with selected cipher requirements)
 ## some of the ciphers (e.g. MULTI2, RC5, SAFER) allow one to set number of rounds
 my $c = Crypt::Cipher->new('MULTI2', $key, $rounds);
 # $rounds = positive integer (should comply with selected cipher requirements)

encrypt

Encrypts $plaintext and returns $ciphertext. An empty string is accepted and returned unchanged; otherwise $plaintext must be exactly blocksize bytes long.

 my $ciphertext = $c->encrypt($plaintext);

decrypt

Decrypts $ciphertext and returns $plaintext. An empty string is accepted and returned unchanged; otherwise $ciphertext must be exactly blocksize bytes long.

 my $plaintext = $c->decrypt($ciphertext);

keysize

Just an alias for max_keysize (needed for Crypt::CBC compatibility).

max_keysize

Returns the maximum allowed key size in bytes for the given cipher.

 $c->max_keysize;
 #or
 Crypt::Cipher->max_keysize('AES');
 #or
 Crypt::Cipher::max_keysize('AES');

min_keysize

Returns the minimum allowed key size in bytes for the given cipher.

 $c->min_keysize;
 #or
 Crypt::Cipher->min_keysize('AES');
 #or
 Crypt::Cipher::min_keysize('AES');

blocksize

Returns the block size in bytes for the given cipher.

 $c->blocksize;
 #or
 Crypt::Cipher->blocksize('AES');
 #or
 Crypt::Cipher::blocksize('AES');

default_rounds

Returns the default number of rounds for the given cipher. Only some ciphers, such as MULTI2, RC5, and SAFER, let you set the number of rounds via new().

 $c->default_rounds;
 #or
 Crypt::Cipher->default_rounds('AES');
 #or
 Crypt::Cipher::default_rounds('AES');

SEE ALSO

  • CryptX
  • See subclasses such as Crypt::Cipher::AES, Crypt::Cipher::Blowfish, ...
2026-05-11 perl v5.42.1