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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?