嘿,我正在编写一个使用@INC钩子来解密来自河豚的真实perl源的程序。我有一个非常恼人的问题,并没有显示出使用警告或我的任何标准技巧......基本上当我创建新的密码对象时,循环跳转到@INC中的下一个对象而没有错误或任何......我不知道该怎么做!
#!/usr/bin/perl -w
use strict;
use Crypt::CBC;
use File::Spec;
sub load_crypt {
my ($self, $filename) = @_;
print "Key?\n: ";
chomp(my $key = <STDIN>);
for my $prefix (@INC) {
my $buffer = undef;
my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish');
my $derp = undef;
$cipher ->start('decrypting');
open my $fh, '<', File::Spec->($prefix, "$filename.nc") or next;
while (read($fh,$buffer,1024)) {
$derp .= $cipher->crypt($buffer);
}
$derp .= $cipher->finish;
return ($fh, $derp);
}
}
BEGIN {
unshift @INC, \&load_crypt;
}
require 'gold.pl';
此外,如果我将实际密钥放在初始化方法中,它仍然会失败
答案 0 :(得分:4)
你在这里遇到了很多问题。首先,你使用的是File :: Spec错误。其次,您将返回一个已经在文件末尾的文件句柄,以及一个不是有效返回值的字符串。 (另外,我把关键提示放在了钩子之外。)
#!/usr/bin/perl -w
use strict;
use Crypt::CBC;
use File::Spec;
# Only read the key once:
print "Key?\n: ";
chomp(my $key = <STDIN>);
sub load_crypt {
my ($self, $filename) = @_;
return unless $filename =~ /\.pl$/;
for my $prefix (@INC) {
next if ref $prefix;
#no autodie 'open'; # VERY IMPORTANT if you use autodie!
open(my $fh, '<:raw', File::Spec->catfile($prefix, "$filename.nc"))
or next;
my $buffer;
my $cipher = Crypt::CBC->new( -key => $key, -cipher => 'Blowfish');
my $derp;
$cipher->start('decrypting');
while (read($fh,$buffer,1024)) {
$derp .= $cipher->crypt($buffer);
}
$derp .= $cipher->finish;
# Subroutine writes 1 line of code into $_ and returns 1 (false at EOF):
return sub { $derp =~ /\G(.*\n?)/g and ($_ = $1, 1) };
}
return; # Didn't find the file; try next @INC entry
} # end load_crypt
# This doesn't need a BEGIN block, because we're only using the hook
# with require, and that's a runtime operation:
unshift @INC, \&load_crypt;
require 'gold.pl';