dsa122870 2012-02-16 04:57
浏览 21
已采纳

使用mcrypt和phpseclib无法获得相同的加密结果

I had an implementation that encrypts an input string using mcrypt. Unfortunately I can't use this anymore because mcrypt isn't installed on the server and I can't install it. So I've had a look at phpseclib but unfortunately I don't get the same encrypted string. Here is my code:

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;

After running the script (on the development server that has mcrypt installed) I get the following output:

This a teststring :)

0fc60e5a06eca68d4aada496e0e83ea65806abfe7d8f72723da470e6c9e86372 0fc60e5a06eca68d4aada496e0e83ea65806abfe7d8f72723da470e6c9e8637231b74e99d9b729813e974f211550d175

As you can see both encrypted strings are identical. Almost. The one that has been encrypted with phpseclib is one block too long and I have no idea why. I've already tried different key and block sizes. I've also tried to use the Crypt_Rijndael class of phpseclib. Hope one of you can point me in the right direction.

  • 写回答

1条回答 默认 最新

  • douzhong2954 2012-02-16 23:29
    关注

    Copied comment: Have you checked that you are using the same padding in both versions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料