【问题标题】:openssl_decrypt tag valueopenssl_decrypt 标记值
【发布时间】:2019-01-29 11:04:28
【问题描述】:

我在我的网站中使用 openssl_encrypt/decrypt 方法,但我在使用 $tag 选项时遇到了一些问题

openssl_encrypt ( $data, $method, $key, $options, $iv, $tag )
openssl_decrypt ( $data, $method, $key, $options, $iv, $tag )

来自http://php.net/manual/en/function.openssl-encrypt.php,标签的定义是:使用AEAD密码模式(GCM或CCM)时通过引用传递的认证标签。但是没看懂。

我在我的代码中尝试过

$data = "text to be encrypted";
$cipher = "aes-128-gcm";
$key = "0123456789abcdefghijklmnob123456";
$option = 0;
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);

if (in_array($cipher, openssl_get_cipher_methods())){       
    $encryptedData = openssl_encrypt($data,$cipher,$key,$option,$iv,$tag);
    echo $encryptedData;

    $decryptedData = openssl_decrypt($encryptedData,$cipher,$key,$option,$iv,$tag);
    echo $decryptedData;
}

我得到了这个结果:

encrypted text: Vlx/yKkPhg0DpD0YKvnFKRiCh/I=
decrypted text: text to be encrypted

这是正确的。但如果我直接以这种方式解密加密文本:

$data = "text to be encrypted";
$cipher = "aes-128-gcm";
$key = "0123456789abcdefghijklmnob123456";
$option = 0;
$ivlen = openssl_cipher_iv_length($cipher);
$iv = openssl_random_pseudo_bytes($ivlen);

if (in_array($cipher, openssl_get_cipher_methods())){
    $encryptedData = "Vlx/yKkPhg0DpD0YKvnFKRiCh/I=";

    $decryptedData = openssl_decrypt($encryptedData,$cipher,$key,$option,$iv,$tag);
    echo $decryptedData;
}

我明白了:

Notice: Undefined variable: tag

如果有人可以向我解释为什么会发生这种情况以及 $tags 的值应该是什么。谢谢

【问题讨论】:

    标签: php encryption openssl


    【解决方案1】:

    PHP 抱怨的标签是使用 GCM 操作模式时 AES 的一个重要方面。在这种模式下,不仅会应用 AES 分组密码,还会计算身份验证标签。它是一个字节数组,代表一个MAC (Message Authentication Code),可用于验证数据的完整性并进行解密。需要提供相同的标签来进行验证。有关详细信息,请参阅Wikipedia page about Galois/Counter Mode

    因此,为了成功解密该密文,您需要捕获openssl_encrypt() 调用产生的$tag 变量并将其输入openssl_decrypt() 调用。您没有这样做,因此抱怨缺少标签。请注意,标签(通常)包含不可读的字符,因此以 base64 编码格式存储更方便。

    除了$tag 变量之外,您还应该为openssl_decrypt() 方法提供与openssl_encrypt() 调用中使用的相同的$iv 变量值。同样,base64 编码使这更容易。

    下面的快速测试演示了这一切,我首先修改了您的脚本以打印更多内容,然后使用提供的脚本进行解密:

    $ php test1.php 
    iv base64-ed: vBKbi8c6vCyvWonV
    plaintext: text to be encrypted
    ciphertext base64-ed: z28spOd3UEDmj+3a8n/WK11ls7w=
    GCM tag base64-ed: OIAggQCGUbPgmPN6lFjQ8g==
    $ php test2.php 
    decrypted ciphertext: text to be encrypted
    

    test2.php 的代码如下:

    $cipher = "aes-128-gcm";
    $key = "0123456789abcdefghijklmnob123456";
    $option = 0;
    $iv = base64_decode("vBKbi8c6vCyvWonV");
    
    if (in_array($cipher, openssl_get_cipher_methods())){       
    
        $encryptedData = "z28spOd3UEDmj+3a8n/WK11ls7w=";
        $tag = base64_decode("OIAggQCGUbPgmPN6lFjQ8g==");
    
        $decryptedData = openssl_decrypt($encryptedData,$cipher,$key,$option,$iv,$tag);
        echo("decrypted ciphertext: ".$decryptedData."\n");
    }
    

    【讨论】:

    • 谢谢,但如果我理解正确的话,每次我需要使用 aes-128-gcm 解密时,我都必须先捕获 $tag?
    • 使用$tag 为您提供身份验证。如果您不需要它,则可以在没有它的情况下解密,使用“普通”aes-128-ctr 模式,通过稍微调整$iv。参见例如AES GCM decryption bypassing authentication in JAVA。不过,您将始终需要捕获 $iv
    • 是的,我昨天想通了,我使用了“aes-256-cbc”,它在没有 $tag 的情况下工作得很好。非常感谢
    • 请注意,标签是 MAC,而不是 HMAC(如答案中所述)。
    • @MartinSuecia 感谢您指出这一点,我更正了。
    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2023-03-12
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多