Crypt-CBC
view release on metacpan or search on metacpan
optional block cipher algorithm.
start()
$cipher->start('encrypting');
$cipher->start('decrypting');
The start() method prepares the cipher for a series of encryption or
decryption steps, resetting the internal state of the cipher if
necessary. You must provide a string indicating whether you wish to
encrypt or decrypt. "E" or any word that begins with an "e" indicates
encryption. "D" or any word that begins with a "d" indicates decryption.
crypt()
$ciphertext = $cipher->crypt($plaintext);
After calling start(), you should call crypt() as many times as
necessary to encrypt the desired data.
finish()
$ciphertext = $cipher->finish();
The CBC algorithm must buffer data blocks internally until they are even
multiples of the encryption algorithm's blocksize (typically 8 bytes).
After the last call to crypt() you should call finish(). This flushes
the internal buffer and returns any leftover ciphertext.
In a typical application you will read the plaintext from a file or
input stream and write the result to standard output in a loop that
might look like this:
$cipher = new Crypt::CBC('hey jude!');
$cipher->start('encrypting');
print $cipher->crypt($_) while <>;
print $cipher->finish();
encrypt()
$ciphertext = $cipher->encrypt($plaintext)
This convenience function runs the entire sequence of start(), crypt()
and finish() for you, processing the provided plaintext and returning
the corresponding ciphertext.
decrypt()
$plaintext = $cipher->decrypt($ciphertext)
This convenience function runs the entire sequence of start(), crypt()
and finish() for you, processing the provided ciphertext and returning
the corresponding plaintext.
encrypt_hex(), decrypt_hex()
$ciphertext = $cipher->encrypt_hex($plaintext)
$plaintext = $cipher->decrypt_hex($ciphertext)
These are convenience functions that operate on ciphertext in a
hexadecimal representation. encrypt_hex($plaintext) is exactly
equivalent to unpack('H*',encrypt($plaintext)). These functions can be
useful if, for example, you wish to place the encrypted in an email
message.
filehandle()
This method returns a filehandle for transparent encryption or
decryption using Christopher Dunkle's excellent Crypt::FileHandle
module. This module must be installed in order to use this method.
filehandle() can be called as a class method using the same arguments as
new():
$fh = Crypt::CBC->filehandle(-cipher=> 'Blowfish',
-pass => "You'll never guess");
or on a previously-created Crypt::CBC object:
$cbc = Crypt::CBC->new(-cipher=> 'Blowfish',
-pass => "You'll never guess");
$fh = $cbc->filehandle;
The filehandle can then be opened using the familiar open() syntax.
Printing to a filehandle opened for writing will encrypt the data.
Filehandles opened for input will be decrypted.
Here is an example:
# transparent encryption
open $fh,'>','encrypted.out' or die $!;
print $fh "You won't be able to read me!\n";
close $fh;
# transparent decryption
open $fh,'<','encrypted.out' or die $!;
while (<$fh>) { print $_ }
close $fh;
get_initialization_vector()
$iv = $cipher->get_initialization_vector()
This function will return the IV used in encryption and or decryption.
The IV is not guaranteed to be set when encrypting until start() is
called, and when decrypting until crypt() is called the first time.
Unless the IV was manually specified in the new() call, the IV will
change with every complete encryption operation.
set_initialization_vector()
$cipher->set_initialization_vector('76543210')
This function sets the IV used in encryption and/or decryption. This
function may be useful if the IV is not contained within the ciphertext
string being decrypted, or if a particular IV is desired for encryption.
Note that the IV must match the chosen cipher's blocksize bytes in
length.
iv()
$iv = $cipher->iv();
$cipher->iv($new_iv);
As above, but using a single method call.
key()
$key = $cipher->key();
$cipher->key($new_key);
Get or set the block cipher key used for encryption/decryption. When
encrypting, the key is not guaranteed to exist until start() is called,
and when decrypting, the key is not guaranteed to exist until after the
first call to crypt(). The key must match the length required by the
underlying block cipher.
When salted headers are used, the block cipher key will change after
each complete sequence of encryption operations.
salt()
$salt = $cipher->salt();
$cipher->salt($new_salt);
Get or set the salt used for deriving the encryption key and IV when in
OpenSSL compatibility mode.
passphrase()
$passphrase = $cipher->passphrase();
$cipher->passphrase($new_passphrase);
This gets or sets the value of the passphrase passed to new() when
literal_key is false.
$data = random_bytes($numbytes)
Return $numbytes worth of random data, using Crypt::URandom, which will
read data from the system's source of random bytes, such as
/dev/urandom.
( run in 0.995 second using v1.01-cache-2.11-cpan-d8267643d1d )