MySQL加密解密

时间:2024-03-18 07:29:45

1. 加密:mysql323,不可逆

select old_password(\'bbs.antian365.com\');
# 10c886615b135b38

 

2. 加密:mysqlsha1,不可逆

select password(\'bbs.antian365.com\');
# *A2EBAE36132928537ADA8E6D1F7C5C5886713CC2

 

3. 加密:md5,不可逆

select md5(\'bbs.antian365.com\');
# 3e8ec6f4db678dc7bf9ef71cd6c8b266

 

4. 加密解密: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下的密码明文。

查看密码明文:

 

5. 加密解密:aes_encrypt()和aes_decrypt()

都有两个参数,第一个是实际需要保存的值,第二个是盐。

比encode()和decode()安全性要高。有说,在windows下不可用,我在windows下测试,可以正常执行。

        # 测试表,同样使用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\';

 查看密码明文: