在类修复中需要'openssl_encrypt'

时间:2019-03-02 17:51:13

标签: php class encryption

这是一个具有解密和加密的简单类,下面的代码不起作用,如何解决?

class en{
    const cipher  = "aes-128-gcm";
    const key = "sitekey";

    function iv(){
        return openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::cipher));
    }
    function encrypt($text,$iv){
        return openssl_encrypt($text, self::cipher, self::key, $options=0, $iv, $tag);
    }
    function decrypt($text,$iv){
        return openssl_decrypt($text, self::cipher, self::key, $options=0, $iv, $tag);
    }
}
$en = new en();

$iv = $en->iv();
$encrypted = $en->encrypt("message to be encrypted",$iv);
$decrypted = $en->decrypt($encrypted,$iv);
echo "iv: ".$iv."<br />";
echo "Encrypted: ".$encrypted."<br />";
echo "Decrypted: ".$decrypted."<br />";

1 个答案:

答案 0 :(得分:0)

在使用openssl_encrypt和openssl_decrypt函数时, 您有$ tag变量,您将其作为参数传递。 但是没有在任何地方声明$ tag。

如果您删除$ tag的2个引用,并将加密方法更改为

aes-128-cbc

代码有效。