Php 从MySQL中选择加密数据

Php 从MySQL中选择加密数据,php,mysql,encryption,Php,Mysql,Encryption,我将一些加密信息存储在MySQL数据库中,但由于某种原因,我无法将其取回。我将加密数据存储为二进制46。为什么我的select语句失败 以下是我的选择声明: SELECT max(created) FROM incentive_sales WHERE incentive_sales.accountID = :aid 那么,我不应该为select语句加密accountID:aid吗 这是我的加密函数: private function _encrypt($decrypted, $password

我将一些加密信息存储在MySQL数据库中,但由于某种原因,我无法将其取回。我将加密数据存储为二进制46。为什么我的select语句失败

以下是我的选择声明:

SELECT max(created) FROM incentive_sales WHERE incentive_sales.accountID = :aid
那么,我不应该为select语句加密accountID:aid吗

这是我的加密函数:

private function _encrypt($decrypted, $password, $salt = '|SgQLL*ea!UMwf^s%'){
  // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  $key = hash('SHA256', $salt . $password, true);
  // Build $iv and $iv_base64.  We use a block size of 128 bits (AES compliant) and CBC mode.  (Note: ECB mode is inadequate as IV is not used.)
  srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22){
    return false;    
  }
  // Encrypt $decrypted using $key.
  $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted, MCRYPT_MODE_CBC, $iv));
  return $iv_base64.$encrypted;
}
private function _decrypt($encrypted, $password, $salt = '|SgQLL*ea!UMwf^s%'){
  // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  $key = hash('SHA256', $salt . $password, true);
  // Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
  $iv = base64_decode(substr($encrypted, 0, 22) . '==');
  // Remove $iv from $encrypted.
  $encrypted = substr($encrypted, 22);
  // Decrypt the data.
  $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv)
  return $decrypted;
}
和我的解密函数:

private function _encrypt($decrypted, $password, $salt = '|SgQLL*ea!UMwf^s%'){
  // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  $key = hash('SHA256', $salt . $password, true);
  // Build $iv and $iv_base64.  We use a block size of 128 bits (AES compliant) and CBC mode.  (Note: ECB mode is inadequate as IV is not used.)
  srand(); $iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
  if (strlen($iv_base64 = rtrim(base64_encode($iv), '=')) != 22){
    return false;    
  }
  // Encrypt $decrypted using $key.
  $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $decrypted, MCRYPT_MODE_CBC, $iv));
  return $iv_base64.$encrypted;
}
private function _decrypt($encrypted, $password, $salt = '|SgQLL*ea!UMwf^s%'){
  // Build a 256-bit $key which is a SHA256 hash of $salt and $password.
  $key = hash('SHA256', $salt . $password, true);
  // Retrieve $iv which is the first 22 characters plus ==, base64_decoded.
  $iv = base64_decode(substr($encrypted, 0, 22) . '==');
  // Remove $iv from $encrypted.
  $encrypted = substr($encrypted, 22);
  // Decrypt the data.
  $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, base64_decode($encrypted), MCRYPT_MODE_CBC, $iv)
  return $decrypted;
}

正确的答案是,据我所知,您不能在where子句中使用加密数据作为选择,因为它总是不同的


为了避免这种情况,因为我确实希望能够基于加密数据进行选择,我还存储了加密数据的散列/盐析值,然后在select语句中使用该值。

哪个字段是二进制类型?accountID或created或其他一些字段。@Phil_1984_uAccountId