CPAN模块访问OpenSSL(en / de)crypt(密码)操作

时间:2014-06-04 08:02:33

标签: perl openssl cpan

我正在寻找一个perl(CPAN)模块来访问OpenSSL的(en / de)加密功能。完全等同于通过openssl enc ...(使用 openssl 二进制文件)管道但没有子进程的fork / exec开销的东西。

到目前为止,我能找到的所有模块对实现SSL或TLS更感兴趣,并且具有所有相关的通信和开销。我知道Crypt::CBC以及各种兼容的密码模块,例如Crypt::OpenSSL::AES,但是它在perl中完成大部分工作,一次只调用一个块的底层(C)库,这太慢了。

1 个答案:

答案 0 :(得分:1)

我相信你正在寻找与之相当的Perl:

<强>加密

$ openssl enc -bf -in file.txt -out file.bf
enter bf-cbc encryption password:
Verifying - enter bf-cbc encryption password:

$ cat file.bf
Salted__��k^,�2�.�t�af/

<强>解密

$ openssl enc -bf -d -in file.bf -out file.txt-2
enter bf-cbc decryption password:

$ cat file.txt-2 
test

根据https://www.openssl.org/docs/apps/enc.html


Perl等价物将是:

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

use Crypt::CBC;
use IO::All;
use IO::Prompter;

my $in_file = shift || die "Usage: $0 'input file'";

my $password = prompt('enter bf-cbc encryption password:', -echo => '');

my $check_password = prompt('Verifying - enter bf-cbc encryption password:', -echo => '');

if ($password ne $check_password) {
    die "Verify failure - passwords do not match\n";
}

my $file_contents = io($in_file)->slurp;

my $cipher = Crypt::CBC->new(
    -key => $password,
    -cipher => 'OpenSSL::Blowfish',
);

my $encrypted = $cipher->encrypt($file_contents);

say $encrypted;

say "-" x 80;

my $decrypted = $cipher->decrypt($encrypted);

say $decrypted;

exit 0;

其中输出以下内容:

$ $ cat file.txt
test

$ ./openssl-enc-perl.pl file.txt
enter bf-cbc encryption password:
Verifying - enter bf-cbc encryption password:
Salted__�>4�>
=1vn6�
--------------------------------------------------------------------------------
test

很明显,它不是来自以下的strace输出:

% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
 23.68    0.002480           8       295       220 stat
 16.52    0.001730          14       122           read
  8.32    0.000871          12        71         3 open
  6.21    0.000650          14        45           mmap
  6.01    0.000629           8        75        48 ioctl
  5.30    0.000555           5       105         3 lseek
  4.92    0.000515          64         8           select
  4.56    0.000478           7        68           close
  3.91    0.000409          29        14           write
  3.91    0.000409          12        34           mprotect
  3.63    0.000380           8        48           brk
  3.08    0.000323           4        74         1 rt_sigaction
  1.73    0.000181          20         9         9 access
  1.72    0.000180           8        22           fstat
  0.89    0.000093           4        22           geteuid
  0.78    0.000082          10         8           rt_sigprocmask
  0.77    0.000081           4        20           getegid
  0.75    0.000079           4        18           getuid
  0.74    0.000078          39         2           munmap
  0.74    0.000077          26         3         1 execve
  0.70    0.000073           4        18           getgid
  0.33    0.000035          35         1           readlink
  0.27    0.000028          14         2           arch_prctl
  0.22    0.000023          23         1           lstat
  0.17    0.000018           5         4           fcntl
  0.15    0.000016           4         4           getgroups
------ ----------- ----------- --------- --------- ----------------
100.00    0.010473                  1093       285 total

此示例要求:

  1. IO::All
  2. IO::Prompter
  3. Crypt::CBC
  4. Crypt::OpenSSL::Blowfish
  5. 但实际上你只需要两个Crypt模块。如果您愿意,可以使用纯粹的Perl等效替换IO模块。