0

我可能只是简单地看待这个问题,我不确定我想做的事情是否可行,但希望这可以节省大量时间,不必读入和读出不同的文件。

我想做的是使用反引号来接收 OpenSLL exe 的输出,它将加密字符串的某个部分。然后使用输出将其添加到另一个文件中。OpenSLL 会自动输出到一个文本文件。

例如:

Sample text: 1234, test text
ID- 1234, 
Encrypted text- test text

届时将

ID- 1234, 
Returned encrypted text- **&&**&&
And then be output to a file
1234, **&&**&&

我知道这可以通过读入和解析加密文本然后将输出附加到一个新文件来完成,但是当我希望对 100 或 1000 行执行此操作时,我希望避免这种情况,因为这将是很多额外的加工。

希望您能看到我的想法,如果有人可以提供帮助,将不胜感激。

#!/usr/bin/perl -w
use strict;

my $OpenSLLFile = "\\openssl.exe";

my $publickey = "public.pem";
my $privatekey = "private.pem";
my $inputtext = "output.txt";
my $outputtext = "abcd.txt";
my $compID = "1232";

my $encryptiontext = "openssl rsautl -encrypt -pubin -inkey $publickey -in $inputtext -out $outputtext";
my $decryptiontext = "openssl rsautl -decrypt -inkey $privatekey -in encrypted.txt -out plaintext.txt";

#Idea 1
#my $test = `$encryptiontext`;

#Idea 2    
#my $test = system ("$encryptiontext");

#Idea 3
`$encryptiontext`
my $test =  $?;

open (outputtest, '>>outputtest.txt') or die "$!";

my $a = "$compID, $test";

print outputtest "$a\n";

close(outputtest);
4

2 回答 2

1

您拼凑在一起的 openssl 命令正在写入某些输出文件(-out参数后面的文件)。因此,如果您想获得输出,您有两种选择:

  1. 从 openssl 写入的文件中读取输出
  2. 让 openssl 写入标准输出并使用反引号运算符直接在 Perl 中捕获它。

第二个版本避免了临时文件和与之相关的所有麻烦(例如,必须删除它们,无论是否失败;确保没有解密的内容留在文件系统中其他用户可以读取的位置等)。

-out ...通过从命令中删除参数,这应该是微不足道的。然后你可以像你的想法一样捕捉文本:

my $encrypt_commnad = "openssl rsautl -encrypt -pubin -inkey $publickey -in $inputtext";
my $encrypted_text  = `$encrypt_command`;
于 2012-10-16T09:50:08.570 回答
1

There are a number of modules on CPAN that do OpenSSL encryption and decryption. It would probably be easier to use one of those modules than trying to roll your own with system commands.

于 2012-10-16T10:00:01.967 回答