Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: Encrypt String Crypt::CBC by gryphon
in thread Encrypt String Crypt::CBC by docbrown25

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2020-12-02 18:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How often do you use taint mode?





      Results (44 votes). Check out past polls.

      Notices?