| Name | Description |
|---|---|
AES_DECRYPT() |
Decrypt using AES |
AES_ENCRYPT() |
Encrypt using AES |
COMPRESS()(v4.1.1) |
Return result as a binary string |
DECODE() |
Decodes a string encrypted using ENCODE() |
DES_DECRYPT() |
Decrypt a string |
DES_ENCRYPT() |
Decrypt a string |
ENCODE() |
Encode a string |
ENCRYPT() |
Encrypt a string |
MD5() |
Calculate MD5 checksum |
OLD_PASSWORD()(v4.1) |
Return the value of the old (pre-4.1) implementation of PASSWORD |
PASSWORD() |
Calculate and return a password string |
SHA1(), SHA() |
Calculate an SHA-1 160-bit checksum |
UNCOMPRESS()(v4.1.1) |
Uncompress a string compressed |
UNCOMPRESSED_LENGTH()(v4.1.1) |
Return the length of a string before compression |
The functions in this section perform encryption and decryption, and compression and uncompression:
| Compression or encryption | Uncompression or decryption |
| AES_ENCRYT() | AES_DECRYPT() |
| COMPRESS() | UNCOMPRESS() |
| ENCODE() | DECODE() |
| DES_ENCRYPT() | DES_DECRYPT() |
| ENCRYPT() | Not available |
| MD5() | Not available |
| OLD_PASSWORD() | Not available |
| PASSWORD() | Not available |
| SHA() or SHA1() | Not available |
| Not available | UNCOMPRESSED_LENGTH() |
Note
The encryption and compression functions return binary strings. For many of these functions, the result might contain arbitrary byte values. If you want to store these results, use a BLOB column rather than a CHAR or (before MySQL 5.0.3) VARCHAR column to avoid potential problems with trailing space removal that would change data values.
Note
Exploits for the MD5 and SHA-1 algorithms have become known. You may wish to consider using one of the other encryption functions described in this section instead.
-
AES_ENCRYPT(,str,key_str)AES_DECRYPT(crypt_str,key_str)These functions allow encryption and decryption of data using the official AES (Advanced Encryption Standard) algorithm, previously known as “Rijndael.” Encoding with a 128-bit key length is used, but you can extend it up to 256 bits by modifying the source. We chose 128 bits because it is much faster and it is secure enough for most purposes.
AES_ENCRYPT()encrypts a string and returns a binary string.AES_DECRYPT()decrypts the encrypted string and returns the original string. The input arguments may be any length. If either argument isNULL, the result of this function is alsoNULL.Because AES is a block-level algorithm, padding is used to encode uneven length strings and so the result string length may be calculated using this formula:
16 × (trunc(
string_length/ 16) + 1)If
AES_DECRYPT()detects invalid data or incorrect padding, it returnsNULL. However, it is possible forAES_DECRYPT()to return a non-NULLvalue (possibly garbage) if the input data or the key is invalid.You can use the AES functions to store data in an encrypted form by modifying your queries:
INSERT INTO t VALUES (1,AES_ENCRYPT('text','password'));AES_ENCRYPT()andAES_DECRYPT()can be considered the most cryptographically secure encryption functions currently available in MySQL. -
COMPRESS(string_to_compress)Compresses a string and returns the result as a binary string. This function requires MySQL to have been compiled with a compression library such as
zlib. Otherwise, the return value is alwaysNULL. The compressed string can be uncompressed withUNCOMPRESS().mysql>
SELECT LENGTH(COMPRESS(REPEAT('a',1000)));-> 21 mysql>SELECT LENGTH(COMPRESS(''));-> 0 mysql>SELECT LENGTH(COMPRESS('a'));-> 13 mysql>SELECT LENGTH(COMPRESS(REPEAT('a',16)));-> 15The compressed string contents are stored the following way:
- Empty strings are stored as empty strings.
- Non-empty strings are stored as a four-byte length of the uncompressed string (low byte first), followed by the compressed string. If the string ends with space, an extra “
.” character is added to avoid problems with endspace trimming should the result be stored in aCHARorVARCHARcolumn. (Use ofCHARorVARCHARto store compressed strings is not recommended. It is better to use aBLOBcolumn instead.)
-
DECODE(crypt_str,pass_str)Decrypts the encrypted string
crypt_strusingpass_stras the password.crypt_strshould be a string returned fromENCODE(). -
ENCODE(str,pass_str)Encrypt
strusingpass_stras the password. To decrypt the result, useDECODE().The result is a binary string of the same length as
str.The strength of the encryption is based on how good the random generator is. It should suffice for short strings.
-
DES_DECRYPT(crypt_str[,key_str])Decrypts a string encrypted with
DES_ENCRYPT(). If an error occurs, this function returnsNULL.Note that this function works only if MySQL has been configured with SSL support. See Section 5.8.7, “Using Secure Connections”.
If no
key_strargument is given,DES_DECRYPT()examines the first byte of the encrypted string to determine the DES key number that was used to encrypt the original string, and then reads the key from the DES key file to decrypt the message. For this to work, the user must have theSUPERprivilege. The key file can be specified with the--des-key-fileserver option.If you pass this function a
key_strargument, that string is used as the key for decrypting the message.If the
crypt_strargument does not appear to be an encrypted string, MySQL returns the givencrypt_str. -
DES_ENCRYPT(str[,{key_num|key_str}])Encrypts the string with the given key using the Triple-DES algorithm.
Note that this function works only if MySQL has been configured with SSL support. See Section 5.8.7, “Using Secure Connections”.
The encryption key to use is chosen based on the second argument to
DES_ENCRYPT(), if one was given:Argument Description No argument The first key from the DES key file is used. key_numThe given key number (0-9) from the DES key file is used. key_strThe given key string is used to encrypt str.The key file can be specified with the
--des-key-fileserver option.The return string is a binary string where the first character is
CHAR(128 |. If an error occurs,key_num)DES_ENCRYPT()returnsNULL.The 128 is added to make it easier to recognize an encrypted key. If you use a string key,
key_numis 127.The string length for the result is given by this formula:
new_len=orig_len+ (8 - (orig_len% 8)) + 1Each line in the DES key file has the following format:
key_numdes_key_strEach
key_numvalue must be a number in the range from0to9. Lines in the file may be in any order.des_key_stris the string that is used to encrypt the message. There should be at least one space between the number and the key. The first key is the default key that is used if you do not specify any key argument toDES_ENCRYPT().You can tell MySQL to read new key values from the key file with the
FLUSH DES_KEY_FILEstatement. This requires theRELOADprivilege.One benefit of having a set of default keys is that it gives applications a way to check for the existence of encrypted column values, without giving the end user the right to decrypt those values.
mysql>
SELECT customer_address FROM customer_table>WHERE crypted_credit_card = DES_ENCRYPT('credit_card_number'); -
ENCRYPT(str[,salt])Encrypts
strusing the Unixcrypt()system call and returns a binary string. Thesaltargument should be a string with at least two characters. If nosaltargument is given, a random value is used.mysql>
SELECT ENCRYPT('hello');-> 'VxuFAJXVARROc'ENCRYPT()ignores all but the first eight characters ofstr, at least on some systems. This behavior is determined by the implementation of the underlyingcrypt()system call.The use of
ENCYPT()with multi-byte character sets other thanutf8is not recommended because the system call expects a string terminated by a zero byte.If
crypt()is not available on your system (as is the case with Windows),ENCRYPT()always returnsNULL. -
MD5(str)Calculates an MD5 128-bit checksum for the string. The value is returned as a binary string of 32 hex digits, or
NULLif the argument wasNULL. The return value can, for example, be used as a hash key.mysql>
SELECT MD5('testing');-> 'ae2b1fca515949e5d54fb22b8ed95575'This is the “RSA Data Security, Inc. MD5 Message-Digest Algorithm.”
If you want to convert the value to uppercase, see the description of binary string conversion given in the entry for the
BINARYoperator in Section 11.9, “Cast Functions and Operators”.See the note regarding the MD5 algorithm at the beginning this section.
-
OLD_PASSWORD(str)OLD_PASSWORD()was added to MySQL when the implementation ofPASSWORD()was changed to improve security.OLD_PASSWORD()returns the value of the old (pre-4.1) implementation ofPASSWORD()as a binary string, and is intended to permit you to reset passwords for any pre-4.1 clients that need to connect to your version 5.0 MySQL server without locking them out. See Section 5.7.9, “Password Hashing as of MySQL 4.1”. -
PASSWORD(str)Calculates and returns a password string from the plaintext password
strand returns a binary string, orNULLif the argument wasNULL. This is the function that is used for encrypting MySQL passwords for storage in thePasswordcolumn of theusergrant table.mysql>
SELECT PASSWORD('badpwd');-> '*AAB3E285149C0135D51A520E1940DD3263DC008C'PASSWORD()encryption is one-way (not reversible).PASSWORD()does not perform password encryption in the same way that Unix passwords are encrypted. SeeENCRYPT().Note
The
PASSWORD()function is used by the authentication system in MySQL Server; you should not use it in your own applications. For that purpose, considerMD5()orSHA1()instead. Also see RFC 2195, section 2 (Challenge-Response Authentication Mechanism (CRAM)), for more information about handling passwords and authentication securely in your applications. -
SHA1(,str)SHA(str)Calculates an SHA-1 160-bit checksum for the string, as described in RFC 3174 (Secure Hash Algorithm). The value is returned as a binary string of 40 hex digits, or
NULLif the argument wasNULL. One of the possible uses for this function is as a hash key. You can also use it as a cryptographic function for storing passwords.SHA()is synonymous withSHA1().mysql>
SELECT SHA1('abc');-> 'a9993e364706816aba3e25717850c26c9cd0d89d'SHA1()can be considered a cryptographically more secure equivalent ofMD5(). However, see the note regarding the MD5 and SHA-1 algorithms at the beginning this section. -
UNCOMPRESS(string_to_uncompress)Uncompresses a string compressed by the
COMPRESS()function. If the argument is not a compressed value, the result isNULL. This function requires MySQL to have been compiled with a compression library such aszlib. Otherwise, the return value is alwaysNULL.mysql>
SELECT UNCOMPRESS(COMPRESS('any string'));-> 'any string' mysql>SELECT UNCOMPRESS('any string');-> NULL -
UNCOMPRESSED_LENGTH(compressed_string)Returns the length that the compressed string had before being compressed.
mysql>
SELECT UNCOMPRESSED_LENGTH(COMPRESS(REPEAT('a',30)));-> 30
Filed under: MySql