douzi2785 2017-11-27 10:11
浏览 55
已采纳

相当于PhpSecLib中的mcrypt_encrypt

I have an old piece of code that's written using mcrypt extension and I have to change it to phpseclib. But my code doesn't generate the same output as mcrypt function:

Old code:

$encryptedText =mcrypt_encrypt(
        MCRYPT_RIJNDAEL_256,
        $myKey,
        $data ,
        MCRYPT_MODE_CBC,
        $myIV
    );

My new code:

$aes = new \phpseclib\Crypt\AES(\phpseclib\Crypt\AES::MODE_CBC);
$aes->setKey($myKey);
$aes->setIV($myIV);
$aes->disablePadding();
$seclib = $aes->encrypt( $data );

but $encryptedText and $seclib are not the same.

  • 写回答

1条回答 默认 最新

  • doushi7761 2017-11-27 10:45
    关注

    You're equating Rijndael with AES, a common misconception.

    AES is only a subset of it - Rijndael-128, with the difference between AES variations being only the key size:

    • AES-128 is Rijndael-128 with a 128-bit key.
    • AES-256 is again Rijndael-128, but with a 256-bit key.

    The suffix number in Rijndael variations on the other hand, refers to both key size and block size, so of course you cannot get Rijndael-256 by doing AES, as you need a 256-bit block size.

    There's a page on the phpseclib docs, which generates sample code after you input the basic variables (cipher, mode, key size, bit size). It outputs the following for Rijndael, CBC, 256, 256:

    <?php
    include('Crypt/Rijndael.php');
    include('Crypt/Random.php');
    
    $cipher = new Crypt_Rijndael(); // could use CRYPT_RIJNDAEL_MODE_CBC
    $cipher->setBlockLength(256);
    // keys are null-padded to the closest valid size
    // longer than the longest key and it's truncated
    //$cipher->setKeyLength(256);
    $cipher->setKey('abcdefghijklmnopqrstuvwxyz123456');
    // the IV defaults to all-NULLs if not explicitly defined
    $cipher->setIV(crypt_random_string($cipher->getBlockLength() >> 3));
    
    $size = 10 * 1024;
    $plaintext = str_repeat('a', $size);
    
    echo $cipher->decrypt($cipher->encrypt($plaintext));
    

    I am not sure if the library actually supports this cipher without mcrypt availablity, but it should.


    I assume you are doing this because mcrypt is being dropped from PHP, and I strongly suggest that you change your strategy.

    Even if the above works, it would be quite slow when using a userland PHP implementation of the algorithm (something which is noted in the phpseclib docs), but more importantly - you'll have no other alternatives if this library stops working, is no longer maintained, etc. Non-AES variations of Rijndael are not ubiquitous, and there are more modern algorithms available today anyway (hint: libsodium being added to PHP 7.2).

    If I were you, I'd change the algorithm entirely. Of course, that would mean re-encrypting all of the data, but you'll have to do that eventually and now is really the best time to do it.

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

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗