<?
function cookie_encrypt($str, $id = 0)
{
  global $signature;
  
	if (function_exists('mcrypt_module_open'))
	{
		$td = mcrypt_module_open ('rijndael-256', '', 'ofb', '');
		if (!$iv = @file_get_contents(INI.'cookie_shif.dat'))
		{
			$iv = base64_encode( mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM));
			file_put_contents(INI.'cookie_shif.dat', $iv);
			chmod(INI.'cookie_shif.dat', 0777);
		}
		$ks = @mcrypt_enc_get_key_size ($td);
		
		/* Создать ключ */
		$key = substr (md5 ($id.@$_SERVER['HTTP_USER_AGENT'].$signature), 0, $ks);
		@mcrypt_generic_init ($td, $key, base64_decode($iv));
		$str = @mcrypt_generic ($td, $str);
		@mcrypt_generic_deinit ($td);
		@mcrypt_module_close ($td);
	}

	$str = base64_encode($str);
	return $str;
}

function cookie_decrypt($str, $id = 0)
{
  global $signature;
  
	$str = base64_decode($str);

	if (function_exists('mcrypt_module_open'))
	{
		$td = mcrypt_module_open ('rijndael-256', '', 'ofb', '');

		if (!$iv = @file_get_contents(INI.'cookie_shif.dat'))
		{
			$iv = base64_encode( mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM));
			file_put_contents(INI.'cookie_shif.dat', $iv);
			chmod(INI.'cookie_shif.dat', 0777);
		}
		$ks = @mcrypt_enc_get_key_size ($td);
		
		/* Создать ключ */
		$key = substr (md5 ($id.@$_SERVER['HTTP_USER_AGENT'].$signature), 0, $ks);
		@mcrypt_generic_init ($td, $key, base64_decode($iv));
		$str = @mdecrypt_generic ($td, $str);
		@mcrypt_generic_deinit ($td);
		@mcrypt_module_close ($td);
	}
	return $str;
}
?>