当前位置:网站首页>MySQL存储数据加密

MySQL存储数据加密

2022-07-04 21:37:00 CodingSir

加密方式主流的有两种

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';
原网站

版权声明
本文为[CodingSir]所创,转载请带上原文链接,感谢
https://blog.csdn.net/educast/article/details/125479097