...and stuff it into this (in LocalSites::Crypt):sub get_magic_token { my $self=shift; my $token=LocalSites::Crypt::encrypt_string(join '|',$self->id(), +$self->username(), $self->password()); return $token; }
$crypt_string is then used as a GET parameter in a link in the email message. When clicked, it's picked up by this:sub encrypt_string { my $clear_string=shift; my ($prepend, $key ) = &_secret_vals(); $key = pack( "H32", $key ); my $cipher=new Crypt::CBC( $key, 'IDEA' ); my $crypt_string=$cipher->encrypt_hex( $clear_string ); return $crypt_string; }
sub decode_magic_token { my $self=shift; my $token=shift; return 0 unless $token; my $decrypted=LocalSites::Crypt::decrypt_string($token); my ($id,$username,$password)=split /\|/, $decrypted; return $id; }
All of this works beautifully when everything is working beautifully.
The problem comes when the user comes back with a bad token. If, for example, the token is truncated, the script fails with this error:
"Ciphertext does not begin with a valid header for 'salt' header mode at (library_path)/LocalSites/Crypt.pm line 42"
Line 42 is the decrypt_hex line in the following subroutine, also in LocalSites::Crypt
sub decrypt_string { my $crypt_string=shift; my ( $prepend, $key ) = &_secret_vals(); $key = pack( "H32", $key ); my $cipher=new Crypt::CBC( $key, 'IDEA' ); my $clear_string=$cipher->decrypt_hex( $crypt_string ); return $clear_string; }
I've tried putting the decrypt_string() code in an eval() in the hopes of trapping the error:
sub decode_magic_token { my $self=shift; my $token=shift; return 0 unless $token; my ($id, $username, $password); eval { my $decrypted=LocalSites::Crypt::decrypt_string($token); ($id,$username,$password)=split /\|/, $decrypted; }; if (@$) { return 0; } else { return $id; } }
But this doesn't help for reasons that I should probably understand but don't.
If I could have some advice on how to fix this, I'd really appreciate it.
In reply to Crypt::CBC and error trapping by Kuroth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |