MySQL Encrypt returns true on wrong field with numbers as salt
I'm not totally sure how to describe this so the easiest way is with a test case. The running the following sql will return three rows on the select. My understanding of encrypt tells me that this shouldn't return any rows.
It only happens when the salt begins with two numbers.
Please also ignore the fact that encrypt is called how it is. Its a legacy app and I need to understand what is happening before making the change.
CREATE TABLE IF NOT EXISTS `test` (
`user` varchar(10) NOT NULL,
`pass` varchar(10) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `test` (`user`, `pass`) VALUES
('user', '11');
INSERT INTO `test` (`user`, `pass`) VALUES
('user', '22');
INSERT INTO `test` (`user`, `pass`) VALUES
('user', '33');
INSERT INTO `test` (`user`, `pass`) VALUES
('user', 'aa');
SELECT *
FROM `test`开发者_C百科
WHERE encrypt( 'user', test.pass )
Check the output without WHERE clause -
SELECT *, ENCRYPT('user', pass) FROM test;
+------+------+-----------------------+
| user | pass | ENCRYPT('user', pass) |
+------+------+-----------------------+
| user | 11 | 11VKs9AZ4WOfc |
| user | 22 | 22QsW1gRCcd2I |
| user | 33 | 33PLcxSqvhZnc |
| user | aa | aaBrLCcg4bKmQ |
+------+------+-----------------------+
When you use WHERE clause, this values are converted to boolean; in your case value '11VKs9AZ4WOfc' is TRUE, but 'aaBrLCcg4bKmQ' is FALSE.
Here it is a small example that may explain this behavior -
SELECT 'A' = TRUE, '1' = TRUE;
+------------+------------+
| 'A' = TRUE | '1' = TRUE |
+------------+------------+
| 0 | 1 |
+------------+------------+
I think some of your values are cast as TRUE, and some as FALSE.
Here is clearly mentioned
If crypt() is not available on your system (as is the case with Windows), ENCRYPT() always returns NULL.
so first check that crypt()
is available or not
精彩评论