php - While 循环加密数据

标签 php mysql database encryption while-loop

我正在尝试通过 while 循环传递加密数据,以便从数据库中解密数据。我能够得到一些结果。但是,由于解密依赖于字符串是否与 key 匹配。如果键不匹配,它不会显示任何内容,因为所有字符串都组合在一起,而不是在每个表行上断开。

我看到的情况:

如果加密数据线1是:1234

如果加密数据行2是:5678

+-------------------+
| client_name       |
+-------------------+
| 1234              |
| 5678              |
+-------------------+

while 循环的结果是:12345678。我想要实现的是让 while 循环逐行输出以进行解密。

这是我的 while 循环输出:

<?php
  $data = mysql_query("SELECT * FROM invoices WHERE username = '$username'") or die(mysql_error());
    while($info = mysql_fetch_array( $data ))
    {
    $security = new Security();
    echo "<a href='#' class='list-group-item'>".$security->decrypt($info['client_name'])." <span class='label label-default pull-right'>Invoice 20</span></a>";
  }
?>

这是我要解密的内容:(基于 - http://wpy.me/blog/15-encrypt-and-decrypt-data-in-php-using-aes-256)

<?php class Security {

            # Private key
            public static $salt = 'ZfTfbipGsZ4yz34jFrGHagahptzLN7ROigy';


            # Encrypt a value using AES-256.
            public static function encrypt($plain, $key = null, $hmacSalt = null) {
                if(empty($key)) {
                    $key = self::$salt;
                }

                self::_checkKey($key, 'encrypt()');

                if ($hmacSalt === null) {
                    $hmacSalt = self::$salt;
                }

                $key = substr(hash('sha256', $key . $hmacSalt), 0, 32); # Generate the encryption and hmac key

                $algorithm = MCRYPT_RIJNDAEL_128; # encryption algorithm
                $mode = MCRYPT_MODE_CBC; # encryption mode

                $ivSize = mcrypt_get_iv_size($algorithm, $mode); # Returns the size of the IV belonging to a specific cipher/mode combination
                $iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_URANDOM); # Creates an initialization vector (IV) from a random source
                $ciphertext = $iv . mcrypt_encrypt($algorithm, $key, $plain, $mode, $iv); # Encrypts plaintext with given parameters
                $hmac = hash_hmac('sha256', $ciphertext, $key); # Generate a keyed hash value using the HMAC method
                return $hmac . $ciphertext;
            }

            # Check key
            protected static function _checkKey($key, $method) {
                if (strlen($key) < 32) {
                    echo "Invalid key $key, key must be at least 256 bits (32 bytes) long."; die();
                }
            }

            # Decrypt a value using AES-256.
            public static function decrypt($cipher, $key = null, $hmacSalt = null) {
                if(empty($key)) {
                    $key = self::$salt;
                }

                self::_checkKey($key, 'decrypt()');

                if (empty($cipher)) {
                    echo 'The data to decrypt cannot be empty.'; die();
                }
                if ($hmacSalt === null) {
                    $hmacSalt = self::$salt;
                }

                $key = substr(hash('sha256', $key . $hmacSalt), 0, 32); # Generate the encryption and hmac key.

                # Split out hmac for comparison
                $macSize = 64;
                $hmac = substr($cipher, 0, $macSize);
                $cipher = substr($cipher, $macSize);

                $compareHmac = hash_hmac('sha256', $cipher, $key);
                if ($hmac !== $compareHmac) {
                    return false;
                }

                $algorithm = MCRYPT_RIJNDAEL_128; # encryption algorithm
                $mode = MCRYPT_MODE_CBC; # encryption mode
                $ivSize = mcrypt_get_iv_size($algorithm, $mode); # Returns the size of the IV belonging to a specific cipher/mode combination

                $iv = substr($cipher, 0, $ivSize);
                $cipher = substr($cipher, $ivSize);
                $plain = mcrypt_decrypt($algorithm, $key, $cipher, $mode, $iv);
                return rtrim($plain, "\0");
            }

        }
?>

您建议我在 while 循环中做什么,以分别输出每个数据点?

最佳答案

就像这样:

if($data->num_rows() > 0){
   // create her security object not inside loop.
   $security = new Security();

   foreach($data->result() as $row){
       echo "<a href='#' class='list-group-item'>".$security->decrypt($row->client_name)." <span class='label label-default pull-right'>Invoice 20</span></a><br/>";
   }
}

关于php - While 循环加密数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848949/

相关文章:

php - 如何更新 jQuery DatePicker 的 minDate 值?

php - Trello 使用 api 获取卡片的创建日期

php - 如何使用 php 和 mysql 显示 future 30 天的数据?

javascript - 使用基于子数组元素 id 的键创建 couchdb View

mysql - 如何添加新列并从其他列获取数据?数据库

php - Doctrine 2.1 - 获取实体限制

PHP5.3 : How to use PDO singleton class (indirectly) from the main source code

MySQL 1 :N Data Mapping

mysql - 动态 SQL = [错误] 42000 - [SQL Server] '01' 附近的语法不正确

mysql - Mysql大表优化