[PHP] 간단한 문자열 암복호화

문자열 암복호화를 위한 간단한 PHP Class 입니다.

<?php
// 문자열 암복호화
class DECENCRYPT
{
    var $salt;
    var $lenght;

    function __construct($salt)
    {
        $this->salt = md5($salt);
        $this->length = strlen($this->salt);
    }

    function encrypt($str)
    {
        $length = strlen($str);
        $result = '';

        for($i=0; $i<$length; $i++) {
            $char    = substr($str, $i, 1);
            $keychar = substr($this->salt, ($i % $this->length) - 1, 1);
            $char    = chr(ord($char) + ord($keychar));
            $result .= $char;
        }

        return base64_encode($result);
    }

    function decrypt($str) {
        $result = '';
        $str    = base64_decode($str);
        $length = strlen($str);

        for($i=0; $i<$length; $i++) {
            $char    = substr($str, $i, 1);
            $keychar = substr($this->salt, ($i % $this->length) - 1, 1);
            $char    = chr(ord($char) - ord($keychar));
            $result .= $char;
        }

        return $result;
    }
}

// 사용
$enc = new DECENCRYPT('1234567890');

$str = $enc->encrypt('ABC');

echo $str.'<br>';
echo $enc->decrypt($str);
?>

1234567890 은 암호화 키로 사용되기 때문에 노출돼서는 안됩니다.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.