• 로그인 함 해보끄나?

  • Sarangnamu.net June 17, 2003
    Home Login Profile Study Open Project Q&A Google Code
    des3
    Last Modify : 13 January, 2011(01:25)
    php
    <?php 
     
    //
    // 기본 키 값
    //
    $key		= "YourTripleDesKeyValues00";		// 24 bit Key
    $iv			= "initalvt";						// 8 bit IV
    $input		= "Text to encrypt";				// text to encrypt
    $bit_check	= 8;								// bit amount for diff algor.
     
     
    //
    // 테스트
    //
    $str = encrypt($input,$key,$iv,$bit_check);
     
    echo "Start: $input<br />";
    echo "Encrypted: $str <br />";
    echo "Decrypted: ". decrypt($str,$key,$iv,$bit_check);
     
     
    //
    // 관련 함수
    //
     
    function hex2text($str)
    {
        $str = explode('%', $str);
        array_shift($str);
        $nmlstr = '';
        foreach($str as $hexstr) {
          $nmlstr .= chr(base_convert($hexstr, 16, 10));
        }    
        return $nmlstr;
    }
     
    function StringToHex($szValue)
    {
    	$szTemp = "";
     
    	for($i=0; $i<strlen($szValue); ++$i)
    	{
    		$szTemp .= bin2hex($szValue[$i]);
    	}
     
    	return $szTemp;
    }
     
    function hex2bin($str)
    {
        $bin = "";
        $i = 0;
     
    	do
    	{
            $bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
            $i += 2;
        }
    	while ($i < strlen($str));
     
        return $bin;
    }
     
    function HexToString($szValue)
    {
    	$szTemp = "";
     
    	for($i=0; $i<strlen($szValue); ++$i)
    	{
    		$szTemp .= hex2bin($szValue[$i++].$szValue[$i]);
    	}
     
    	return $szTemp;
    }
     
    function encrypt($text,$key,$iv,$bit_check)
    {
    	$text_num = str_split($text, $bit_check);
    	$text_num = $bit_check-strlen($text_num[count($text_num)-1]);
     
    	for ($i=0;$i<$text_num; $i++)
    		$text = $text . chr($text_num);
     
    	$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','ecb','');
    	mcrypt_generic_init($cipher, $key, $iv);
    	$decrypted = mcrypt_generic($cipher,$text);
    	mcrypt_generic_deinit($cipher);
    	mcrypt_module_close($cipher);
     
    	return strtoupper(StringToHex($decrypted));
    }
     
    function decrypt($encrypted_text,$key,$iv,$bit_check)
    {
    	$encrypted_text = HexToString(strtolower($encrypted_text));
     
    	$cipher = mcrypt_module_open(MCRYPT_TRIPLEDES,'','ecb','');
    	mcrypt_generic_init($cipher, $key, $iv);
    	$decrypted = mdecrypt_generic($cipher,$encrypted_text);
    	mcrypt_generic_deinit($cipher);
    	mcrypt_module_close($cipher);
    	$last_char=substr($decrypted,-1);
     
    	for($i=0; $i<$bit_check-1 ; $i++)
    	{
    		if(chr($i) == $last_char)
    		{
    			$decrypted = substr($decrypted, 0, strlen($decrypted) - $i);
    			break;
    		}
    	}
     
    	return $decrypted;
    }
    ?>

    Comment


    입력하3 1498429603



    Locations of visitors to this page