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
만일 당신이 배를 만들고 싶다면 사람들을 불러모아 목재를 가져오게 하고 일을 지시하고 일감을 나눠 주는 등의 일을 하지 말아라. 대신 그들에게 저 넓고 끝없는 바다에 대한 동경심을 키워 줘라. (생텍쥐페리)