我能够使用Blowfish和密码分组链接模式(CBC)让PHP和Perl协同工作。Blowfish密钥至少需要8个字符(即使Blowfish的最小长度是8位,Perl也不接受小于8个字符的密钥),最大56个字符。IV必须正好是8个字符,填充必须为null,因为PHP使用null填充。此外,PHP需要libmcrypt >= 2.4.9才能与Perl兼容。
PERL
----
use Crypt::CBC;
$cipher = Crypt::CBC->new( {'key' => 'my secret key',
'cipher'=> 'Blowfish',
'iv' => '12345678',
'regenerate_key' => 0,
'padding' => 'null',
'prepend_iv' => 0
});
$cc = 'my secret text';
$encrypted = $cipher->encrypt($cc);
$decrypted = $cipher->decrypt($encrypted);
print "encrypted : ".$encrypted;
print "<br>";
print "decrypted : ".$decrypted;
PHP
---
$cc = 'my secret text';
$key = 'my secret key';
$iv = '12345678';
$cipher = mcrypt_module_open(MCRYPT_BLOWFISH,'','cbc','');
mcrypt_generic_init($cipher, $key, $iv);
$encrypted = mcrypt_generic($cipher,$cc);
mcrypt_generic_deinit($cipher);
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,$encrypted);
mcrypt_generic_deinit($cipher);
echo "encrypted : ".$encrypted;
echo "<br>";
echo "decrypted : ".$decrypted;