您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

求兼容PHP5.5和5.6的des加解密函数或类

2025/11/15 6:13:08发布31次查看
key = $key; if( $iv == 0 ) { $this->iv = $key; } else { $this->iv = $iv; //mcrypt_create_iv ( mcrypt_get_block_size (mcrypt_des, mcrypt_mode_cbc), mcrypt_dev_random ); } } function encrypt($str) { //加密,返回大写十六进制字符串 $size = mcrypt_get_block_size ( mcrypt_des, mcrypt_mode_cbc ); $str = $this->pkcs5pad ( $str, $size ); return strtoupper( bin2hex( mcrypt_cbc(mcrypt_des, $this->key, $str, mcrypt_encrypt, $this->iv ) ) ); } function decrypt($str) { //解密 $strbin = $this->hex2bin( strtolower( $str ) ); $str = mcrypt_cbc( mcrypt_des, $this->key, $strbin, mcrypt_decrypt, $this->iv ); $str = $this->pkcs5unpad( $str ); return $str; } function hex2bin($hexdata) { $bindata = ; for($i = 0; $i strlen ( $text )) return false; if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) return false; return substr ( $text, 0, - 1 * $pad ); }}$my_encrypt = new des('qwertyuiop');echo $my_encrypt->encrypt('1234567');
网上找到各种的都不对,上面贴的这个好像因php版本的问题测试也有各种错误,不知怎么改。
回复讨论(解决方案) 试试这个: http://www.cnblogs.com/wangchuang/archive/2012/04/23/2466002.html
楼上说的这个的测试结果:
notice: uninitialized string offset: 6 in c:\web\project1\web\dec.php on line 148
notice: uninitialized string offset: 7 in c:\web\project1\web\dec.php on line 148
notice: uninitialized string offset: 6 in c:\web\project1\web\dec.php on line 148
notice: uninitialized string offset: 7 in c:\web\project1\web\dec.php on line 148
mdn2iafg6imabkvbi2l+ka==
hello levenphp notice: uninitialized string offset: 6 in c:\web\project1\web\dec.php on line 148 php notice: uninitialized string offset: 7 in c:\web\project1\web\dec.php on line 148 php notice: uninitialized string offset: 6 in c:\web\project1\web\dec.php on line 148 php notice: uninitialized string offset: 7 in c:\web\project1\web\dec.php on line 148
function encrypt($str, $key) { $block = mcrypt_get_block_size('des', 'ecb'); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); return mcrypt_encrypt(mcrypt_des, $key, $str, mcrypt_mode_ecb);}function decrypt($str, $key) { $str = mcrypt_decrypt(mcrypt_des, $key, $str, mcrypt_mode_ecb); $block = mcrypt_get_block_size('des', 'ecb'); $pad = ord($str[($len = strlen($str)) - 1]); return substr($str, 0, strlen($str) - $pad); }
php5.6.3 通过
据说是与 java 兼容的
你的有 deprecated: function mcrypt_cbc() is deprecated 
显然是不能用的
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product