我正在尝试加密然后在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);
}
有人可以帮我找出问题所在吗?
答案 0 :(得分: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.
这更多是评论而不是答案,但不幸的是评论框不适合此回复。
答案 1 :(得分:1)
您应致电binmode
你错过了
->start('decrypting');
解密。
您应该使用crypt()
而不是decrypt()
。 [是的,这很令人困惑]请参阅perldoc:
解密()
$plaintext = $cipher->decrypt($ciphertext)
此 便利功能贯穿整个 start(),crypt()和 完成()为你,处理 提供密文并返回 相应的明文。
解密例程应如下所示:
$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;
}