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
전깃불이 나간 어두운 방안에서 초가 있으면서도 초를 아끼느라 켜지 않는다면 어떻게 될까. 마찬가지로 한두 마디의 상냥한 말이면 상대방의 마음을 밝게 해 주고 유쾌한 분위기를 만들 수 있는데 그러지 않는다면 그것은 마치 초를 아끼기 위해 어둠 속에 있는 것과 같다. (T. 제퍼슨)