【发布时间】: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