MariaDB DECODE() Function
Learn how MariaDB DECODE() reverses ENCODE() in the default SQL mode, and why this legacy pair is not secure encryption.
On this page
In MariaDB’s default SQL mode, DECODE(crypt_str, pass_str) reverses a value produced by ENCODE() when the same pass string is supplied. The pair is not cryptographically secure and must not be used to protect passwords or sensitive data. See the official DECODE() documentation.
In Oracle mode, DECODE() has Oracle-compatible conditional-expression behavior instead. The DECODE_ORACLE() function provides that behavior in all modes.
Syntax
DECODE(crypt_str, pass_str)
crypt_stris a binary string returned byENCODE().pass_stris the same pass string supplied toENCODE().
Example
The following statements use non-sensitive sample text and keep the encoded value in a session variable:
SET @encoded_demo = ENCODE('sample text', 'demo-key');
SELECT DECODE(@encoded_demo, 'demo-key') AS decoded_text;
The query returns sample text. It demonstrates how the two functions fit together; it is not an encryption recipe for confidential data.
Security note
Do not use the ENCODE()/DECODE() pair for application passwords or confidential fields. Use an adaptive password-hashing library for passwords, and a reviewed encryption design with protected keys when data must be recovered later. See the OWASP Password Storage Cheat Sheet and Cryptographic Storage Cheat Sheet.
Related functions
ENCODE()creates the value thatDECODE()reverses.PASSWORD()is a MariaDB Server account-authentication function; MariaDB says it is not intended for application password storage.