PHP & Others

PHP 문자열 encode decode

페이지 정보

본문

function encode($value) {

    if (!$value) {

        return false;

    }


    $key = sha1('yourownkey');

    $strLen = strlen($value);

    $keyLen = strlen($key);

    $j = 0;

    $crypttext = '';


    for ($i = 0; $i < $strLen; $i++) {

        $ordStr = ord(substr($value, $i, 1));

        if ($j == $keyLen) {

            $j = 0;

        }

        $ordKey = ord(substr($key, $j, 1));

        $j++;

        $crypttext .= strrev(base_convert(dechex($ordStr + $ordKey), 16, 36));

    }


    return $crypttext;

}



function decode($value) {

    if (!$value) {

        return false;

    }


    $key = sha1('yourownkey');

    $strLen = strlen($value);

    $keyLen = strlen($key);

    $j = 0;

    $decrypttext = '';


    for ($i = 0; $i < $strLen; $i += 2) {

        $ordStr = hexdec(base_convert(strrev(substr($value, $i, 2)), 36, 16));

        if ($j == $keyLen) {

            $j = 0;

        }

        $ordKey = ord(substr($key, $j, 1));

        $j++;

        $decrypttext .= chr($ordStr - $ordKey);

    }


    return $decrypttext;

}

관련자료

등록된 댓글이 없습니다.
Today's proverb
살아 있는 동안 더 많이 감탄하세요. 가슴을 열고 보면 어디 감탄할 거리가 한두 가지입니까. 감탄할 거리가 있을때가 참을 것이 아니라 즉각 감탄해야 합니다. 가슴 두근거리고 놀라고 환호할때 우리의 행복은 곱으로 느껴집니다. (정채봉)