这个类里有一个有点奇怪的问题:
<?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);
}
}
当这样调用时:
<?php
$encryption = new \lib\Encryption( 'somekey' );
echo $encryption->decrypt( $safeInfo );
strangle yields:
警告:mdecrypt_generic():位于E:\ htdocs \ site \ application \ lib \ encryption.cls.php的第82行的90不是有效的MCrypt资源
警告:mcrypt_generic_deinit():90不是E:\ htdocs \ site \ application \ lib \ encryption.cls.php的第84行中有效的MCrypt资源
警告:mcrypt_module_close():90不是E:\ htdocs \ site \ application \ lib \ encryption.cls.php的第85行中有效的MCrypt资源
(这些行出现在加密类中。)
和
预期解密的字符串(即成功解密的字符串)。
如果有人能指出为什么会出现这些警告,以及为什么似乎不影响结果,我将非常感激。
PS,对加密类的效果的任何评论都受欢迎。
resource(2) of type (mcrypt)
。 - Gohn67