MySQL encryption/Compression function _mysql

Source: Internet
Author: User
Tags crypt decrypt md5 sha1 uncompress

These problems can result in changes in data values. In general, the above problem may occur if you use a non binary string data type, such as char,varchar,text data types.

    • Aes_encrypt () and Aes_decrypt ()

Aes_encrypt () and Aes_decrypt () can encrypt/decrypt data using the official AES algorithm. The algorithm uses a 128-bit key to encode, but the user can extend it to 256 bits. MySQL chooses a 128-bit key because the algorithm is faster and is safe enough for most users.


The Aes_encrypt (STR,KEY_STR) function encrypts a string and returns a binary string. The Aes_decrypt (crypt_str, KEY_STR) function decrypts data encrypted using the official AES (Advanced encryption Standard) algorithm and returns the original string, which can be any length. If the input variable is NULL, the function returns the result also null.


Because AES is a block-level algorithm, you need to use fillers to encode strings that are not even-numbered.

    • ENCODE () and Decode ()

ENCODE (str, PASS_STR): This function encrypts string str using PASS_STR as a password, and its encrypted results can be decrypted by the decode () function. The result returned by the function is a same length as Str. DECODE (Crypt_str, PASS_STR): The function uses PASS_STR as a password to decrypt the string encode that is encrypted with CRYPT_STR ().

    • Des_encrypt () and Des_encrypt ()

Des_encrypt (str[, {key_num|key_str}]): This function encrypts the encrypted string with the given key using the Triple DES algorithm.
Des_decrypt (crypt_str[, Key_str]): This function decrypts a string that is encrypted by Des_encrypt (), and returns null if an error occurs.

    • COMPRESS () and uncompress ()

COMPRESS (string_to_compress): This function compresses a string and returns a binary string. This function requires MySQL to be compiled with a single compressed library, such as zlib, otherwise the return value of the function is always null. The compressed string can be decompressed by the uncompress () function. Uncompress (string_to_uncompress): This function compresses a string that is compressed by the compress () function. If the variable is not a compressed value, the result is returned to NULL.

    • PASSWORD ()

PASSWORD (str): This function is used to encrypt the mysql password stored in the PASSWORD column in the user table. The PASSWORD () function is used by the authentication system in the MySQL server, and users should not use the function in their own applications. If you need to use an encryption function, consider using either MD5 () or SHA1 () instead.

Examples of its cryptographic results are as follows:


In MySQL's system database MySQL user table, there is a column named password, which holds the password data of the user encrypted by the password function. As shown below:


    • ENCRYPT ()

ENCRYPT (str[, salt]): This function is used to encrypt str by using the UNIX crypt () system, and returns a binary string. Where the salt variable should be a string that contains more than two characters. If the salt is not given, a random value is used. If the crypt () system call is not available on the user's operating system (as is the case with the Windows operating system), the function returns NULL.

    • MD5 ()

MD5 (str): This function computes a 128-bit MD5 checksum of a string, and the result returned is a binary string of 32 hexadecimal digits. If the variable is NULL, it is returned to NULL.

Examples of its cryptographic results are as follows:

    • SHA1 ()/sha ():

The SHA1 (str)/sha (str) function computes the 160-bit SHA-1 checksum of the string str. The return value is a binary string consisting of 40 hexadecimal digits. If the variable is NULL, NULL is returned.

Examples of its cryptographic results are as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.