我试图通过while循环传递加密数据,以便从数据库中解密数据。我能够得到一些结果。但是,由于解密依赖于字符串是否与密钥匹配。如果密钥不匹配,它就不会显示任何内容,因为所有字符串组合在一起,而不是在每个表格行上打破。
我看到了什么:
如果加密数据行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循环中做什么,分别输出每个数据点?
答案 0 :(得分:-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/>";
}
}