S
SlimClity
I'm trying to encrypt file's using the Perl Crypt::Rijndeal module,
see encrypt.pl and decrypt.pl. Because the data must be in blocks of
16 I use "get16". Both scripts work, I can encrypt and decrypt files.
But the decrypted file is corrupt, eg. encrypt word file --> decrypt
--> File is corrupt.
I think this has something to do with the get16, when decrypting I
think I must remove those "\0" which were added (how??).... Or is it
something else??
-- encrypt.pl --
use Crypt::Rijndael;
$cipher = new Crypt::Rijndael "1234567890123456",
Crypt::Rijndael::MODE_CBC;
$srcfile = "doc.doc";
$destfile = "doc.enc";
#my ($srcfile, $destfile) = @_;
my $buffer;
open INF, $srcfile
or die "\nCan't open $srcfile for reading: $!\n";
open OUTF, ">$destfile"
or die "\nCan't open $destfile for writing: $!\n";
binmode INF;
binmode OUTF;
while (
read (INF, $buffer, 1024) # read in (up to) 64k chunks, write
and print OUTF $cipher->encrypt(get16($buffer)) # exit if read or
write fails
) {};
die "Problem copying: $!\n" if $!;
close OUTF
or die "Can't close $destfile: $!\n";
close INF
or die "Can't close $srcfile: $!\n";
sub get16 {
my $data = shift;
return "\0" x ( 16 - length($data)%16) . $data;
}
-- end of encrypt.pl --
-- decrypt.pl --
use Crypt::Rijndael;
$cipher = new Crypt::Rijndael "1234567890123456",
Crypt::Rijndael::MODE_CBC;
$srcfile = "doc.enc";
$destfile = "doc-new.doc";
#my ($srcfile, $destfile) = @_;
my $buffer;
open INF, $srcfile
or die "\nCan't open $srcfile for reading: $!\n";
open OUTF, ">$destfile"
or die "\nCan't open $destfile for writing: $!\n";
binmode INF;
binmode OUTF;
while (
read (INF, $buffer, 65536) # read in (up to) 64k chunks, write
and print OUTF $cipher->decrypt($buffer) # exit if read or write
fails
) {};
die "Problem copying: $!\n" if $!;
close OUTF
or die "Can't close $destfile: $!\n";
close INF
or die "Can't close $srcfile: $!\n";
sub get16 {
my $data = shift;
return "\0" x ( 16 - length($data)%16) . $data;
}
-- end of decrypt.pl --
see encrypt.pl and decrypt.pl. Because the data must be in blocks of
16 I use "get16". Both scripts work, I can encrypt and decrypt files.
But the decrypted file is corrupt, eg. encrypt word file --> decrypt
--> File is corrupt.
I think this has something to do with the get16, when decrypting I
think I must remove those "\0" which were added (how??).... Or is it
something else??
-- encrypt.pl --
use Crypt::Rijndael;
$cipher = new Crypt::Rijndael "1234567890123456",
Crypt::Rijndael::MODE_CBC;
$srcfile = "doc.doc";
$destfile = "doc.enc";
#my ($srcfile, $destfile) = @_;
my $buffer;
open INF, $srcfile
or die "\nCan't open $srcfile for reading: $!\n";
open OUTF, ">$destfile"
or die "\nCan't open $destfile for writing: $!\n";
binmode INF;
binmode OUTF;
while (
read (INF, $buffer, 1024) # read in (up to) 64k chunks, write
and print OUTF $cipher->encrypt(get16($buffer)) # exit if read or
write fails
) {};
die "Problem copying: $!\n" if $!;
close OUTF
or die "Can't close $destfile: $!\n";
close INF
or die "Can't close $srcfile: $!\n";
sub get16 {
my $data = shift;
return "\0" x ( 16 - length($data)%16) . $data;
}
-- end of encrypt.pl --
-- decrypt.pl --
use Crypt::Rijndael;
$cipher = new Crypt::Rijndael "1234567890123456",
Crypt::Rijndael::MODE_CBC;
$srcfile = "doc.enc";
$destfile = "doc-new.doc";
#my ($srcfile, $destfile) = @_;
my $buffer;
open INF, $srcfile
or die "\nCan't open $srcfile for reading: $!\n";
open OUTF, ">$destfile"
or die "\nCan't open $destfile for writing: $!\n";
binmode INF;
binmode OUTF;
while (
read (INF, $buffer, 65536) # read in (up to) 64k chunks, write
and print OUTF $cipher->decrypt($buffer) # exit if read or write
fails
) {};
die "Problem copying: $!\n" if $!;
close OUTF
or die "Can't close $destfile: $!\n";
close INF
or die "Can't close $srcfile: $!\n";
sub get16 {
my $data = shift;
return "\0" x ( 16 - length($data)%16) . $data;
}
-- end of decrypt.pl --