Ask a Question related to PERL Modules, Design and Development.
-
Kevin #1
Problem with Crypt::CBC
I know I'm missing something, but I'll be damned if I can figure it out...
I want to create a simple sub-routine to encrypt and (later) decrypt a
variable length string. The key for encryption and decryption is
absolutely known to be the same; it is created from the concatenation of
some other data which does not change. However, the key will be unique to
each instance of the encrypt/decrypt.
The goal is to store the encrypted string in a Mysql database, and have
the ability to use the decryption sub-routine to decrypt it at a future
time.
I've tried making the "$key" a reference in the $cipher creation hash
(i.e., 'key' => \$key), but that didn't help.
And yes, Crypt::Blowfish is installed. :-)
Here is the code I have:
#==============================#
# Encrypt
#==============================#
#! /usr/local/bin/perl -w
use strict;
use Crypt::CBC;
my $key = <some key>;
my $cipher = Crypt::CBC->new( {
'key' => $key,
'cipher' => 'Blowfish',
'prepend_iv' => 0,
'regenerate_key' => 0,
'padding' => 'null'});
my $encrypted = $cipher->encrypt($ARGV[0]);
open (FH, "> string.txt");
print FH $encrypted;
close FH;
exit;
#==============================#
# Decrypt
#==============================#
#! /usr/local/bin/perl -w
use strict;
use Crypt::CBC;
my $key = <some key>;
my $cipher = Crypt::CBC->new( {
'key' => $key,
'cipher' => 'Blowfish',
'prepend_iv' => 0,
'regenerate_key' => 0,
'padding' => 'null'});
my $encrypted = $cipher->encrypt($ARGV[0]);
open (FH, "< string.txt");
def $/;
my $encrypted = <FH>;
close FH;
my $decrypted = $cipher->decrypt($encrypted);
print $decrypted, "\n";
exit;
The encrypted string never looks the same twice; consequently, the
decrypted string that gets returned is not the same as the original input
string.
Thoughts, anyone?
KJJ
Kevin Guest
-
Crypt::DH Crypt::Random install problem
The Linux Test Project (ltp http://ltp.sourceforge.net/) perl file autoltp requires Net::SSH and Net::SFTP These ultimately require Crypt::DH which... -
Crypt:DES make problem on Solaris 9
Hi, I'm trying to install Crypt::DES module on a Solaris9 box and have the following problem when running make: gcc -c ... -
crypt function in PHP different from Perl's crypt?
Why returns the crypt function a longer strin than Perls crypt? I need the same length (8 chars) for a password field link its used in the... -
crypt problem
Hi, I have a weird problem with the crypt function. If I do: <?php echo(CRYPT_SALT_LENGTH); echo(CRYPT_MD5); echo crypt("xxxxxxxx", 'aa');... -
A Crypt-xDBM_File problem?
#!d:/usr/bin/perl.exe -w use strict; use Fcntl; use DB_File; use Crypt::Blowfish; use Crypt::xDBM_File; use MLDBM qw(DB_File); my %hash; tie... -
Sisyphus #2
Re: Problem with Crypt::CBC
Kevin wrote:
That way you're getting a random (different) iv each time you run the>
> Here is the code I have:
>
> #==============================#
> # Encrypt
> #==============================#
> #! /usr/local/bin/perl -w
> use strict;
> use Crypt::CBC;
>
> my $key = <some key>;
> my $cipher = Crypt::CBC->new( {
> 'key' => $key,
> 'cipher' => 'Blowfish',
> 'prepend_iv' => 0,
> 'regenerate_key' => 0,
> 'padding' => 'null'});
>
> my $encrypted = $cipher->encrypt($ARGV[0]);
>
> open (FH, "> string.txt");
> print FH $encrypted;
> close FH;
>
> exit;
>
script. Hence you get different cyphertext each time. Specify an 'iv' in
the new() constructor (there's an example at the start of the Crypt::CBC
documentation) and you will get the same cyphertext each time.
Cheers,
Rob
--
To reply by email u have to take out the u in kalinaubears.
Sisyphus Guest
-
Thomas Kratz #3
Re: Problem with Crypt::CBC
Kevin wrote:
That should work out of the box with Crypt::CBC.> I know I'm missing something, but I'll be damned if I can figure it out...
>
> I want to create a simple sub-routine to encrypt and (later) decrypt a
> variable length string. The key for encryption and decryption is
> absolutely known to be the same; it is created from the concatenation of
> some other data which does not change. However, the key will be unique to
> each instance of the encrypt/decrypt.
Be careful: you have to make sure you are (re)storing binary data.>
> The goal is to store the encrypted string in a Mysql database, and have
> the ability to use the decryption sub-routine to decrypt it at a future
> time.
You can use pack/unpack with 'H*' or MIME::Base64 to convert to text.
[...]
Get rid of all the unneccessary parameters. A simple:> Here is the code I have:
>
> #==============================#
> # Encrypt
> #==============================#
> #! /usr/local/bin/perl -w
> use strict;
> use Crypt::CBC;
>
> my $key = <some key>;
> my $cipher = Crypt::CBC->new( {
> 'key' => $key,
> 'cipher' => 'Blowfish',
> 'prepend_iv' => 0,
> 'regenerate_key' => 0,
> 'padding' => 'null'});
my $cipher = Crypt::CBC->new({
key => $key,
cipher => 'Blowfish',
}) or return;
will do. (Your problem is the prepend_iv set to 0).
[rest of code snipped]
You got that wrong. The encrypted data should not be the same for>
> The encrypted string never looks the same twice; consequently, the
> decrypted string that gets returned is not the same as the original input
> string.
different calls to encrypt(). This is by design.
Thomas
--
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</ #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#.... >>e>r^..>l^...>k^..
Thomas Kratz Guest



Reply With Quote

