移位算法实现

2019-03-13  本文已影响0人  firaga

目标: 实现字符串移位加密.

缺点: 密钥传输

const DIC =':abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

public static function sDecode($s)

{

$sKey      ='vjJ3LKNENnrvvRc9HPl5NxRQR5tRrHW';

$a          = str_split($s);

$aKey      = str_split($sKey);

$iKeyLength = count($aKey);

$aDic      = str_split(self::DIC);

$iDicLength = count($aDic);

$kp        =0;

$o          ='';

foreach ($a as $c) {

$step = ord($aKey[$kp %$iKeyLength]);

$dp  =self::ctn($c) -$step;

while ($dp <0) {

$dp +=$iDicLength;

}

//            echo $dp . chr(9) . $aDic[$dp] . chr(10);

            $o    .=$aDic[$dp];

$line = [$c,self::ctn($c),$step,$dp,$aDic[$dp]];

//            echo implode(chr(9), $line) . chr(10);

            $kp++;

}

return $o;

}

public static function sEncode($s)

{

$sKey      ='vjJ3LKNENnrvvRc9HPl5NxRQR5tRrHW';

$a          = str_split($s);

$aKey      = str_split($sKey);

$iKeyLength = count($aKey);

$aDic      = str_split(self::DIC);

$iDicLength = count($aDic);

$kp        =0;

$o          ='';

foreach ($a as $c) {

$n    =self::ctn($c);

$step = ord($aKey[$kp %$iKeyLength]);

//            echo $n.chr(9).$step.chr(9).$kp%$iKeyLength.chr(10);

            $dp = ($n +$step) %$iDicLength;

$o  .=$aDic[$dp];

$kp++;

}

return $o;

}

private static function ctn($c)

{

$aDic = str_split(self::DIC);

$aMap = array_flip($aDic);

return isset($aMap[$c]) ?$aMap[$c] :'|';

}

上一篇下一篇

猜你喜欢

热点阅读