使用前确保开启了 mcrypt模块

<?php
function idtag_des_decode($key,$encrypted)
{
    $encrypted = base64_decode($encrypted);

    $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    $ks = mcrypt_enc_get_key_size($td);
    mcrypt_generic_init($td, $key, $key);       //初始处理

    $decrypted = mdecrypt_generic($td, $encrypted);       //解密

    mcrypt_generic_deinit($td);       //结束
    mcrypt_module_close($td);

    $y=pkcs5_unpad($decrypted);
    return $y;
}

function idtag_des_encode($key,$text)
{

    $y=pkcs5_pad($text);

    $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
   /// $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    $ks = mcrypt_enc_get_key_size($td);
    mcrypt_generic_init($td, $key, $key);       //初始处理
    $encrypted = mcrypt_generic($td, $y);       //解密
    mcrypt_generic_deinit($td);       //结束
    mcrypt_module_close($td);

    return base64_encode($encrypted);
}

function pkcs5_pad($text,$block=8)
{
        $pad = $block - (strlen($text) % $block);
        return $text . str_repeat(chr($pad), $pad);
}


function pkcs5_unpad($text)
{
   $pad = ord($text{strlen($text)-1});
   if ($pad > strlen($text)) return $text;
   if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text;
   return substr($text, 0, -1 * $pad);
}

$key = '12345678';
$str = 'str';
$encstr = idtag_des_encode($key,$str);


$ys=idtag_des_decode($key,$encstr);
echo $ys;
?>

Posted on 2009-07-10 11:18  古代  阅读(573)  评论(0编辑  收藏  举报