【发布时间】:2019-12-27 13:58:49
【问题描述】:
我是 PHP 新手,我正在为我在大学的一门学科开发一个 Web 应用程序。该网络应用程序包含预订自然活动的平台。 我的问题在于其中一项要求“加密/解密数据”,其中我需要在信用卡数据进入数据库之前对其进行加密。 为简单起见,信用卡字段位于预订表中。 这是我的代码:
$fieldsReservation = array(
'idUser' => $idUser,
'idActivity' => $idActivity,
'reservationDate' => $reservationDate,
'state' => 'reserved',
'cardName' => $cardName,
'cardType' => $cardType,
'cardNumber' => $cardNumber,
'cardExpiry' => $cardExpiry,
'cardCVV' => $cardCVV);
$password = '3sc3RLrpd17';
$method = 'aes-256-cbc';
// password must be exact 32 chars (256 bit)
$password = substr(hash('sha256', $password, true), 0, 32);
// IV must be exact 16 chars (128 bit)
$iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
foreach ($fieldsReservation as $key => $value){
$fieldsReservation[$key] = base64_encode(openssl_encrypt($value, $method, $password, OPENSSL_RAW_DATA, $iv));
}
我已经尝试过使用foreach,但是使用foreach,所有数据都已加密,我只想加密信用卡数据。
如果有人可以帮助我,我将不胜感激!
【问题讨论】:
-
很高兴知道一个新人加密信用卡并将它们保存到数据库中,我感谢您提供的解密密钥。你的项目的 URL 又是什么?纯粹出于好奇而询问。
-
通过重复使用密钥和IV,您可以绝对确定结果并不像应有的那样安全。更重要的是,密码与密钥不同,SHA-256 不是一个好的基于密码的密钥派生函数。因此,即使代码有效,您的代码也不安全,这毕竟是加密的目标。
标签: php encryption aes