我能够使用 Blowfish 和密码块链接让 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 = '我的秘密密钥';
$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;
echo "<br>";
echo "解密后: ".$decrypted;