从 Perl 中的字符串执行整个 Perl 程序

发布于 2024-12-08 20:23:53 字数 204 浏览 2 评论 0原文

我有一个在文件中用 Blowfish 加密的程序和第二个 perl 程序,提示输入用于将其解密为字符串的密码,我希望不必将解密的源写入硬盘驱动器,尽管将其保存在内存中并不是真正的问题,因为运行该程序的人已经知道其来源。我想我可能会使用 eval 但我需要运行的程序有很多使用 Curses 和东西的输入/输出,所以 eval 不会工作,因为它只返回最后一个值......有谁知道如何实现这一点?

I have a program that is encrypted with Blowfish in a file and a second perl program that prompts for a passphrase that is used to decrypt it into a string, I would like to not have to write the decrypted source to the hard drive ever, although having it in memory isn't really a problem as those running the program already know the source. I thought I might use eval but the program I need to run has a lot of input/output using Curses and stuff so eval wont work as it only returns the last value... Does anyone have any idea how this could be accomplished?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南烟 2024-12-15 20:23:53

您可以使用 @INC 挂钩来执行解密。然后您可以简单地requireuse加密的程序。例如,

# loader.pl
use Crypt::Rot13;
use File::Spec;
sub decrypt_line {
    if ($_ ne '') {
        my ($self, $state) = @_;
        my ($crypt, $key) = @$state;
        $crypt->charge($_);
        ($_) = $crypt->rot13($key);
        return 1;
    }
}
sub load_crypt {
    my ($self, $filename) = @_;
    print "Key?\n";
    chomp(my $key = <STDIN>);
    for my $prefix (@INC) {
        open my $fh, '<', File::Spec->catfile($prefix, "$filename.r13") or next;
        return ($fh, \&decrypt_line, [Crypt::Rot13->new(), $key]);
    }
}
BEGIN {
    unshift @INC, \&load_crypt;
}
require 'hello.pl';
# hello.pl.r13
cevag "Uryyb, jbeyq!\a";
$ perl loader.pl
Key?
13
Hello, world!

You can use an @INC hook to perform the decryption. Then you can simply require or use the encrypted program. For example,

# loader.pl
use Crypt::Rot13;
use File::Spec;
sub decrypt_line {
    if ($_ ne '') {
        my ($self, $state) = @_;
        my ($crypt, $key) = @$state;
        $crypt->charge($_);
        ($_) = $crypt->rot13($key);
        return 1;
    }
}
sub load_crypt {
    my ($self, $filename) = @_;
    print "Key?\n";
    chomp(my $key = <STDIN>);
    for my $prefix (@INC) {
        open my $fh, '<', File::Spec->catfile($prefix, "$filename.r13") or next;
        return ($fh, \&decrypt_line, [Crypt::Rot13->new(), $key]);
    }
}
BEGIN {
    unshift @INC, \&load_crypt;
}
require 'hello.pl';
# hello.pl.r13
cevag "Uryyb, jbeyq!\a";
$ perl loader.pl
Key?
13
Hello, world!
骄傲 2024-12-15 20:23:53

没有理由 eval 不适用于您所描述的内容。虽然它只返回一个值,但这并不能阻止评估代码与终端交互。通常不会以这种方式使用,但您的用例是使用字符串eval的合理原因。 (请注意,您最终仍可能将源代码写入交换文件。)

There's no reason eval won't work for what you describe. While it only returns a single value, that doesn't prevent the eval'd code from interacting with the terminal. It's not usually used that way, but your use-case is a legitimate reason for using string eval. (Note that you could still end up with the source code written to your swap file.)

表情可笑 2024-12-15 20:23:53

只需启动一个单独的 perl 实例并使用标准输入将程序传递给它,如下所示:

echo 'print 2+2 . "\n";' | perl -

使用 perl 代码:

open(P,"|perl -") || die "Failed: $!\n";                                                                                                                                                        
print P 'print 2+2 . "\n"'

Just start a separate perl instance and pass the program to it using standard input, like this:

echo 'print 2+2 . "\n";' | perl -

With perl code:

open(P,"|perl -") || die "Failed: $!\n";                                                                                                                                                        
print P 'print 2+2 . "\n"'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文