Следующее использует Crypt :: CBC для соления, заполнения и сцепления, и оно использует Crypt :: Rijndael (AES) для шифрования.
use strict;
use warnings;
use feature qw( say );
use Crypt::CBC qw( );
sub encrypt {
my ($key, $plaintext) = @_;
my $iv = Crypt::CBC->random_bytes(16);
my $cipher = Crypt::CBC->new(
-cipher => 'Rijndael',
-literal_key => 1,
-key => $key,
-iv => $iv,
-header => 'none',
);
return $iv . $cipher->encrypt($plaintext);
}
sub decrypt {
my ($key, $ciphertext) = @_;
my $iv = substr($ciphertext, 0, 16, '');
my $cipher = Crypt::CBC->new(
-cipher => 'Rijndael',
-literal_key => 1,
-key => $key,
-iv => $iv,
-header => 'none',
);
return $cipher->decrypt($ciphertext);
}
{
my $key = Crypt::CBC->random_bytes(32);
say "Key: ", unpack "H*", $key;
my $expect = 'secret';
say "Plaintext: $expect";
my $ciphertext = encrypt($key, $expect);
say "Ciphertext: ", unpack "H*", $ciphertext;
my $got = decrypt($key, $ciphertext);
say "Plaintext: $got";
say $expect eq $got ? "ok" : "not ok";
}