mcrypt警告但仍解密数据

Ian*_*ood 13 php mcrypt

我在这堂课中有点奇怪:

<?php
namespace lib;

/**
 * Short description of Crypt
 *
 * @author xxxx
 * @package
 */
class Encryption
{
    /**
     * Short description of _ch
     * handle to the mcrypt resource
     *
     * @access private
     * @var $_ch
     */
    private $_ch;

    /**
     * Short description of __construct
     *
     * @access public
     * @author xxxx
     * @param
     * @return void
     */
    public function __construct( $keyData = NULL, $algorithm = \MCRYPT_RIJNDAEL_256, $mode = MCRYPT_MODE_ECB, $encLibPath = '', $modeDir = '' )
    {
        $this->_ch = mcrypt_module_open( $algorithm, $encLibPath, $mode, $modeDir );

        $vector  = mcrypt_create_iv ( mcrypt_enc_get_iv_size( $this->_ch ), \MCRYPT_DEV_URANDOM );
        $keySize = mcrypt_enc_get_key_size( $this->_ch );

        $key = substr( hash( 'SHA512', $keyData . $keySize ), 0, $keySize );

        $x = mcrypt_generic_init( $this->_ch, $key, $vector );
    }

    /**
     * Short description of encrypt
     *
     * @access public
     * @author xxxx
     * @param String $str
     * @return String $res
     */
    public function encrypt( $str )
    {
        if( !is_string( $str ) )
        {
            throw new \InvalidArgumentException( 'Attemptig to encrypt data that is not a string' );
            return false;
        }
        $res = mcrypt_generic( $this->_ch, $str );

        mcrypt_generic_deinit( $this->_ch );
        mcrypt_module_close( $this->_ch );

        #var_dump($str,$res);
        return $res;
    }

    /**
     * Short description of decrypt
     *
     * @access public
     * @author xxxx
     * @param String $str
     * @return String $res
     */
    public function decrypt( $str )
    {
        if( !is_string( $str ) )
        {
            throw new \InvalidArgumentException( 'Attemptig to decrypt data that is not a string' );
            return false;
        }

82      $res = mdecrypt_generic( $this->_ch, $str );

84      mcrypt_generic_deinit( $this->_ch );
85      mcrypt_module_close( $this->_ch );

        #var_dump($str,$res);
        return trim( $res);
    }
}
Run Code Online (Sandbox Code Playgroud)

这样调用时:

<?php
$encryption    = new \lib\Encryption( 'somekey' );

echo $encryption->decrypt( $safeInfo );
Run Code Online (Sandbox Code Playgroud)

扼杀收益率:

警告:mdecrypt_generic():90不是第82行的E:\ htdocs\site\application\lib\encryption.cls.php中的有效MCrypt资源

警告:mcrypt_generic_deinit():90不是第84行的E:\ htdocs\site\application\lib\encryption.cls.php中的有效MCrypt资源

警告:mcrypt_module_close():90不是第85行的E:\ htdocs\site\application\lib\encryption.cls.php中的有效MCrypt资源

(这些行显示在加密类中.)

预期的解密字符串(如成功解密).

我会感激任何可能指出为什么警告被提出以及为什么它似乎不会影响结果的人.

PS对加密类的有效性的任何评论都是最受欢迎的.

Zhe*_*Kai 5

它看起来很好

<?php
$encryption = new \lib\Encryption( 'somekey' );
echo $encryption->decrypt(pack("H*", "4a4a564f26618d47536ff35b8a0af3212814a5f0ba635d2cf6f8cd31589042e2"));
Run Code Online (Sandbox Code Playgroud)

_ch失去mcrypt_module_close( $this->_ch );了方法encrypt()

也许你只能更改__construct和保存args,只需_ch在每次加密或解密时创建句柄(如).

我不擅长Mcrypt,所以也许比我的更好,但是"有效的MCrypt资源"问题的原因是真的 mcrypt_module_close