可以';使用mcrypt和phpseclib进行加密不会得到相同的结果


Can't get the same result for encryption with mcrypt and phpseclib

我有一个使用mcrypt加密输入字符串的实现。不幸的是,我不能再使用这个了,因为服务器上没有安装mcrypt,我无法安装它。所以我看了phpseclib,但不幸的是,我没有得到相同的加密字符串。这是我的代码:

include('Crypt/AES.php');
$key256 = "1234567890123456";
$iv =  "6543210987654321";
$cleartext = "This a teststring :)";
echo $cleartext . "<br /><br />";
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
if (mcrypt_generic_init($cipher, $key256, $iv) != -1)
{
  // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
  $cipherText = mcrypt_generic($cipher,$cleartext );
  mcrypt_generic_deinit($cipher);
}
$enc64 = bin2hex($cipherText);
echo $enc64 . "<br />";
while (strlen($cleartext) % 16 != 0) {
  $cleartext .= "'0";
}
$aes = new Crypt_AES();
$aes->setKey($key256);
$aes->setIV($iv);
$cipherText = $aes->encrypt($cleartext);
$enc64 = bin2hex($cipherText);
echo $enc64;

运行脚本(在安装了mcrypt的开发服务器上)后,我得到以下输出:

这是一个测试字符串:)

0c60e5e6eca68d4a496e0e83ea65806abfe7f72723da470e6c9e86372

正如您所看到的,两个加密字符串是相同的。几乎用phpseclib加密的那个太长了一个块,我不知道为什么。我已经试过不同的键和块大小。我还尝试使用phpseclib的Crypt_Rijndael类。希望你们中的一个人能给我指明正确的方向。

复制的注释:您是否检查过在两个版本中使用相同的填充。