content_image/731668.jpg content_image/731669.jpg
INSERT INTO md5_tbl (md5_val, ...) VALUES(UNHEX(MD5(’abcdef’)), ...); 

Applications should be prepared to handle the very rare case that a hashing function produces the same value for two different input values. One way to make collisions detectable is to make the hash column a primary key. 

<img src='content_image/731668.jpg'>

<img src='content_image/731669.jpg'>

• AES_DECRYPT( crypt_str , key_str [, init_vector ]) 

## Note 

Exploits for the MD5 and SHA-1 algorithms have become known. You may wish to consider using another one-way encryption function described in this section instead, such as SHA2() . 

## Caution 

Passwords or other sensitive values supplied as arguments to encryption functions are sent as cleartext to the MySQL server unless an SSL connection is used. Also, such values appear in any MySQL logs to which they are written. To avoid these types of exposure, applications can encrypt sensitive values on the client side before sending them to the server. The same considerations apply to encryption keys. To avoid exposing these, applications can use stored procedures to encrypt and decrypt values on the server side. 

This function decrypts data using the official AES (Advanced Encryption Standard) algorithm. For more information, see the description of AES_ENCRYPT() . 

Statements that use AES_DECRYPT()  are unsafe for statement-based replication. 

• AES_ENCRYPT( str , key_str [, init_vector ]) 

AES_ENCRYPT()  and AES_DECRYPT()  implement encryption and decryption of data using the official AES (Advanced Encryption Standard) algorithm, previously known as “Rijndael.”  The AES standard permits various key lengths. By default these functions implement AES with a 128-bit key length. Key lengths of 196 or 256 bits can be used, as described later. The key length is a trade off between performance and security. 

AES_ENCRYPT()  encrypts the string str  using the key string key_str  and returns a binary string containing the encrypted output. AES_DECRYPT()  decrypts the encrypted string crypt_str  using the key string key_str  and returns the original plaintext string. If either function argument is NULL , the function returns NULL . 

The str  and crypt_str  arguments can be any length, and padding is automatically added to str so it is a multiple of a block as required by block-based algorithms such as AES. This padding is automatically removed by the AES_DECRYPT()  function. The length of crypt_str  can be calculated using this formula: 

16 * (trunc( string_length  / 16) + 1) 

For a key length of 128 bits, the most secure way to pass a key to the key_str  argument is to create a truly random 128-bit value and pass it as a binary value. For example: 

INSERT INTO t VALUES (1,AES_ENCRYPT(’text’,UNHEX(’F3229A0B371ED2D9441B830D21A390C3’))); 

A passphrase can be used to generate an AES key by hashing the passphrase. For example: 

INSERT INTO t 

