易经加密算法之八卦加密算法

下面的易经八卦的加密算法核心部分:


结果如下:

<?php
header( 'Content-Type:text/html;charset=utf-8');

//asc转为二进制
function asc2bin($temp)
{
    $len = strlen($temp);
    $data = "";
    for ($i = 0; $i < $len; $i++) {
        $data .= sprintf("%08b", ord(substr($temp, $i, 1)));
    }
    return $data;
}

//二进制转asc
function bin2asc($temp)
{
    $len = strlen($temp);
    $data = "";
    for ($i = 0; $i < ($len / 8); $i++) {
        $data .= chr(intval(substr($temp, $i * 8, 8), 2));
    }
    return $data;
}

$zhb = array("111" => "乾", "000" => "坤", "010" => "坎", "101" => "离", "001" => "震", "100" => "艮", "110" => "巽", "011" => "兑", "1" => "阳", "0" => "阴");


$szb = array("2" => "二", "3" => "三", "4" => "四", "5" => "五", "6" => "六", "7" => "七", "8" => "八", "9" => "九", "10" => "十");

function z_bg($str)
{
    global $zhb, $szb;
    $bin = asc2bin($str);
    $result = strtr($bin, $zhb);
    $rs = "";
    $rn = substr($result, 0, 2);
    $nownums = 1;
    $jh = 0;
    for ($i = 2; $i < strlen($result) + 2; $i = $i + 2) {
        $nowstr = substr($result, $i, 2);

        if ($nowstr == $rn && $nownums < 10) {
            $nownums++;
        } else {
            if ($nownums > 1) {
                $rs .= $rn . $nownums;
            } else {
                $rs .= $rn;
            }
            $nownums = 1;
        }
        $rn = $nowstr;
    }
    $rs = strtr($rs, $szb);
    $result = "";
    for ($i = 0; $i < strlen($rs); $i = $i + 8) {
        $result .= substr($rs, $i, 8);
        if ($i + 8 < strlen($rs) && !$jh) {
            $result .= ",";
            $jh = 1;
        } else {
            $result .= "。\n";
            $jh = 0;
        }

    }
    return $result;
}

function de_bg($str)
{
    global $zhb, $szb;
    $dzhb = array_flip($zhb);
    $dszb = array_flip($szb);
    $str = strtr($str, array("," => "", "。" => "", "\n" => ""));

    $rs = "";
    $rn = substr($str, 0, 2);
    for ($i = 2; $i < strlen($str) + 2; $i = $i + 2) {
        $nowstr = substr($str, $i, 2);
        $rns = $nowstr;
        $nowstr = strtr($nowstr, $dszb);
        if (strlen($nowstr) == 1) {
            $rs .= str_repeat($rn, $nowstr);
            $rns = "";
        } else {
            $rs .= $rn;
        }
        $rn = $rns;
    }
    $rs = bin2asc(strtr($rs, $dzhb));
    return $rs;
}

//开始加密
$encode = z_bg('这个是要加密的内容,一些秘密的东西就防止这里了,哈哈哈哈哈');
echo $encode;
echo '<hr>';
//开始解密
echo de_bg($encode);


评论/留言