Crypt-CBC
view release on metacpan or search on metacpan
lib/Crypt/CBC.pm view on Meta::CPAN
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.
=head2 crypt()
$ciphertext = $cipher->crypt($plaintext);
After calling start(), you should call crypt() as many times as
necessary to encrypt the desired data.
=head2 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();
=head2 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.
=head2 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.
=head2 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. B<encrypt_hex($plaintext)> is exactly
equivalent to B<unpack('H*',encrypt($plaintext))>. These functions
can be useful if, for example, you wish to place the encrypted in an
email message.
=head2 filehandle()
This method returns a filehandle for transparent encryption or
decryption using Christopher Dunkle's excellent L<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;
=head2 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.
=head2 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.
=head2 iv()
$iv = $cipher->iv();
$cipher->iv($new_iv);
As above, but using a single method call.
=head2 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.
=head2 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.
=head2 passphrase()
$passphrase = $cipher->passphrase();
$cipher->passphrase($new_passphrase);
This gets or sets the value of the B<passphrase> passed to new() when
B<literal_key> is false.
( run in 3.242 seconds using v1.01-cache-2.11-cpan-7fcb06a456a )