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;
}
?>