简体   繁体   English

为什么MySQL的ENCRYPT会在每次调用时返回不同的结果?

[英]Why does MySQL's ENCRYPT return different results on each call?

I have an ugly server issue , and i'm trying not to overlook any details on this. 我有一个丑陋的服务器问题 ,我试图不忽略任何细节。

My virtual email users' passwords are stored with MySQL's ENCRYPT function. 我的虚拟电子邮件用户密码存储在MySQL的ENCRYPT功能中。 My basic idea was I'll dump my virtual users' table from the old machine, then import it in the new one. 我的基本想法是我将从旧机器转储我的虚拟用户表,然后将其导入新机器。

Just for double-check I tried to store a string with ENCRYPT then again, and the stored data was different. 只是为了仔细检查我试图再次存储带有ENCRYPT的字符串,并且存储的数据不同。 Does this mean I can't export/import my users simply as I thought? 这是否意味着我不能像我想的那样导出/导入我的用户?

What Datajam has already described is correct. Datajam已经描述的内容是正确的。 Here's some further explanation. 这是一些进一步的解释。

If you don't supply a salt to the ENCRYPT() function then a random one will be generated and used to encrypt the string. 如果不向ENCRYPT()函数提供salt,则会生成随机的盐并用于加密字符串。 The salt is just two bytes/characters. salt只有两个字节/字符。

First I'll demonstrate that if I run ENCRYPT() twice with the same string it'll give different values (because the random salt differs) 首先,我将演示如果我使用相同的字符串运行ENCRYPT()两次,它将给出不同的值(因为随机盐不同)

mysql> SELECT ENCRYPT('hello');
+------------------+
| ENCRYPT('hello') |
+------------------+
| 5Q5CiJWj4GItY    | 
+------------------+
1 row in set (0.02 sec)

mysql> SELECT ENCRYPT('hello');
+------------------+
| ENCRYPT('hello') |
+------------------+
| 7QHPY3iSLVdas    | 
+------------------+
1 row in set (0.00 sec)

Now if I use the last entry and attempt to ENCRYPT() again using the value we have already as the salt we'll get the same result back: 现在,如果我使用最后一个条目并再次使用我们已经作为salt的值尝试ENCRYPT() ,我们将得到相同的结果:

mysql> SELECT ENCRYPT('hello', '7QHPY3iSLVdas');
+-----------------------------------+
| ENCRYPT('hello', '7QHPY3iSLVdas') |
+-----------------------------------+
| 7QHPY3iSLVdas                     | 
+-----------------------------------+
1 row in set (0.00 sec)

Just to prove that if we get the string (password) wrong with the same salt we'll get a different value. 只是为了证明如果我们得到相同盐的字符串(密码)错误,我们将获得不同的值。 Note that in this example the two first characters (which are just the salt) remain the same. 请注意,在此示例中,两个第一个字符(只是盐)保持不变。

mysql> SELECT ENCRYPT('helloX', '7QHPY3iSLVdas');
+------------------------------------+
| ENCRYPT('helloX', '7QHPY3iSLVdas') |
+------------------------------------+
| 7QKDSis4DZnCU                      | 
+------------------------------------+
1 row in set (0.01 sec)

Using this information you should try to run the ENCRYPT() function both of the MySQL servers specifying the same salt with both you should get the same result back. 使用此信息,您应该尝试运行ENCRYPT()函数,两个MySQL服务器指定相同的salt,您应该得到相同的结果。 If not then the implementation of crypt() likely varies between the two. 如果没有,那么crypt()的实现可能在两者之间有所不同。

I understand this is an old post but if you have a similar problem you don't need to rebuild all the encrypted passwords. 我知道这是一个旧帖子,但如果你有类似的问题,你不需要重建所有加密的密码。 The salt is the first two characters. 盐是前两个字符。

由于这个原因,ENCRYPT函数可能会使用随机值对输入进行加盐 - 您希望将相同的数据加密两次以提供不同的密文。

MySQL's ENCRYPT() function has an optional second argument to define the salt used by the hashing algorithm. MySQL的ENCRYPT()函数有一个可选的第二个参数来定义散列算法使用的salt。 If you do not provide a salt then the result will be different, even for the same input string. 如果您不提供salt,则结果将不同,即使对于相同的输入字符串也是如此。

If you are migrating a database and want to retain the same hashes, just make sure you also use the same salt value. 如果要迁移数据库并希望保留相同的哈希值,请确保使用相同的salt值。 ENCRYPT() should give the same result with the same input string and salt value. ENCRYPT()应该使用相同的输入字符串和salt值给出相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM