On 28 May 2002 07:37:24 -0000, [EMAIL PROTECTED] (Postman Pat) wrote:

>Greetings,
>I am looking for basic examples of how I would go about doing encryption 
>using rijandael. For instance, I wanna take a text file $file and encrypt 
>it with a given key $key and write it to file $cryptfile.
>
>Any ideas?
>

Yeah, the basic problem you face is that the Crypt::Rijndael module
needs it's key to be 32 bytes and the data must have a blocksize
of 16 bytes.  This adds just enough difficulty to make this module hard
for beginners.  

The following is an example of setting a simple 32 byte
key, and a sub which will give your data a blocksize of 16 bytes.

Watch the wordwrap when you copy and paste it, also you may need
a dos2unix conversion.
##########################################################
#!/usr/bin/perl
use Crypt::Rijndael;

$key = chr(0) x 32; #key is all nulls here
substr($key, 0, 1) = chr(1); #put 1 chr(1)in key just for fun

print "key->$key\n";

$plaintext= "adrqwqqqqqqqqqqqqqqqqqqqqqqwrxcq4gfq3g2q45g2q43g5";
print "plaintext->$plaintext\n";
$plaintext16= get16($plaintext);
print "plaintext16->$plaintext16\n";


$cbc = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC;
$crypted = $cbc->encrypt($plaintext16);
print 'crypted->',"$crypted\n"; #keep encrypted string in different print string
#to avoid character corruption of preceding string

$cbc = new Crypt::Rijndael $key, Crypt::Rijndael::MODE_CBC;
$decrypted = $cbc->decrypt($crypted);
print "decrypted->$decrypted\n";

#this sub makes all data blocksize of 16 bytes.
sub get16 {
my $data = shift;
print "data=$data\n";
return "\0" x ( 16 - length($data)%16 ) . $data;
}

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




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to