Schrift
[thread]10889[/thread]

Crypt::Rijndael und die blocksize

Leser: 5


<< >> 5 Einträge, 1 Seite
styx-cc
 2007-11-25 04:11
#102969 #102969
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Hallo, ich habmir zum spielen was gebastelt, allerdings bin ich auf ein Prolem gestossen, und zwar verschluesselt mir das Script die Strings nur wenn deren Groesse durch die blocksize (Standartmaessig 16 byte) teilbar ist anderfalls gibts ne Fehlermeldung "encrypt: datasize not multiple of blocksize (16 bytes) at aes.txt line 15.":

Code: (dl )
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
#!/usr/bin/perl

use strict;

use warnings;

use Crypt::Rijndael;
use Digest::MD5 qw/md5_hex/;


my $password = $ARGV[0] || 1;
my $cipher = Crypt::Rijndael->new(md5_hex($password), Crypt::Rijndael::MODE_CBC() );


print decrypt( encrypt('testtesttesttest') );

sub encrypt {
my $plain = shift;
#$plain must be a multiple of 16 (in bytes)
my $crypted = $cipher->encrypt($plain);
return $crypted;
}

sub decrypt {
my $crypted = shift;
my $plain = $cipher->decrypt($crypted);

return $plain;
}


Wie bekomme ich nun variierende Strings dazu immer durch 16bytes teilbar zu sein, ohne sie zu veraendern?

MfG
Pörl.
ptk
 2007-11-25 12:37
#102973 #102973
User since
2003-11-28
3645 Artikel
ModeratorIn
[default_avatar]
Du könntest ein Füllbyte nehmen, welches nicht in den Daten vorkommt, z.B. das Nullbyte. Natürlich muss man es auf der anderen Seite wieder entfernen. Oder du merkst dir die Länge des Strings und schneidest die überflüssigen Bytes wieder heraus.
GwenDragon
 2007-11-25 13:42
#102974 #102974
User since
2005-01-17
14533 Artikel
Admin1
[Homepage]
user image
Quote
keysize

Returns the keysize, which is 32 (bytes). The Rijndael cipher actually supports keylengths of 16, 24 or 32 bytes, but there is no way to communicate this to Crypt::CBC.
http://search.cpan.org/~bdfoy/Crypt-Rijndael-1.05/...

und da CPAN:Crypt-CBC verwendet wird, das eine Blockgröße von 16 Byte standardgemäß verwendet.

Quote
When the last block of plaintext is shorter than the block size, it must be padded. Padding methods include: "standard" (i.e., PKCS#5), "oneandzeroes", "space", and "null".

http://search.cpan.org/~lds/Crypt-CBC-2.24/CBC.pm#...

Du musst also "nachfüllen".

Warum rufts du nicht gleich Crypt-CBC auf?
die Drachin, Gwendolyn


Unterschiedliche Perl-Versionen auf Windows (fast wie perlbrew) • Meine Perl-Artikel

styx-cc
 2007-11-25 16:44
#102983 #102983
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Vielen Dank, habs jetzt mit Crypt-CBC gemacht, klappt wunderbar:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::CBC;
use Digest::MD5 qw/md5_hex/;

my $password = $ARGV[0] || 1;
my $cipher = Crypt::CBC->new(-key    => md5_hex($password),
                             -cipher => 'Rijndael');

print decrypt( encrypt('gfg') );

sub encrypt {
  my $plain = shift;
  #$plain must be a multiple of 16 (in bytes)
  my $crypted = $cipher->encrypt($plain);
  return $crypted;
}

sub decrypt {
 my $crypted = shift;
 my $plain = $cipher->decrypt($crypted);
 return $plain;
}
Pörl.
styx-cc
 2007-11-25 20:37
#102987 #102987
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Soo, hab jetzt noch ein wenig rumgespielt und stehe vor dem naechsten Problem, welches ich nicht aleine geloest bekomme. Und zwar versuche ich Dateien zu verschluesseln, das klappt auch fast komplett, aber am Ende schneidet er mir immer ein paar Bytes weg und ich find nicht raus woran es liegt..

Code: (dl )
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
43
44
45
46
47
48
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::CBC;
use Digest::MD5 qw/md5_hex/;

warn "Not enough arguments, use\naes.txt <pass> <file> <de-/encrypt>\n" if scalar @ARGV < 3;
my ($password, $file_name, $action) = @ARGV;
start_up($action, 0, $file_name);

sub start_up {
my $action = shift;
my $clean_up = shift;
my $file_name = shift;

my $is_file = -f $file_name ? 1 : 0;
return unless $is_file;
$action = $action eq 'e' ? 'encrypting' : 'decrypting';
crypto($file_name, $clean_up, $action);
}

sub crypto {
my $file = shift;
my $clean_up = shift;
my $action = shift;

my $suffix = $action eq 'encrypting' ? 'aes' : 'no';
my $cipher = Crypt::CBC->new(-key => md5_hex($password),
-cipher => 'Rijndael');
my $buffer;
my $buffer_size = 1024;
print "Begin $action $file...";
$cipher->start($action);
open(SOURCEFILE, $file) or die $!;
open DESTFILE, '>', "$file.$suffix" or die $!;
while (sysread(SOURCEFILE,$buffer,$buffer_size)) {
$buffer = $cipher->crypt($buffer);
print DESTFILE $buffer;
}
close DESTFILE;
close SOURCEFILE or die $!;
$cipher->finish;
print " done!\n";
if ($clean_up) {
#...
}
return 1;
}

Das kann man ueber die Kommandozeile aufrufen:
Code: (dl )
perl crypt.pl passwort file e

e fuer encrypt, d fuer decrypt.

Ich habs schon mit read probiert, das print DESTFILE $buffer; durch ein syswrite ersetzt etc. half alles nichts.
Vielen Dank

edit:
Hab jetzt noch einen kleinen Fehler behoben, allerdings klappt das gnaze immer noch nicht:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
  open SOURCEFILE, '<', $file or die $!;  
open DESTFILE, '>', "$file.$suffix" or die $!;
$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 $!;

Hauptsächlich gehts um "syswrite(DESTFILE, $cipher->finish, $buffer_size);", um den Rest aus demPuffer auch in die Datei zu schreiben.
Pörl.
<< >> 5 Einträge, 1 Seite



View all threads created 2007-11-25 04:11.