
本文旨在解决在使用 openssl_encrypt 函数加密 PHP 数组数据时遇到的常见问题,包括加密结果不一致以及如何正确使用 continue 语句跳过特定数组元素的加密。通过详细的代码示例和解释,帮助开发者理解并解决这些问题,确保数据加密的正确性和安全性。
在使用 openssl_encrypt 加密数组数据时,可能会遇到以下两个主要问题:
这两个问题通常与密钥管理和循环逻辑有关。
加密结果不一致的根本原因在于,在循环中使用数组的键(key)覆盖了预定义的加密密钥 $key。 openssl_encrypt 函数使用这个被覆盖的键作为密钥,导致每次加密使用的密钥不同,从而产生不同的加密结果。
立即学习“PHP免费学习笔记(深入)”;
解决方案:
修改循环结构,使用不同的变量名作为循环索引,避免覆盖预定义的加密密钥 $key。
错误示例:
$key = "your_encryption_key";
foreach ($array as $section => $items) {
foreach ($items as $key => $value) { // 错误:$key 被覆盖
$encrypted = openssl_encrypt($value, $cipher, $key, $options=0, $iv);
}
}正确示例:
$key = "your_encryption_key";
foreach ($array as $section => $items) {
foreach ($items as $index => $value) { // 正确:使用 $index 避免覆盖
$encrypted = openssl_encrypt($value, $cipher, $key, $options=0, $iv);
}
}在修改后的代码中,使用 $index 代替 $key 作为内部循环的索引,这样可以确保 openssl_encrypt 函数始终使用预定义的 $key 进行加密。
continue 语句用于跳过当前循环的剩余部分,并进入下一次循环。如果 continue 语句未能按预期工作,通常是因为条件判断不正确。
问题:
原始代码尝试使用 $items zuojiankuohaophpcn 2 作为 continue 的条件,这实际上是在比较整个 $items 数组与数值 2,而不是检查当前数组元素的索引。
解决方案:
根据数组索引的格式(数字或字符串),选择合适的条件判断方式。
如果数组索引是数字:
直接比较索引值。
foreach ($items as $index => $value) {
if ($index < 2) {
continue;
}
// 加密逻辑
}如果数组索引是字符串(例如,"[0]"):
需要先移除字符串中的括号,再进行比较。
foreach ($items as $index => $value) {
if (str_replace(['[', ']'], '', $index) < 2) {
continue;
}
// 加密逻辑
}完整示例代码:
<?php
$bgyaa = array(
'[0]' => array(
'[0]' => '2',
'[1]' => 'bgyaa.ZBRDE5aTZsUGZmWQ',
'[2]' => '12346',
'[3]' => 'John Citizen',
'[4]' => 'noy-pic-1.jpg',
'[5]' => 'noy-pic-2.jpg',
'[6]' => 'RESIDENT',
'[7]' => '777 Sarangani Street',
'[8]' => '03/27/84',
'[9]' => 'B',
'[10]' => '287-865-194',
'[11]' => ''),
'[1]' => array(
'[0]' => '3',
'[1]' => 'bgyaa.ZMTEtpTC5qVGNTUQ',
'[2]' => '12347',
'[3]' => 'Dominador Pridas',
'[4]' => 'domeng-pic-1.jpg',
'[5]' => 'domeng-pic-2.jpg',
'[6]' => 'TENANT',
'[7]' => '321 Mango Drive',
'[8]' => '03/27/84',
'[9]' => 'B',
'[10]' => '287-865-194',
'[11]' => ' '),
'[2]' => array(
'[0]' => '4',
'[1]' => 'bgyaa.ZpcEpteDJOZlBVQQ',
'[2]' => '12348',
'[3]' => 'Taylor Swift',
'[4]' => 'taylorswift-pic-1.jpg',
'[5]' => 'taylorswift-pic-2.jpg',
'[6]' => 'TENANT',
'[7]' => '826 Anonas Street',
'[8]' => '03/27/84',
'[9]' => 'B',
'[10]' => '287-865-194',
'[11]' => ' '),
);
$key = "c871754451c2b89d4cdb1b14705be457b7fabe967af6a559f3d20c79ded5b5ff18675e56fa77d75fdcd47c34271bb74e372d6d04652f7aa6f529a838ca4aa6bd";
$iv = "f1e64276d153ad8a";
$cipher = "aes-256-cbc-hmac-sha256";
if (in_array($cipher, openssl_get_cipher_methods())) {
$ivlen = openssl_cipher_iv_length($cipher);
$plain_text = 'John Citizen';
$encrypted = openssl_encrypt($plain_text, $cipher, $key, $options = 0, $iv);
echo "<br/><br/><br/>Bellw are from direct encrytion of the plain text name<br/>";
echo "plain text is John Citizen " . "<br/>";
echo "encrypted text is " . $encrypted . "<br/><br/><br/>";
}
echo "And then below are openssl_encrypt (cipher aes-256-cbc) encrypted array codes beside their plain text original values<br/>";
echo "NOTE that the encrypted code q+vG/KXTZsYExxV5yX7DFw== for the name John Citizen is different to the above, and not decryptable<br/><br/>";
foreach ($bgyaa as $section => $items) {
foreach ($items as $index => $value) {
// 使用 $index 代替 $key
// 使用 str_replace 处理字符串索引
if (str_replace(['[', ']'], '', $index) < 2) {
continue;
}
if (in_array($cipher, openssl_get_cipher_methods())) {
$ivlen = openssl_cipher_iv_length($cipher);
$encrypted = openssl_encrypt($value, $cipher, $key, $options = 0, $iv);
}
echo $index . " : " . $encrypted . " : " . $value . "<br/>";
}
}
echo "";
?>通过避免在循环中覆盖加密密钥,并根据数组索引的格式选择正确的条件判断方式,可以有效解决在使用 openssl_encrypt 函数加密 PHP 数组数据时遇到的加密结果不一致和循环控制失效问题。同时,注意密钥管理、IV 的选择和错误处理,以确保数据加密的安全性。
以上就是使用 OpenSSL 加密 PHP 数组数据:解决加密不一致和循环控制问题的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号