MySQL存储数据加密

加密方式主流的有两种

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';

posted @ 2022-02-13 21:15  看不见的R  阅读(1455)  评论(0编辑  收藏  举报