我将一些加密信息存储在MySQL数据库中,但由于某种原因我无法将其恢复。我将加密数据存储为 BINARY(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;
}
答案 0 :(得分:0)
正确的答案是,"据我所知,你不能将加密数据用作where子句中的选择,因为它总是不同的。"
为了避免这种情况(因为我希望能够根据加密数据进行选择),我还存储了加密数据的散列/加盐值,然后我在select语句中使用该值。