Now you can run exactly the same thing again:
mysql> SELECT ENCRYPT('something secret');
This time the result is different:
+-----------------------------+
| ENCRYPT('something secret') |
+-----------------------------+
| tRCUBHu2/nW9g |
+-----------------------------+
By specifying the salt, you get a predictable result. There is no method of "decrypting," but you can
check whether an encrypted string corresponds with an unencrypted string which is tested against it.
For example, if you have an encrypted version of a password stored in your database but no "plain text"
version, you can still establish whether a test password matches the one stored.
Using the second example, you'll use the salt—the first two characters—from the encrypted string, and
once again encrypt your string (which might be the new test password):
mysql> SELECT ENCRYPT('something secret','tR');
You have taken "tR" from the encrypted text, and the output is as follows:
+----------------------------------+
| ENCRYPT('something secret','tR') |
+----------------------------------+
| tRCUBHu2/nW9g |
+----------------------------------+
Carefully comparing the encrypted results, you see that they are the same.
ENCODE(string,password), DECODE(encoded_string,password)
The ENCODE() function encodes a given string with password as the access password and returns a
binary string.
DECODE() does the opposite, taking the binary encoded_string and decoding it with password.
For example, to encode
mysql> SELECT ENCODE('my string','mypass');
would produce
+------------------------------+
| ENCODE('my string','mypass') |
+------------------------------+
| = ̧+?TSz‹ |
+------------------------------+
1 row in set (0.07 sec)
To decode
mysql> SELECT DECODE(ENCODE('my string','mypass'),'mypass');
would produce
+-----------------------------------------------+
| DECODE(ENCODE('my string','mypass'),'mypass') |
+-----------------------------------------------+
| my string |
+-----------------------------------------------+