in reply to Multimedia file encryption : Crypt::CBC or Crypt::Rijndael

Try this:
#!/usr/bin/perl use warnings; use strict; use Crypt::Rijndael; # keys must be 128, 192 or 256 "bits" long, # 8 bits in a letter, so 8 x 16 = 128 #my $key = 'abcdefgghjkloiuy'; #128 bits or 8 x 16 my $key = 'abcdefgghjkloiuyabcdefgghjkloiuy'; #256 bits or 8 x 32 print "key->$key\n"; my $plaintext= "adrqwqqqqqqqqqqqqqqqqqqqqqqwrxcq4gfq3g2q45g2q43g5"; print "plaintext->$plaintext\n"; my $plaintext16= get16($plaintext); print "plaintext16->$plaintext16\n"; my $cbc = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC; my $crypted = $cbc->encrypt($plaintext16); print 'crypted->',"$crypted\n"; #keep encrypted string in different pr +int string #to avoid character corruption of prec +eding string my $cbc1 = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC; my $decrypted = $cbc1->decrypt($crypted); print "decrypted->$decrypted\n"; #this sub makes all data blocksize of 16 bytes. sub get16 { my $data = shift; return $data . "\0" x ( 16 - length($data)%16 ); } exit; #You can also use this to get data that is in a multiple of 16 bytes: # this requires $username < 16 bytes and prefix packs with spaces # sprintf '%16s', $username; #or you can xor it # $filecrypted = $cipher->encrypt($file^("\0"x16)); #To strip any padding after decryption: #my $plaintext = $cipher->decrypt($secret_stuff); # $plaintext =~ s/^\0+//; # remove leading null bytes, use \s (not \0) for spaces

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Multimedia file encryption : Crypt::CBC or Crypt::Rijndael
by ikegami (Patriarch) on Nov 15, 2010 at 00:53 UTC

    If you used Crypt::CBC,

    • You wouldn't have to pad the file.
    • You wouldn't have to make the key exactly the right size. What you pass is actually a passphrase from which a key of the right length is created.

    By the way, your padding method is buggy. It will trim trailing NUL bytes in the data. Crypt::CBC uses a method that allows perfect recovery of the original.

      Dear Monks,

      I completely changed my script , using some examples mentioned used in Crypt::CBC

      I believe, this code is working fine for me. But, just wanted to get the confirmation from you guys.

      my $start=time(); use Crypt::CBC; use strict vars; use strict; use warnings; my $key="abcdefgghjkloiuy"; my $cipher = Crypt::CBC->new(-key => $key, -cipher => 'Rijndael', -salt => 1, ) || die "Couldn't create CBC object"; open(F,"Robo.mp4"); $cipher->start('encrypting'); while (read(F,my $buffer,1024)) { $cipher->crypt($buffer); } $cipher->finish; my $end=time(); print "\n".$end-$start;

      IF the code is completely correct, can someone suggest a better way to calculate the time for execution, which also shows the milliseconds too..