Problem with Crypt::CBC

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. 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...
    2. 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 ...
    3. 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...
    4. 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');...
    5. 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...
  3. #2

    Default Re: Problem with Crypt::CBC

    Kevin wrote:
    >
    > 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;
    >
    That way you're getting a random (different) iv each time you run the
    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

  4. #3

    Default Re: Problem with Crypt::CBC

    Kevin wrote:
    > 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.
    That should work out of the box with Crypt::CBC.
    >
    > 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.
    Be careful: you have to make sure you are (re)storing binary data.
    You can use pack/unpack with 'H*' or MIME::Base64 to convert to text.

    [...]
    > 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'});
    Get rid of all the unneccessary parameters. A simple:

    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]
    >
    > The encrypted string never looks the same twice; consequently, the
    > decrypted string that gets returned is not the same as the original input
    > string.
    You got that wrong. The encrypted data should not be the same for
    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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139