PHP mcrypt 加密解密函数

加密

function mcrypt_encode ($data, $key) {
    $init_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
    $init_vect = mcrypt_create_iv($init_size, MCRYPT_RAND);
    $data = $init_vect . mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $init_vect);
    return $data;
}

解密

function mcrypt_decode ($data, $key) {
    $init_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC);
    if ($init_size > strlen($data)) {
        return false;
    }
    $init_vect = substr($data, 0, $init_size);
    $data = substr($data, $init_size);
    return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $init_vect), "\0");
}


你可能感兴趣的:(PHP,mcrypt)