2012-02-16 04:57 阅读 17


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:


$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 );

$enc64 = bin2hex($cipherText);
echo $enc64 . "<br />";

while (strlen($cleartext) % 16 != 0) {
  $cleartext .= "\0";

$aes = new Crypt_AES();
$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 douzhong2954 2012-02-16 23:29

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

    点赞 评论 复制链接分享