首页 > 后端开发 > php教程 > 求兼容PHP5.5和5.6的des加解密函数或类

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

WBOY
发布: 2016-06-20 12:28:57
原创
1340 人浏览过

<?phpclass DES{ var $key; var $iv; //偏移量  function DES( $key, $iv=0 ) { //key长度8例如:1234abcd  $this->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 ( $hexData ); $i += 2) {   $binData .= chr ( hexdec ( substr ( $hexData, $i, 2 ) ) );  }  return $binData; }  function pkcs5Pad($text, $blocksize) {  $pad = $blocksize - (strlen ( $text ) % $blocksize);  return $text . str_repeat ( chr ( $pad ), $pad ); }  function pkcs5Unpad($text) {  $pad = ord ( $text {strlen ( $text ) - 1} );  if ($pad > 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 
显然是不能用的

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板