Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法解密cookie_Php_Encryption_Cookies_Php Openssl - Fatal编程技术网

Php 无法解密cookie

Php 无法解密cookie,php,encryption,cookies,php-openssl,Php,Encryption,Cookies,Php Openssl,我需要加密我网站上的cookies。为了简单起见,让我们假设我想要加密的数据是我的会话ID 下面是我将如何生成cookie,并使用PHP openSSL对其进行加密和解密 /** * Encrypt any cookie * @param $content * @param $key_name * @param $iv_name * @return string */ function encrypt_cookie(string $content, string $key_name,

我需要加密我网站上的cookies。为了简单起见,让我们假设我想要加密的数据是我的会话ID

下面是我将如何生成cookie,并使用PHP openSSL对其进行加密和解密

/**
 * Encrypt any cookie
 * @param $content
 * @param $key_name
 * @param $iv_name
 * @return string
 */
function encrypt_cookie(string $content, string $key_name, string $iv_name): string
{
    $method = 'AES-256-CFB';
    $ivLength = openssl_cipher_iv_length($method);
    $needStrong = true;
    $keyLength = 256;
    if (!isset($_SESSION[$key_name]))  {
        $key = openssl_random_pseudo_bytes($keyLength, $needStrong);
        $_SESSION[$key_name] = $key;
    } else {
        $key = $_SESSION[$key_name];
    }
    $iv = openssl_random_pseudo_bytes($ivLength, $needStrong);
    $_SESSION[$iv_name] = $iv;

    return openssl_encrypt($content, $method, $key, $options=OPENSSL_RAW_DATA, $iv);
}

/**
 * Decrypt any cookie
 * @param string $cookie_name
 * @param string $key_name
 * @param $iv_name
 * @return string
 */
function decrypt_cookie(string $cookie_name, string $key_name, $iv_name): string
{
    $data = $_COOKIE[$cookie_name];
    $method = 'AES-256-CFB';
    $key = $_SESSION[$key_name];
    $options = OPENSSL_RAW_DATA;
    $iv = $_SESSION[$iv_name];

    return openssl_decrypt($data, $method, $key, $options, $iv);
}

/**
 * Create the cookie and set its value to an
 * encrypted version of my session ID
 */
function cooking_snickerdoodles(): void
{
    $cookie_name = "sugar_cookie";
    $content = session_id();
    $key_name = 'timeout_cookie_key';
    $iv_name = 'sugar_cookie_iv';

    $hex = encrypt_cookie($content, $key_name, $iv_name);
    setcookie($cookie_name, $hex);
}
加密效果很好。它输出一些东西,如果我使用
bin2hex()
转换它,我就可以读取它。然而,我的解密方法根本不起作用。我检查了我的浏览器开发工具,“sugar_cookie”显示为其中一个cookie

当我尝试回显
decrypt_cookie()
的结果时,我完全没有得到任何结果,即使我将其传递到
bin2hex

下面的代码并不重要,但我正在使用它来确保会话数据与cookie数据匹配:

function has_the_cookie($cookie_name): bool
{
    if (isset($_COOKIE[$cookie_name])) {
        return true;
    } else {
        return false;
    }
}

function cookie_tastes_right(): bool
{
    $crumbs = $_COOKIE['sugar_cookie'];
    $whole_cookie = decrypt_cookie($crumbs, $_SESSION['timeout_cookie_key'], $_SESSION['sugar_cookie_iv']);
    if ($whole_cookie === session_id()) {
        return true;
    } else {
        return false;
    }
}

function confirm_cookie_in_bag(): void
{
    if (!has_the_cookie('sugar_cookie') || !cookie_tastes_right()) {
        end_session();
        redirect_to(url_for('admin/login.php'));
    }
}
编辑-显示不存储二进制文件的更新函数
您正在使用
OPENSSL\u RAW\u DATA
-此调用的输出将不是十六进制,而是二进制。在cookie中存储原始二进制文件是不可能的!您可能更喜欢base64而不是hex,这是默认行为。

有什么最适合的选项建议吗?不要使用任何选项,让它成为base64。或者,如果您需要hex,请在其上调用
bin2hex
。阅读文档。好的,谢谢,但是我仍然没有得到任何要解密的函数输出。如果cookie需要比会话存活更长的时间,那么您将丢失IV并且无法解密它。只是说…实际上,cookie的目的是在浏览器关闭时强制关闭会话,这应该是好主意。我也喜欢使用
sugar\u cookie
:-0
/**
 * Encrypt any cookie
 * @param $content
 * @param $key_name
 * @param $iv_name
 * @return string
 */
function encrypt_cookie(string $content, string $key_name, string $iv_name): string
{
    $method = 'AES-256-CFB';
    $ivLength = openssl_cipher_iv_length($method);
    $needStrong = true;
    $keyLength = 256;
    if (!isset($_SESSION[$key_name]))  {
        $key = openssl_random_pseudo_bytes($keyLength, $needStrong);
        $_SESSION[$key_name] = $key;
    } else {
        $key = $_SESSION[$key_name];
    }
    $iv = openssl_random_pseudo_bytes($ivLength, $needStrong);
    $_SESSION[$iv_name] = $iv;

   return bin2hex(openssl_encrypt($content, $method, $key, $options=OPENSSL_RAW_DATA, $iv));

}

/**
 * Decrypt any cookie
 * @param string $cookie_name
 * @param string $key_name
 * @param $iv_name
 * @return string
 */
function decrypt_cookie(string $cookie_name, string $key_name, $iv_name): string
{
    $data = hex2bin($_COOKIE[$cookie_name]);
    $method = 'AES-256-CFB';
    $key = $_SESSION[$key_name];
    $options = OPENSSL_RAW_DATA;
    $iv = $_SESSION[$iv_name];

    //ECHO and exit for demo purposes only
    echo bin2hex(openssl_decrypt($data, $method, $key, $options, $iv));
    exit;
}