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;

}

관련자료

댓글 0
등록된 댓글이 없습니다.
Today's proverb
보람 있게 보낸 하루가 편안한 잠을 가져다주듯이 값지게 쓰여진 인생은 편안한 죽음을 가져다준다. (레오나르도 다빈치)