Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Multimedia file encryption : Crypt::CBC or Crypt::Rijndael

by sundeep (Acolyte)
on Nov 14, 2010 at 06:39 UTC ( [id://871291]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,

I wanted to encrypt a video/audio file using AES file encryption. I tried the following code , with different combination and tried to get the output, but i was not successful. Can , someone please help me with the multimedia file encryption please

use MIME::Base64; use Crypt::Rijndael; use bytes; use Crypt::CBC; open (FILE,"<Inception.avi"); $_ = <FILE>; my $in = decode_base64($_); print $in ; my $key = pack("H*", "01020304050607080910111213141516"); # my $cipher = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC or +die "Error: $!"; # my $cipher = Crypt::CBC->new(-cipher => 'Rijndael',-key=>$key); $cipher->set_iv(substr($in, 0, 16)); print "\n"; print "out = '", unpack("N/A", $cipher->decrypt(substr($in, 16))),"'\n +"; #print "out = '", unpack("N/A", $cipher->decrypt($in)), "'\n"; print $cipher; close FILE;

Thanks in advance

Replies are listed 'Best First'.
Re: Multimedia file encryption : Crypt::CBC or Crypt::Rijndael
by ikegami (Patriarch) on Nov 14, 2010 at 07:34 UTC
    Where's the encryption?
      I commented the 2 lines of $cipher. First , i used in Crypt::CBC and the next time i used Crypt::Rijndael. Both, didn't work fine.
        That does not answer my question.
Re: Multimedia file encryption : Crypt::CBC or Crypt::Rijndael
by ig (Vicar) on Nov 14, 2010 at 17:06 UTC

    There are examples in Crypt::CBC synopsis, including an example of encrypting a "large" file, such as your multimedia file might be. Have you tried using Crypt::CBC as suggested there?

Re: Multimedia file encryption : Crypt::CBC or Crypt::Rijndael
by zentara (Archbishop) on Nov 14, 2010 at 22:31 UTC
    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

      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..

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found