2

我试图在 Perl 中加密然后解密二进制文件但没有成功,解密的文件总是与原始文件不同。我尝试了 Blowfish 和 AES (Rijandael),这是一个简短的示例:

#!/usr/bin/perl
use Crypt::CBC;
use autodie;

my $cipher = Crypt::CBC->new(
    -key    => 'my secret key',
    -cipher => 'Blowfish',
    -header => 'none',
    -iv     => 'dupajasi'
);
$cipher->start('encrypting');
my $sourcefile = "fs9419105v001s.zip";

{
    open(my $OUTF, '>', "$sourcefile.perl.crypt");
    open(my $F, '<', $sourcefile);
    print "[?] Encrypting ... \n";
    while (read($F, $buffer, 1024)) {
        print $OUTF $cipher->crypt($buffer);
    }
    print {$OUTF} $cipher->finish;
    close($OUTF);
}

print "[?] Decrypting.,..... \n";
$cipher2 = Crypt::CBC->new(
    -key    => 'my secret key',
    -cipher => 'Blowfish',
    -header => 'none',
    -iv     => 'dupajasi'
);

{
    open(my $OUTF, '>', "$sourcefile.perl.decrypt");
    open(my $F, '<', "$sourcefile.perl.crypt");
    while (read($F, $buffer, 1024)) {
        print {$OUTF} $cipher2->decrypt($buffer);
    }
    print {$OUTF} $cipher2->finish;
    close($OUTF);
}

有人可以帮我找出问题所在吗?

4

2 回答 2

2

我可以确认这一点。我发现了一个提示:差异恰好发生在 1024 10 /400 16边界处。

> diff -u fs9419105v001s.zip.hex fs9419105v001s.zip.perl.decrypt.hex
--- fs9419105v001s.zip.hex
+++ fs9419105v001s.zip.perl.decrypt.hex
@@ -62,7 +62,7 @@
 03d0  5d d9 f2 f6 43 bb 3d 9d  92 aa 89 ca 75 dc 0e 51  ]...C.=. ....u..Q
 03e0  55 b2 1a e8 65 d5 29 ac  ca d9 a4 f8 1a cc 67 8b  U...e.). ......g.
 03f0  f9 1b 65 be bc 19 bf 51  e6 9f de 28 ef db db ff  ..e....Q ...(....
-0400  85 38 78 09 a7 62 9f 9d  08 db fc cb 13 90 b9 84  .8x..b.. ........
+0400  c6 a0 8b 75 f4 17 6b 64  08 db fc cb 13 90 b9 84  ...u..kd ........
 0410  f4 a8 30 d2 1d 19 52 f7  8e 84 09 a8 59 f3 4e 1e  ..0...R. ....Y.N.
 0420  3c 30 ca 6e 5b dc bb f3  48 fa 5d 3c b1 e0 64 07  <0.n[... H.]<..d.
 0430  61 98 9e a1 57 9a 69 d6  35 a7 95 5b 0d d7 31 c4  a...W.i. 5..[..1.

这更像是一个评论而不是一个答案,但不幸的是评论框不适合这个回复。

于 2010-12-02T15:13:51.700 回答
1
  1. 你应该打电话binmode

  2. 你错过 ->start('decrypting');了解密。

  3. 你应该使用crypt()not decrypt()。[ 是的,这很令人困惑 ] 请参阅 perldoc:

解密()

$plaintext = $cipher->decrypt($ciphertext)

这个方便的函数会为你运行 start()、crypt() 和 finish() 的整个序列,处理提供的密文并返回相应的明文。

解密例程应如下所示:

$cipher2 = Crypt::CBC->new(
    -key    => 'my secret key',
    -cipher => 'Blowfish',
    -header => 'none',
    -iv     => 'dupajasi'
);

$cipher2->start('de')

print "[?] Decrypting ... \n";

{
    open(my $OUTF, '>', "$sourcefile");
    open(my $F, '<', "$sourcefile.perl.crypt");
    binmode $OUTF;
    binmode $F;
    while (read($F, $buffer, 1024)) {
        print $OUTF $cipher2->crypt($buffer);
    }
    print $OUTF $cipher2->finish;
    close $OUTF;
}
于 2010-12-02T15:02:48.567 回答