SQL AES_ENCRYPT and ENCRYPTBYPASSPHRASE
AES_ENCRYPT(text, key) returns ciphertext bytes and AES_DECRYPT reverses it. The function is easy; keeping the key out of logs is the hard part.
- Категория
- Strings
- Returns
binary- In this category
- 171 of 172
Синтаксис
AES_ENCRYPT(str, key [, iv])
ENCRYPTBYPASSPHRASE(passphrase, text)
Each block is a separate form. The support table and the examples show which engine takes which.
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Different name or syntax | ENCRYPTBYPASSPHRASE, or ENCRYPTBYKEY with a symmetric key |
| PostgreSQL | Not built in | not built in; the pgcrypto extension (pgp_sym_encrypt, encrypt) |
| MySQL | Да | AES_ENCRYPT / AES_DECRYPT |
| SQLite | Not built in | none; encryption builds such as SQLCipher encrypt the whole file |
| Oracle | Different name or syntax | DBMS_CRYPTO.ENCRYPT, a PL/SQL package |
Примеры
MySQL
SELECT HEX(AES_ENCRYPT('secret', 'k')) AS cipher_hex,
CAST(AES_DECRYPT(AES_ENCRYPT('secret', 'k'), 'k') AS CHAR) AS plain; -- E8B5783E9F4703DD32E1688CD2106E66, secret
MySQL
SET block_encryption_mode = 'aes-256-cbc';
SET @iv = RANDOM_BYTES(16);
SET @k = UNHEX(SHA2('my passphrase', 256));
SELECT CAST(AES_DECRYPT(AES_ENCRYPT('secret', @k, @iv), @k, @iv) AS CHAR) AS plain; -- secret
SQL Server
DECLARE @c VARBINARY(8000) = ENCRYPTBYPASSPHRASE('k', 'secret');
SELECT CAST(DECRYPTBYPASSPHRASE('k', @c) AS VARCHAR(100)) AS plain, DECRYPTBYPASSPHRASE('wrong', @c) AS wrong_key; -- secret, NULL
Полезно знать
- MySQL's default block_encryption_mode is aes-128-ecb: no IV, so equal plaintexts encrypt to equal ciphertexts. Set block_encryption_mode to 'aes-256-cbc' and pass a random IV from RANDOM_BYTES(16) as the third argument.
- The key is part of the SQL text, so it can end up in the general log, slow log, binary log or monitoring tools. Encrypting in the application, or using the engine's key management, keeps it out.
- A wrong key does not raise an error. DECRYPTBYPASSPHRASE returns NULL; MySQL's AES_DECRYPT usually returns NULL but can return garbage bytes.
- ENCRYPTBYPASSPHRASE is non-deterministic: the same input encrypts differently each time, so you cannot index or search on the ciphertext.
Questions about AES_ENCRYPT
Should I encrypt passwords with AES_ENCRYPT?
No. Passwords should be hashed with a slow password hash (bcrypt, scrypt, Argon2) in the application, so they cannot be decrypted at all.
What column type holds the ciphertext?
A binary type: VARBINARY or BLOB on MySQL, VARBINARY on SQL Server, RAW or BLOB on Oracle. Storing it in a text column corrupts it under character set conversion.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, MySQL, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.