项目中使用了thinkphp3.2的框架,用到里面的Des加密类,但是调用时竟然出错了!
简单了封装成了全局函数:
/**
* Des加密
* @param $str
* @param $key
* @return string
*/
function des_encrypt($str, $key)
{
$crypt = new \Think\Crypt();
$crypt->init('Des'); // 选择Des加密
$res = $crypt->encrypt($str, $key);
return $res;
}
调用语句为: $promotion = des_encrypt('some data', 'my key');
然后执行时就报错了!
:)
Bit shift by negative number
错误位置
FILE: /xxx/simplewind/Core/Library/Think/Crypt/Driver/Des.class.php LINE: 195
TRACE
#0 /xxx/simplewind/Core/Library/Think/Crypt/Driver/Des.class.php(75): Think\Crypt\Driver\Des::_createKeys('my key')
#1 /xxx/simplewind/Core/Library/Think/Crypt/Driver/Des.class.php(34): Think\Crypt\Driver\Des::_des('my key', '0000000000some ...', 1)
#2 /xxx/simplewind/Core/Library/Think/Crypt.class.php(37): Think\Crypt\Driver\Des::encrypt('0000000000some ...', 'my key', '0000000000')
#3 /xxx/application/Common/Common/function.php(2438): Think\Crypt::encrypt('some data', 'my key')
#4 /xxx/application/Recharge/Controller/OtherRechargeController.class.php(119): des_encrypt('some data', 'my key')
看这错误描述,似乎是说左移或右移时使用了负数,看了下代码确实是这样的!
Des.class.php中 private static function _createKeys ($key)
但这是tp框架的代码,难道我要改?改了还能正确?
-------------------------------------------------------------------------------------------
于是,我又写了个最基本的测试代码,如下:
果然,错误信息是一样的!
那怎么办,难道thinkphp3.2中的Des加密一直是错误的,感觉不可能呀!
希望有遇到过的朋友来交流下,到底我错在哪了?