Jump to: navigation, search

Mysql ENCRYPT

From w3cyberlearnings

Contents

MySQL ENCRYPT Function

This function encrypts string by using the Unix crypt() system call and it returns as a binary string. The crypt() is not available in Windows OS, and calling Encrypt() returns NULL.

Syntax ENCRYPT

  • str: string to encrypt
  • salt (optional): salt key (without this argument, it will randomly generate a salt key)
ENCRYPT(str, salt);

Example 1

mysql> SELECT ENCRYPT('great');
+------------------+
| ENCRYPT('great') |
+------------------+
| VHL6l0u9IcEy6    | 
+------------------+
1 row in set (0.01 sec)

mysql> SELECT ENCRYPT('great','sal31');
+--------------------------+
| ENCRYPT('great','sal31') |
+--------------------------+
| sa4kB9DfBtPLk            | 
+--------------------------+
1 row in set (0.00 sec)

Example 2

mysql> INSERT INTO userpassword (username, password)
    -> VALUES('Tarlar',ENCRYPT('password123','good'));
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM userpassword;
+----+----------+---------------+
| id | username | password      |
+----+----------+---------------+
|  1 | Tarlar   | goBU.g71fjk6Y | 
+----+----------+---------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM userpassword
    -> WHERE password=ENCRYPT('password123');
Empty set (0.00 sec)

mysql> SELECT * FROM userpassword 
    -> WHERE password=ENCRYPT('password123','good');
+----+----------+---------------+
| id | username | password      |
+----+----------+---------------+
|  1 | Tarlar   | goBU.g71fjk6Y | 
+----+----------+---------------+
1 row in set (0.00 sec)

Related Links


Navigation
Web
SQL
MISC
References