Leser: 2
3 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::CBC;
$|++;
crypto('aes.pl', 'encrypting');
crypto('aes.pl', 'decrypting');
sub crypto {
my $file = shift;
my $action = shift;
my $suffix = $action eq 'encrypting' ? 'aes' : 'no';
my $cipher = Crypt::CBC->new(-key => 'pass',
-cipher => 'Rijndael');
my $buffer;
my $buffer_size = 1024;
open SOURCEFILE, '<', $file or die $!;
open DESTFILE, '>', "$file.$suffix" or die $!;
my $length = 0;
$cipher->start($action);
while ( read(SOURCEFILE, $buffer, $buffer_size) ) {
$buffer = $cipher->crypt($buffer);
syswrite(DESTFILE, $buffer, $buffer_size);
}
syswrite(DESTFILE, $cipher->finish, $buffer_size);
close DESTFILE;
close SOURCEFILE or die $!;
return 1;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#!/usr/bin/perl use strict; use warnings; use PerlIO::via::CBC; warn "Not enough arguments, use:\n./aes.pl password de-/encrypt file_name\n" if scalar @ARGV < 3; my ($password, $action, $file_name) = @ARGV; start_up($action, $file_name); sub start_up { my $action = shift; my $file_name = shift; unless (-f $file_name) { print "Can't find $file_name.\n"; exit 0; } crypto($file_name, $action); } sub crypto { my $file = shift; my $action = shift; my $op = $action eq 'encrypt' ? '>' : '<'; my $suffix = $action eq 'encrypt' ? '.aes' : ''; PerlIO::via::CBC->config( 'key' => $password, 'cipher' => 'Rijndael', ); my $fh; open($fh, "$op:via(PerlIO::via::CBC)", "$file$suffix"); if ($action eq 'encrypt') { open(SOURCE, '<', $file) or die $!; print $fh $_ while(sysread(SOURCE, $_, 1024)); close SOURCE; } else { print <$fh>; } close $fh; return 1; }
1
2
3
4
5
6
7
8
9
10
11
styx@styx-laptop:~/programmierung/perl/tests$ perl aes.pl pw encrypt aes.pl
styx@styx-laptop:~/programmierung/perl/tests$ perl aes.pl pw decrypt aes.pl.aes
#!/usr/bin/perl
use strict;
use warnings;
use PerlIO::via::CBC;
warn "Not enough arguments, use:\n./aes.pl password de-/encrypt file_name\n" if scalar @ARGV < 3;
.
.
.
3 Einträge, 1 Seite |