Skip to content

DECODE()

Function Description

The DECODE() function is used to decrypt the data encoded by ENCODE().

Function Syntax

> DECODE(crypt_str, pass_str)

Parameter definition

| Parameters | Description | | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | crypt_str | Encrypted string encoded by ENCODE(). | | pass_str | The password string used for decryption must be the same as the key used for encryption. |

Example

mysql> SELECT DECODE(ENCODE('hello', 'mysecretkey'), 'mysecretkey');
+------------------------------------------------------------------+
| DECODE(ENCODE(hello, mysecretkey), mysecretkey) |
+------------------------------------------------------------------+
| hello |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255),
    encrypted_password BLOB
);

INSERT INTO users (username, encrypted_password)
VALUES ('john', ENCODE('password123', 'mysecretkey'));

mysql> SELECT username, DECODE(encrypted_password, 'mysecretkey') AS decrypted_password FROM users WHERE username = 'john';
+------------------------------------------+
| username | decrypted_password |
+------------------------------------------+
| john | password123 |
+------------------------------------------+
1 row in set (0.00 sec)