Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Crypt::Rijndael

by olly (Scribe)
on Apr 13, 2002 at 13:14 UTC ( [id://158769]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to encrypt stuff using Crypt::Rijndael, when I use the followwing code:
my $cipher = new Crypt::Rijndael "a" x 32, Crypt::Rijndael::MODE_CBC; my $usernameCrypt = $cipher->encrypt($username);
I get an error saying encrypt: datasize not multiple of blocksize (16 bytes) at /var/www/cgi-bin/development/sqlmanager/version0.1/cgi/sql.cgi line 328.
any ideas? Imagination is more important then knowledge -Einstein-

Replies are listed 'Best First'.
Re: Crypt::Rijndael
by RMGir (Prior) on Apr 13, 2002 at 14:19 UTC
    At a totally wild guess, I'd think that the message means exactly what it says, and Rijndael expects blocks of data that are multiples of 16 bytes in size.

    Try this:

    my $cipher = new Crypt::Rijndael "a" x 32, Crypt::Rijndael::MODE_CBC; my $usernameCrypt = $cipher->encrypt($username^("\0"x16));
    That should work, as long as length($username)<16.

    The trick here is that the XOR will extend "length($username)" without changing the values.

    Note that that's a) totally untested and b) may or may not be a secure way of using Rijndael, I have no idea.

    Of course, if "a"x32 is your key, the security of my trick is the least of your worries :)
    --
    Mike

    Edit:"\0" rather than '\0', that's what I get for working in C yesterday...

Re: Crypt::Rijndael
by tachyon (Chancellor) on Apr 13, 2002 at 15:44 UTC

    From the docs (see RTFM):

    $cipher->encrypt($data) Encrypt data. The size of $data must be a multiple of blocksize (16 bytes), otherwise this function will croak. Apart from that, it can be of (almost) any length.

    You can also use either of these to get data that is in a multiple of 16 bytes:

    # this requires $username < 16 bytes and prefix packs with spaces sprintf '%16s', $username; # this will give you a data length divisible by 16 for any length # we use null byte prefix packing sub get16 { my $data = shift; return "\0" x ( 16 - length($data)%16 ) . $data; }

    I really hope you are not using the POD example string 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' as your key. The Rijndael cipher is a symmetric key block cipher, which is a type of cipher that encrypts data in blocks, rather than a single bit at a time, and uses the same key for both encryption and decryption. If you know the key you can decrypt an encrypted block so this is also termed a secret key cipher. 'a' x 32 is not that secret :-)

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Crypt::Rijndael
by Ryszard (Priest) on Apr 14, 2002 at 12:45 UTC
    I'm interested as to why you are using Rijndael over a different cypher. Is it because it is the official AES algorithm?

    There is no doubt that the algorithm is strong, however, if you have chosen it soley on its AES merit, then perhaps this may not be the algorithm for you.

    Why do i say this? because there are other algorithms out there that may be easier for you to implement in perl ,not to mention the difference classes of encryption algorithms (asymmetric, symmetric), performance difference between different algorithms, or the type of data you want to encrypt.

    This node describes the difference between cypher block chaining and non cypherblock chaining and how to implement both in perl (via cpan modules). Here is a link to a bunch of cyptanalysis of different algorithms.

    In short a CBC will encrypt/decrypt a message of arbitrary length, which is what I'm guessing you'd like to do. Just becuase the algorithm isnt marketed as "strong" (ala AES) doesnt make it weak. Consider a runner up in the AES finals.

    Remember, dont always go for the marketing.

      Better late then never, the reason is Beatnik would whipp me if I didn't use it. He seems to be a big rijndael fan. :) Go saint beatnik Imagination is more important then knowledge -Einstein-
Re: Crypt::Rijndael
by olly (Scribe) on Apr 13, 2002 at 15:52 UTC
    How do I remove the added data at decrypt then? Imagination is more important then knowledge -Einstein-
      my $plaintext = $cipher->decrypt($secret_stuff); $plaintext =~ s/^\0+//; # remove leading null bytes, use \s (not \0) +for spaces

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Crypt::Rijndael
by sevensven (Pilgrim) on Apr 15, 2002 at 23:52 UTC

    Maybe a piece of code can help you.

    use Crypt::CBC; use strict; my $cipher = Crypt::CBC->new( { 'key' => 'a' x 32, # change this to a decent key wi +th 32 chars 'cipher' => 'Rijndael', 'iv' => 'm_?.5fP}', 'regenerate_key'=> 0, 'padding' => 'space', 'prepend_iv' => 0 } ); my $data_to_encrypt = 'what\'s up, doc ?'; my $cipheredtext = $cipher->encrypt($data_to_encrypt); print "ciphered : $cipheredtext \n"; my $plaintext = $cipher->decrypt($cipheredtext); print "plaintext : $plaintext\n"; $cipher->finish;

    Rijndael has implementations in several languages, so portability should not be an issue, but consider other, faster encryptions you should.

    -- sevensven or nana korobi, ya oki

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://158769]
Approved by jlk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found