为什么第一个和最后一个命令行返回空值??

$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p@ssW0rd', 'ñsñsñsñ');"
NULL
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p@ssW0rd', 'dafdadsfe');"
dac0rB9hTC86M
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p@ssW0rd', 'dafdadsfeñññ');"
dac0rB9hTC86M
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p@ssW0rd', CONCAT('6', SUBSTRING(SHA(RAND()), -16)));"
63YRpxd2B8u1A
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p@ssW0rd', CONCAT('$6', SUBSTRING(SHA(RAND()), -16)));"
c7hTUIQiUwO02
$ mysql -u root -ppass -Bse "SELECT ENCRYPT('p@ssW0rd', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)));"
NULL

我还执行了:
$ mysql -u root -ppass -Bse "SELECT CONCAT('$6$', SUBSTRING(SHA(RAND()), -16));"
$7b07151fc5373796

然后魔法来了:
MariaDB [(none)]> SELECT ENCRYPT('p@ssW0rd', 'ñsñsñsñ');
+------------------------------------+
| ENCRYPT('p@ssW0rd', 'ñsñsñsñ')     |
+------------------------------------+
| NULL                               |
+------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT ENCRYPT('p@ssW0rd', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)));
+------------------------------------------------------------------------------------------------------------+
| ENCRYPT('p@ssW0rd', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16)))                                            |
+------------------------------------------------------------------------------------------------------------+
| $6$0edfe4f3541e5035$kYDyVhQ2sEHDlZE2GsNX0s2Xstg8z7Mj.D3ly.A0DzItg/5WIGoZldKQ0uSiaW9X/ljmmjGbWkMJwsY/WZ4/p. |
+------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

这很烦人,因为我想从命令行插入一个密码字段不为空的表:
$ mysql -u root -ppass  -Bse "INSERT INTO servermail.virtual_users (id, domain_id, password , email) VALUES ('1', '1', ENCRYPT('s1perP@$$', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), '[email protected]');"
ERROR 1048 (23000) at line 1: Column 'password' cannot be null
$ "INSERT INTO servermail.virtual_users (id, domain_id, password , email) VALUES ('1', '1', ENCRYPT('s1perP@$$', CONCAT('$6$', SUBSTRING(SHA(RAND()), -16))), '[email protected]');" | mysql -u root -ppass
ERROR 1048 (23000) at line 1: Column 'password' cannot be null

最佳答案

第一:不要使用MySQLENCRYPT()函数。通过使用它,您将向数据库服务器发送未加密的密码,这可能会使它们受到危害(特别是当数据库服务器位于远程计算机上时)。它在MySQL的未来版本中将不再可用,因此在新的应用程序中使用它是不明智的。
第二:如果您打算不顾我的警告使用ENCRYPT()函数,您需要了解它如何使用salt参数。随机选择的值不太可能有效,特别是如果它们包含非ASCII字符(如ñ),并且将导致函数返回空值。如果你没有一个盐,把这个论点放在一边,使用随机生成的盐,而不是试图自己生成一个盐。

09-04 10:13