Boring has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a problem on string compare:

use Crypt::CBC; use MIME::Base64; my $key = '0000000123456789009876543210abcdefghijklmnopqrstuvwxyz'; my $iv = '87654321'; my $cipt = 'hED7MTyAXjY='; #encrypted string "u_ser_a" my $cmp = "u_ser_a"; $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish', -iv => $iv, -header => 'none', -padding => 'standard', -literal_key => 'true', -keysize => length($key), ); $clt = $cipher->decrypt(decode_base64($cipt)); if ($cmp eq $clt) { print "correct!\n"; } else { print "not match!n"; }

However, the result is "not match!".

I have no idea why the output is "not match".

Please advice.

Thank you.

Replies are listed 'Best First'.
Re: compare string problem
by jethro (Monsignor) on Dec 10, 2010 at 09:42 UTC

    Add some Data::Dumper lines and you will see

    ... if ($cmp eq $clt) { print "correct!\n"; } else { use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper(\$cmp,\$clt); } # Output: $VAR1 = \"u_ser_a"; $VAR2 = \"u_ser_a\0";
      YES!!!!

      You all are right.

      There is a "\0" at the end of the $clt.

      Thank you everyone/monks.

Re: compare string problem
by moritz (Cardinal) on Dec 10, 2010 at 09:37 UTC
Re: compare string problem
by Anonymous Monk on Dec 10, 2010 at 09:35 UTC
    Its obvious, its because they are not equal :) Try
    use Data::Dumper; print Dumper( [ $cmp, $clt ] );
    to learn how they're different