如何使用 Perl 编写加密的 zip 存档

How to write encrypted zip archives with Perl

我想将机密信息发送给可能不太懂技术并且无法使用 GnuPG 或其他复杂加密技术的收件人。因此定义发送加密的 Zip 文件被认为是安全的™。 Zip 文件是首选,因为 Microsoft Windows 从 XP 开始支持开箱即用。

很遗憾,来自 CPAN 的 Archive::Zip 不支持写入 加密存档。

我们如何使用 Perl 编写加密的 Zip 文件?

我没有找到直接使用任何 Perl Zip 库的方法。所以我开始远程控制命令行 zip 实用程序。最好的方法似乎是来自 CPAN 的 Expect

#!/usr/bin/env perl

use strict;
use warnings;

use Expect;

`touch foo`;

my $zip = Expect->new;
$zip->raw_pty(1);       # behave more like a pipe, disable echoing
$zip->log_stdout(0);    # don't print out from `zip`
$zip->spawn(qw<zip --encrypt foo.zip foo>);

my $password = "secret";
my $timeout  = 1;

for ( 1 .. 2 ) {

    # don't use a regex ref like qr/foo/ but a string like q/foo/ or 'foo'!
    $zip->expect( $timeout, -re => q/password:/ )
      or die "zip didn't ask for password";
    $zip->send( $password, "\n" );
}

my $success = $zip->expect( $timeout, -re => q/foo \((deflated|stored) / );

$zip->soft_close;

print $success ? "success" : "fail", "\n";