openssl_decrypt:设置AEAD密码解密的标签失败

问题描述

我希望大家都做得很好。

我正在使用 openssl 扩展名进行加密/解密。当我像下面这样编码时,即使我不使用 bin2hex(),它也可以正常工作。请注意, $ tag 对每个用户来说都是静态值,它不会像 $ iv $ key 那样改变。

$plaintext = 'password';
$cipher = "aes-128-gcm";
$tag = 'tqjFo+ChP8UB2gHkG9F5Vw==';
echo '<pre>';       

    $ivlen = openssl_cipher_iv_length($cipher);
    $key = openssl_random_pseudo_bytes($ivlen);
    $iv = openssl_random_pseudo_bytes($ivlen);
    
    $iv = bin2hex($iv);
    $key = bin2hex($key);

    $encrypted = bin2hex(openssl_encrypt($plaintext,$cipher,$key,$options=0,$iv,$tag));
    $decrypted = openssl_decrypt(hex2bin(($encrypted)),$tag);
    echo 'key='.$key. "\n";
    echo 'iv='.$iv. "\n";
    echo 'plaintext='.$plaintext. "\n";
    echo 'encrypted to: '.$encrypted. "\n";
    echo 'decrypted to: '.$decrypted. "\n\n";


echo '</pre>';

问题是当我必须存储 $ iv $ key 和 在数据库MysqL)中 $ encrypted ,并将其用于以后的解密。

首先让我向您展示我的操作方式

数据库(我不确定字段类型还是字符串,二进制等)

enter image description here

函数加密密码并存储$ iv,$ key和$ encryptedData 进入数据库

public function createOpenSSL(Request $req){
    $plaintext = $req->password;
    $cipher = "aes-128-gcm";
    $tag = "tqjFo+ChP8UB2gHkG9F5Vw==";
    $ivlen = openssl_cipher_iv_length($cipher);
    $key = openssl_random_pseudo_bytes($ivlen);
    $iv = openssl_random_pseudo_bytes($ivlen);
    $iv = bin2hex($iv);
    $key = bin2hex($key);
    $encrypted = openssl_encrypt($plaintext,$tag);
    $user = new openssl();
    $user->password = $encrypted;
    $user->ukey = $key;
    $user->iv =$iv;
    $user->save();

}

功能将找到匹配“ id”的用户并解密 密码(但不幸的是它不起作用

public function showOpenSSL(Request $req){
    $user = openssl::findorFail($req->id);
    $cipher = "aes-128-gcm";
    $tag = "tqjFo+ChP8UB2gHkG9F5Vw==";
    $key = hex2bin($user->ukey);
    $iv = hex2bin($user->iv);
    $encrypted = $req->password;
    $decrypted = openssl_decrypt($encrypted,$tag);  
}

执行上述功能时,发生“ openssl_decrypt():设置AEAD密码解密失败的标签错误。我已经尽了最后两天的努力,但是什么也没找到。请告诉我我在哪里做错了。另请指导我有关最适合存储 $ iv $ key $ encrypted 数据库字段类型。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...