问题描述
我希望大家都做得很好。
我正在使用 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 ,并将其用于以后的解密。
首先让我向您展示我的操作方式
数据库(我不确定字段类型还是字符串,二进制等)
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();
}
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 (将#修改为@)