问题描述
我正在尝试加密在 textarea 标签或文本输入中输入的字符串,并且输入的字符串长度超过 256 个字符。
当文本框中的字符串长度达到 257 时,我无法再解密它!
我使用 GitHub 上的示例代码编写了以下代码,用于 aes-gcm 加密,它适用于短字符串,但不适用于长度超过 256 的字符串。
我搜索了 aes-gcm-256,我发现没有(至少是简单的方法)可能的方法将它从 256 增加到 512 或更多,这也没有意义我们为什么这样做,因为它根本没有必要。
我在解密长度超过 256 的字符串时收到错误消息:
身份验证失败!因为密钥超过 256。
这是因为字符串长度超过 256!
现在我的问题是,如何加密长度超过 256 的字符串?我应该用什么方法在 PHP 中使用 aes-gcm 在数据库中加密和存储长字符串?
在下面的附加代码中,我尝试加密示例字符串,然后将其发送到数据库。
然后我尝试通过调用插入的任何数据(最近或旧数据)的 ID 从数据库中获取数据,以便我可以获取有关该列的信息。
This is link to AES-GCM library for PHP.
更新:
这是我在解密时收到的确切错误:
致命错误:未捕获的 Sop\GCM\Exception\AuthenticationException:身份验证失败。在 C:\xampp\htdocs\vendor\sop\gcm\lib\GCM\Cipher\AES\AESCipher.PHP:124 堆栈跟踪:#0 C:\xampp\htdocs\vendor\sop\gcm\lib\GCM\AESGCM .PHP(54): Sop\GCM\Cipher\AES\AESCipher->nativeDecrypt('\x18\x9CTV\xDBI!#tNt...','&F=VmZ#gP2udV4H...','^ \xBB\xDF\x81\x91\xEF\xFC\xBE\xAD\xDC\x96\x89\xA6\x01\xDE...') #1 C:\xampp\htdocs\test.PHP(49): Sop \GCM\AESGCM::decrypt('\x18\x9CTV\xDBI!#tNt...','^\xBB\xDF\x81\x91\xEF \xFC\xBE\xAD\xDC\x96\x89\xA6\x01\xDE...') #2 C:\xampp\htdocs\test.PHP(55): getinformation->getData() #3 {main}在第 124 行的 C:\xampp\htdocs\vendor\sop\gcm\lib\GCM\Cipher\AES\AESCipher.PHP 中抛出
//Encrypt and Insert data
$cipher = $_POST['textBox_value'];
if($insert = $this->conn()->prepare("INSERT INTO security_check (cipher,aad,sec_key,iv,auth_tag) VALUES (:cipher,:aad,:secKey,:iv,:aTAg)")){
$aad = 'thisisadditional';
// encryption key
$key = 'thisismysecurekey';
// random initialization vector
$iv = openssl_random_pseudo_bytes(64);
// encrypt and generate the authentication tag
[$ciphertext,$auth_tag] = AESGCM::encrypt($cipher,$aad,$key,$iv);
$bsCipher = bin2hex($ciphertext);
$bsAAD = bin2hex($aad);
$bsKey = bin2hex($key);
$bsIV = bin2hex($iv);
$bsTag = bin2hex($auth_tag);
$insert->bindParam(":cipher",$bsCipher,PDO::ParaM_STR);
$insert->bindParam(":aad",$bsAAD,PDO::ParaM_STR);
$insert->bindParam(":secKey",$bsKey,PDO::ParaM_STR);
$insert->bindParam(":iv",$bsIV,PDO::ParaM_STR);
$insert->bindParam(":aTAg",$bsTag,PDO::ParaM_STR);
if($insert->execute()){
return "Data encrypted";
}else{
return false;
}
}
//Get Data,decrypt it then display it
$column = $_POST['id_of_inserted_data'];
if($select = $this->conn()->prepare("SELECT * FROM security_check WHERE id = :col_id")){
$select->bindParam(":col_id",$column,PDO::ParaM_INT);
$select->execute();
$plaintext = "";
$securities = array();
if($select->rowCount()){
while($rows = $select->fetch(PDO::FETCH_ASSOC)){
$securities = [$rows['cipher'],$rows['auth_tag'],$rows['aad'],$rows['sec_key'],$rows['iv']];
}
$select = null;
}
[$ciphertext,$auth_tag,$iv] = array_map('hex2bin',$securities);
$plaintext = AESGCM::decrypt($ciphertext,$iv);
return "Character Count - ".strlen($plaintext)." ".$plaintext;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)