加密方式主流的有兩種
ENCODE 與 DECODE
# 建一張測試表
create table users(
username varchar(128), # 用戶昵稱
password blob #密碼
) engine=innodb default charset=utf8;
# 插入一條測試語句
INSERT INTO users (username, password) VALUES ('john', ENCODE('guessme', 'salt'));
commit;
# 查詢john的密碼(用的mysql workbench)
select t.username, DECODE(t.password,'salt') as password from users t where t.username = 'john';
# 在查詢結構的password值上,右鍵,'open value in viewer'。可以看到text TAB下的密碼明文。
AES_ENCRYPT 與 AES_DECRYPT
這個加密方式的安全級別比encode高,在新版本的數據庫中已經棄用encode與decode。
# 測試表,同樣使用users
# 插入一條語句
INSERT INTO users (username, password) VALUES ('steven', aes_encrypt('password', 'salt'));
commit;
# 查詢steven的密碼(用的mysql workbench)
select t.username, aes_decrypt(t.password,'salt') as password from users t where t.username = 'steven';