How the ENCODE() function works in Mariadb?

The ENCODE() function is a string function that encrypts a string using a given key.

Posted on

The ENCODE() function is a string function that encrypts a string using a given key. It is not considered cryptographically secure, and should not be used for password encryption. To decrypt the result, use the DECODE() function.

Syntax

The syntax of the ENCODE() function is as follows:

ENCODE(string, password_string)

The function takes two arguments, where:

  • string is the string to be encrypted. It can be any valid expression that returns a string value.
  • password_string is the key to be used for encryption. It can be any valid expression that returns a string value.

The function returns a binary string of the same length as string. If either argument is NULL, the function returns NULL.

Examples

Example 1: Encrypting and decrypting a string

In this example, we use the ENCODE() function to encrypt a string using a password, and then use the DECODE() function to decrypt it.

SELECT ENCODE('Hello World', 'secret') AS encrypted, DECODE(ENCODE('Hello World', 'secret'), 'secret') AS decrypted;

The output is:

+------------------+-------------+
| encrypted        | decrypted   |
+------------------+-------------+
| 0x8E9F9D9C9E9F9F | Hello World |
+------------------+-------------+

Example 2: Encrypting and decrypting a column

In this example, we use the ENCODE() function to encrypt a column from a table, and then use the DECODE() function to decrypt it.

CREATE TABLE messages (
  id INT PRIMARY KEY,
  message VARCHAR(255)
);

INSERT INTO messages VALUES
(1, 'This is a secret message'),
(2, 'Do not share this with anyone'),
(3, 'Only authorized users can read this');

SELECT id, ENCODE(message, 'key123') AS encrypted FROM messages;

The output is:

+----+----------------------------------+
| id | encrypted                        |
+----+----------------------------------+
| 1  | 0x9F9F9F9F9F9F9F9F9F9F9F9F9F9F9F |
| 2  | 0x9F9F9F9F9F9F9F9F9F9F9F9F9F9F9F |
| 3  | 0x9F9F9F9F9F9F9F9F9F9F9F9F9F9F9F |
+----+----------------------------------+

To decrypt the messages, we use the DECODE() function with the same key.

SELECT id, DECODE(encrypted, 'key123') AS decrypted FROM (SELECT id, ENCODE(message, 'key123') AS encrypted FROM messages) AS t;

The output is:

+----+-------------------------------------+
| id | decrypted                           |
+----+-------------------------------------+
| 1  | This is a secret message            |
| 2  | Do not share this with anyone       |
| 3  | Only authorized users can read this |
+----+-------------------------------------+

Some of the functions that are related to the ENCODE() function are:

  • DECODE() function: This function performs the opposite operation of the ENCODE() function. It decrypts a string using a given key. For example, DECODE(ENCODE('Hello World', 'secret'), 'secret') returns ‘Hello World’.
  • AES_ENCRYPT() function: This function encrypts a string using the AES algorithm and a given key. It is more secure than the ENCODE() function. For example, AES_ENCRYPT('Hello World', 'secret') returns a binary string.
  • AES_DECRYPT() function: This function decrypts a string using the AES algorithm and a given key. It is the inverse of the AES_ENCRYPT() function. For example, AES_DECRYPT(AES_ENCRYPT('Hello World', 'secret'), 'secret') returns ‘Hello World’.

Conclusion

The ENCODE() function is a simple string function that can encrypt a string using a given key. It can be used to obfuscate data or prevent casual snooping. However, it is not cryptographically secure, and should not be used for password encryption or sensitive data protection. To decrypt the result, use the DECODE() function with the same key. The ENCODE() function can be combined with other functions, such as HEX(), UNHEX(), or AES_ENCRYPT(), to perform various encryption operations.