SQL Tutorial SQL Advanced SQL Database SQL References

MySQL ENCRYPT() Function



The MySQL ENCRYPT() function is used to encrypt a string using UNIX crypt() and returns the encrypted value of the string. This function uses salt in the encryption process and the salt should be at least 2 character long. If the salt is not provided, this function will use a random value as salt. In special cases it returns the following:

  • Returns NULL if salt is less than 2 characters in length.
  • Returns NULL if the string is NULL.
  • Returns NULL if UNIX crypt() is not available on the system.

Syntax

ENCRYPT(string, salt)

Parameters

string Required. Specify the plaintext string to be encrypted using UNIX crypt().
salt Optional. Specify the string should be at least 2 characters long. It is used in the encryption process. If it is not provided, this function will use a random value.

Return Value

Returns the encrypted value of the string.

Example:

The example below shows the usage of ENCRYPT() function.

mysql> SELECT ENCRYPT('password');
Result: 'EY8ULaXsqebTI'

mysql> SELECT ENCRYPT('password', 'key');
Result: 'keZqlq1fzdLxY'

mysql> SELECT ENCRYPT('password', 'K');
Result: NULL

mysql> SELECT ENCRYPT('alphacodingskills');
Result: 'dbtIyw3v.ciJY'

mysql> SELECT ENCRYPT(NULL);
Result: NULL

❮ MySQL Functions