我正在尝试编写与以下php代码段匹配的perl代码:
<?php
$key = 'dd7b47ad2d87b93013a89878fef3f6c5';
$str = 'Hello this is my sensative text string for testing';
$iv = '1234123412341234';
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CFB, $iv);
list(,$ciphertext_h) = unpack('H*', $ciphertext );
echo $ciphertext_h . "\n";
?>
当我运行此代码时,我得到以下密码字符串:
67a15d847220b5417263724032e8981366fb92ae73db1a88edf16da54fe81ac056a2a4c7f9e1f335467d7bf16d6bafe49341
我尝试过使用六种不同的Perl模块,包括Crypt :: CFB,Crypt :: GCrypt和Crypt :: Rijndael。我确定不匹配正在发生,因为选项不一样。我正在努力尝试正确设置选项。我试过的是这个:
use Crypt::CFB;
my $key = 'dd7b47ad2d87b93013a89878fef3f6c5';
my $iv = "1234123412341234";
my $str = "Hello this is my sensative text string for testing";
my $cipher = Crypt::CFB->new($key, 'Crypt::Rijndael', $iv);
my $cipherStr = $cipher->encrypt($str);
print(unpack( 'H*', $cipherStr ) . "\n" );
生成以下密文:
af129a803585990d2887307361d8ce451ec4fce94fb031b15e570d2ee818ab34b030189676171e7dd5fa74a517470fae08eb
我似乎无法更改任何Crypt :: CFB选项。当我尝试时,我得到实例化错误......
我还尝试了Crypt :: Rijndael:
use Crypt::Rijndael;
my $key = 'dd7b47ad2d87b93013a89878fef3f6c5';
my $iv = "1234123412341234";
my $str = "Hello this is my sensative text string for testing";
# pad the $str to a multiple of 16 bytes:
if (my $distance = length($str) % 16) {
$str .= "\0" x (16 - $distance);
}
my $crypt = Crypt::Rijndael->new($key, Crypt::Rijndael::MODE_CFB);
$crypt->set_iv($iv);
my $binary = $crypt->encrypt($str);
print( unpack( 'H*', $binary ) . "\n" );
生成以下密文:
6746b5e4bdde2888f0b2da506176669e846c11a94ca425b4f8b92363030fac374cb9dd7d3a68a740d284530c025d17c0de06ff96563b40cda5deea0324d754a9
密文更长,因为我必须填充数据。如果数据没有填充,我收到错误。仍然与PHP代码不匹配。我想我离我更近了,但却错过了一些东西。有没有人有任何想法。我当然会很感激!