Greetings docbrown25,
From what I can tell from my very limited playing with Crypt::CBC is that to do what you want, you'll need to generate an initialization vector per each encryption, then prepend that IV to the head of the blob before encoding. You'll have to reverse that process of course when decoding and decrypting.
Here's an example bit of code that randomly generates IVs, then uses these to encrypt/encode a URL. Then the decode/decrypt process pulls the IV off the head of the data.
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::CBC;
use MIME::Base64::URLSafe qw( urlsafe_b64encode urlsafe_b64decode );
my $key = 'secret';
sub url_encode {
my ( $string, $iv ) = @_;
return urlsafe_b64encode( $iv . Crypt::CBC->new(
'-key' => $key,
'-header' => 'none',
'-iv' => $iv,
'-cypher' => 'Blowfish',
)->encrypt($string) );
}
sub url_decode {
my $b64 = urlsafe_b64decode( $_[0] );
return Crypt::CBC->new(
'-key' => $key,
'-header' => 'none',
'-iv' => substr( $b64, 0, 8 ),
'-cypher' => 'Blowfish',
)->decrypt( substr( $b64, 8 ) );
}
foreach ( 1 .. 5 ) {
my $iv = join( '', map { [ 'a' .. 'z' ]->[ rand(26) ] } ( 1 .. 8 )
+ );
my $en_string = url_encode( 'http://perlmonks.org', $iv );
my $de_string = url_decode($en_string);
print $iv, ' : ', $en_string, ' => ', $de_string, "\n";
}
Hope this helps.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.