开发者社区> 问答> 正文

用php仿照js写的一个加密方法,出现问题

原本的js是一段加密的方法

function checkform()
{
var dcode;
var dcode1;
var dcode2;
dcode=document.form1.pwd.value;
dcode1="";
dcode2=1275903582
dcode2=""+dcode2*137;
var tmpstr;
var dcodelen;
dcodelen=dcode.length;
for (i=1;i<=dcodelen;i++){
tmpstr = dcode.substring(i-1,i);
dcode1=dcode1 + String.fromCharCode(tmpstr.charCodeAt(0)-i-dcode2.substring(i-1,i));
}
document.form1.pwd.value=dcode1;
}

这是我仿照写的php代码 但始终有错

//加密
function encryption($dcode){
    $dcode1=''; 
    $dcode2=1275903582;
    $dcode2 = "".$dcode2 * 137; 
    $tmpstr='';  
    for ($i=1;$i<=count($dcode);$i++)
    { 
            $tmpstr = substr($dcode,$i-1,1); 
            $tmpstr = $this->charCodeAt($tmpstr, 0); 
            $num = $this->fromCharCode($tmpstr-$i-substr($dcode2,$i-1,1)); 
            $dcode1=$dcode1 + $num;
    }  
            $encryDcode=$dcode1; 
            print_r($encryDcode); 
            die(); 
            return $encryDcode; 
    } 
function fromCharCode($codes) {
if (is_scalar($codes)) 
$codes= func_get_args();
$str= '';
foreach ($codes as $code)
$str.= chr($code);
return $str;
}
function charCodeAt($str, $index) 
{ 
$char = mb_substr($str, $index, 1, 'UTF-8');

if (mb_check_encoding($char, 'UTF-8')) 
{ 
$ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8'); 
return hexdec(bin2hex($ret)); } else { return null; 
}
}

展开
收起
小旋风柴进 2016-03-06 10:07:52 2610 0
1 条回答
写回答
取消 提交回答
  •   function encryption($dcode, $dcode2) {
            $dcode1 = "";
            $dcode2 = "" . $dcode2 * 137;
            $tmpstr="";
            $dcodelen =strlen($dcode);
            for ($i = 0; $i < $dcodelen; $i++) {
                $tmpstr = substr($dcode,$i , 1);
                $cd=charCodeAt($tmpstr,0);
    
                $dcode1 = $dcode1 . fromCharCode($cd -( $i+1)- substr($dcode2,$i , 1));
            }
            return $dcode1;
        }
        function fromCharCode($codes) {
          if (is_scalar($codes)) $codes= func_get_args();
          $str= '';
          foreach ($codes as $code) $str.= chr($code);
          return $str;
        }
        function charCodeAt($str, $index){
          $char = mb_substr($str, $index, 1, 'UTF-8'); 
          if (mb_check_encoding($char, 'UTF-8'))    {
            $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
            return hexdec(bin2hex($ret));
          }
          return null;
       }
      echo encryption("123456", 1275903582);
    2019-07-17 18:54:06
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
PHP安全开发:从白帽角度做安全 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载