Mysql Encrypt Function

MySQL: ENCRYPT Function

This tutorial explains how to use the MySQL ENCRYPT function with syntax and examples.

Description

The MySQL ENCRYPT function is used to encrypt a string using UNIX crypt().

Syntax

The syntax for the ENCRYPT function in MySQL is:

ENCRYPT( string [, salt ] )

Parameters or Arguments

string

The plaintext string that is encrypted using UNIX crypt().

salt

Optional. The string that is at least 2 characters long that is used in the encryption process. If salt is not provided, the ENCRYPT function will use a random value.

Note

  • The ENCRYPT function will return NULL, if salt is less than 2 characters in length.
  • The ENCRYPT function will return NULL, if the string is NULL.
  • The ENCRYPT function will return NULL, if UNIX crypt() is not available on your system.

Applies To

The ENCRYPT function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1

Example

Let's look at some MySQL ENCRYPT function examples and explore how to use the ENCRYPT function in MySQL.

For example:

mysql> SELECT ENCRYPT('abc');
Output: 'HodO.ryHDWKR2'

mysql> SELECT ENCRYPT('password');
Output: 'Xp7fKf8gFYoMc'

mysql> SELECT ENCRYPT('AODBA');
Output: 'ipQqyRshr94pU'

mysql> SELECT ENCRYPT('AODBA', '123');
Output: '120RNc3daWyrU'

mysql> SELECT ENCRYPT('AODBA', '1');
Output: NULL

mysql> SELECT ENCRYPT(NULL);
Output: NULL