Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Contact Us
Online Help
Domain Status
Man Pages

Virtual Servers

Topology Map

Server Agreement
Year 2038

USA Flag



Man Pages

Manual Reference Pages  -  CRYPT::ECB (3)

.ds Aq ’


Crypt::ECB - Encrypt Data using ECB Mode



Use Crypt::ECB OO style

  use Crypt::ECB;

  $crypt = Crypt::ECB->new;
  $crypt->cipher(Blowfish) || die $crypt->errstring;

  $enc = $crypt->encrypt("Some data.");
  print $crypt->decrypt($enc);

or use the function style interface

  use Crypt::ECB qw(encrypt decrypt encrypt_hex decrypt_hex);

  $ciphertext = encrypt($key, Blowfish, "Some data", PADDING_AUTO);
  $plaintext  = decrypt($key, Blowfish, $ciphertext, PADDING_AUTO);

  $hexcode = encrypt_hex($key, $cipher, $plaintext);
  $plain   = decrypt_hex($key, $cipher, $hexcode);


This module is a Perl-only implementation of the ECB mode. In combination with a block cipher such as DES, IDEA or Blowfish, you can encrypt and decrypt messages of arbitrarily long length. Though for security reasons other modes than ECB such as CBC should be preferred. See textbooks on cryptography if you want to know why.

The functionality of the module can be accessed via OO methods or via standard function calls. Remember that some crypting module like for example Blowfish has to be installed. The syntax follows that of Crypt::CBC.


new(), key(), cipher(), padding()

  $crypt = Crypt::ECB->new;
  $crypt->cipher(Blowfish) || die $crypt->errstring;

  print $crypt->key;
  print $crypt->cipher;
  print $crypt->padding;

  $crypt = Crypt::ECB->new(Some_key,Blowfish);
  $crypt->cipher || die "Blowfish wasnt loaded for some reason.";

<B>B>new()<B>B> initializes the variables it uses. Optional parameters are key and cipher. If called without parameters you have to call <B>B>key()<B>B> and <B>B>cipher()<B>B> before you can start crypting. If called with key but without cipher, for compatibility with Crypt::CBCDES’ is assumed.

<B>B>key()<B>B> sets the key if given a parameter. It always returns the key. Note that some crypting modules require keys of definite length. For example the Crypt::Blowfish module expects an eight byte key.

<B>B>cipher()<B>B> sets the block cipher to be used if given a parameter. It tries to load the corresponding module. If an error occurs, it returns 0 and sets $crypt->{Errstring}. Otherwise it returns the cipher name. Free packages available for Perl are for example Blowfish, DES or IDEA. If called without parameter it just returns the name of the cipher.

<B>B>padding()<B>B> sets the way how data is padded up to a multiple of the cipher’s blocksize. Until now two ways are implemented: When set to PADDING_NONE, no padding is done. You then have to take care of correct padding (and truncating) yourself. When set to PADDING_AUTO, the ECB module handles padding (and truncating when decrypting) the same way Crypt::CBC does.

By default the padding style is set to PADDING_NONE. This means if you don’t bother and your data has not the correct length, the module will complain and therefore force you to think about what you really want.

start(), mode(), crypt(), finish()

  $crypt->start(encrypt) || die $crypt->errstring;
  $enc .= $crypt->crypt($_) foreach (@lines);
  $enc .= $crypt->finish;

  print $crypt->mode;

<B>B>start()<B>B> sets the crypting mode and checks if all required variables like key and cipher are set. Allowed parameters are any words starting either with ’e’ or ’d’. The Method returns the mode which is set or 0 if an error occurred.

<B>B>mode()<B>B> is called without parameters and just returns the mode which is set.

<B>B>crypt()<B>B> processes the data given as argument. If called without argument $_ is processed. The method returns the processed data. Cipher and key have to be set in order to be able to process data. If some of these are missing or <B>B>start()<B>B> was not called before, the method dies.

After having sent all data to be processed to <B>B>crypt()<B>B> you have to call <B>B>finish()<B>B> in order to flush data that’s left in the buffer.


  $crypt->caching(1); # caching on
  $crypt->caching(0); # caching off

  print $crypt->caching;

The caching mode is returned. If given an argument caching mode is set. Caching is on if <B>B>caching()<B>B> evaluates true, otherwise caching is off. By default caching is on.

What is this caching? The Crypt::ECB module communicates with the cipher module via some object. Creating the cipher object takes some time for the cipher module has to do some initialization. Now caching means that the same cipher object is used until caching is turned off or the key or the cipher module are changed. If caching is off, a new cipher object is created is created each time <B>B>crypt()<B>B> or <B>B>finish()<B>B> are called and destroyed at the end of these methods. Crypting using caching is <B>muchB> faster than without caching.

encrypt(), decrypt(), encrypt_hex(), decrypt_hex()

  $enc = $crypt->encrypt($data);
  print $crypt->decrypt($enc);

  $hexenc = $crypt->encrypt_hex($data);
  print $crypt->decrypt_hex($hexenc);

<B>B>encrypt()<B>B> and <B>B>decrypt()<B>B> are convenience methods which call <B>B>start()<B>B>, <B>B>crypt()<B>B> and <B>B>finish()<B>B> for you.

<B>B>encrypt_hex()<B>B> and <B>B>decrypt_hex()<B>B> are convenience functions that operate on ciphertext in a hexadecimal representation. They are exactly equivalent to

  $hexenc = join(,unpack(H*,$crypt->encrypt($data)));
  print $crypt->decrypt(pack(H*,$hexenc));

These functions can be useful if, for example, you wish to place the encrypted information into an e-mail message, Web page or URL.


  print $crypt->errstring;

Some methods like <B>B>cipher()<B>B> or <B>B>start()<B>B> return 0 if an error occurs. You can then retrieve a more detailed error message by calling $crypt->errstring.


Variables which could be of interest to the outside world are:


The variables should not be set directly, use instead the above described methods. Reading should not pose a problem.


The two constants naming the padding styles are exported by default:



For convenience en- or decrypting can also be done by calling ordinary functions. The functions are: <B>B>encrypt()<B>B>, <B>B>decrypt()<B>B>, <B>encrypt_hexB>, <B>decrypt_hexB>. The module is smart enough to recognize whether these functions are called in an OO context or not.

encrypt(), decrypt(), encrypt_hex(), decrypt_hex()

  $ciphertext = encrypt($key, $cipher, $plaintext, PADDING_AUTO);
  $plaintext  = decrypt($key, $cipher, $ciphertext, PADDING_AUTO);

  $ciphertext = encrypt_hex($key, $cipher, $plaintext, PADDING_AUTO);
  $plaintext  = decrypt_hex($key, $cipher, $ciphertext, PADDING_AUTO);

<B>B>encrypt()<B>B> and <B>B>decrypt()<B>B> process the provided text and return either the corresponding ciphertext (encrypt) or plaintext (decrypt). Data and padstyle are optional, but remember that by default no padding is done. If data is omitted, $_ is assumed.

<B>B>encrypt_hex()<B>B> and <B>B>decrypt_hex()<B>B> operate on ciphertext in a hexadecimal representation. Otherwise usage is the same as for <B>B>encrypt()<B>B> and <B>B>decrypt()<B>B>.


None that I know of.


The other block cipher modes CBC, CFB and OFB could be implemented.

Convenience encrypt and decrypt functions utilizing base64 encoding could be added.


This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


Christoph Appel (see for email address)


perl(1), Crypt::DES(3), Crypt::IDEA(3), Crypt::CBC(3)
Search for    or go to Top of page |  Section 3 |  Main Index

perl v5.20.3 ECB (3) 2008-07-16

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.