【问题标题】:Selecting Encrypted Data from MySQL从 MySQL 中选择加密数据
【发布时间】:2015-03-27 05:36:16
【问题描述】:

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

这是我的 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;
}

【问题讨论】:

  • BINARY 类型是哪个字段? accountID 或 created 或其他一些字段。
  • @Phil_1984_accoundID

标签: php mysql encryption


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 2015-04-12
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多