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
성공하느냐 실패하느냐는 다른 사람이 아닌 바로 내가 하는 일이다. 내가 바로 힘이다. 나는 내 앞의 장애물을 치울 수도 있고, 미로 속에서 길을 잃을 수도 있다. 내 선택, 내 책임 이다. 이기거나 지는 것은 오직 나만이 가진 내 운명의 열쇠에 달려있다. (알레인 맥스웰)