【问题标题】:While Loop encrypted dataWhile Loop 加密数据
【发布时间】:2016-04-23 06:47:28
【问题描述】:

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

我所看到的:

如果加密数据第一行是: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 循环中做什么,分别输出每个数据点?

【问题讨论】:

  • 我真的不明白这个问题吗?现在正在逐行进行。它只是在同一行上呼应它的“回声”。为每次迭代添加&lt;br /&gt;?并且您应该将 Security 类的实例化放在 while 循环之前。
  • 我认为您的问题是您没有在单独的行上获得输出,但我不太确定。可能是您在解密时遇到问题,因为您的输出看起来与输入相同。如果您想要的只是单独的行,那么添加 'echo "

    " ' 会有所帮助吗?
  • 在您尝试使用 128 位算法解密的代码中,您正在检查 256 位的密钥长度 .... 不计算,您的代码将永远无法正常工作。完全没有。
  • ...您不应该使用已弃用的 mysql_* 函数。使用 mysqli 或 PDO 与准备好的语句代替。

标签: php mysql database encryption while-loop


【解决方案1】:

就这样做吧:

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/>";
   }
}

【讨论】:

  • 你真的应该解释你在做什么,而不仅仅是发布一段代码。我们应该互相帮助学习,而不仅仅是为他们做他们的工作。
  • 另一件事...$data-&gt;result()...那是什么? $data 是一个 mysql 结果资源,据我所知没有任何 ::result() 方法?
  • @MagnusEriksson 抱歉,我正在使用 Codeigniter,它是 codeigniter 的语法。
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 2016-05-16
  • 2021-05-29
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
相关资源
最近更新 更多