加密和解密消息

时间:2014-07-07 05:28:48

标签: php

这是新PHP mcrypt_generic

的良好实现
private function Encrypt_Decrypt($key, $message, $encrypt = true)
{
    if (!isset($key) || !isset($message))
    {
        throw new Exception("Invalid Parameters");
    }

    $iv = md5(md5($key));
    $output = "";
    $td = mcrypt_module_open("blowfish", "", "cbc", "");

    mcrypt_generic_init($td, $key, $iv);
    switch ($encrypt)
    {
        case true:{
            $output = mcrypt_generic($td, $message);
            break;
        }

        case false:{
            $output = mdecrypt_generic($td, $message);
            break;
        }
    }
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);

    return $output;
}

0 个答案:

没有答案
相关问题