【mysql】利用函数md5/ENCRYPT/AES_ENCRYPT/ENCODE/HEX/to_base64给数据库字段加解密(小白指南至简单易用)

小白上手使用指南,按照步骤一步一步执行即可,总共介绍7个维度的加解密

  1. md5加密
  2. ENCRYPT加密
  3. AES_ENCRYPT/AES_DECRYPT加密解密
  4. ENCODE/DECODE加密解密
  5. HEX/UNHEX结合AES_ENCRYPT的加密解密
  6. to_base64/to_base64加密解密

 

1、创建数据库

 CREATE TABLE  IF NOT EXISTS `test` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'ID',
`content` blob NOT NULL COMMENT '内容列',
PRIMARY KEY (`id`)
 )ENGINE=innodb DEFAULT CHARSET=latin1 COMMENT='压缩测试表';

2、md5加密

insert into `test`(content) values(md5("aa"));

查看加密结果

mysql> select * from test where id=1;
+----+----------------------------------+
| id | content                          |
+----+----------------------------------+
|  1 | 4124bc0a9335c27f086f24ba207a4912 |
+----+----------------------------------+

【注】MD5是HASH算法,因此无法解密。要测试某个值是否正确,可以将MD5(值)与存储的MD5进行比较,但是无法从MD5中提取原始值

【说明】如果实在想解密,可以用这个网站:https://www.cmd5.com/  

3、ENCRYPT加密

insert into `test`(content) values(ENCRYPT("bb"));

查看加密结果

mysql> select * from test where id=2;
+----+---------------+
| id | content       |
+----+---------------+
|  2 | p1hzG5OV.NpJQ |
+----+---------------+

4、AES_ENCRYPT/AES_DECRYPT加密解密

insert into `test`(content) values(AES_ENCRYPT("cc","cc1"));

查看加密结果

mysql> select * from test where id=3;
+----+------------------+
| id | content          |
+----+------------------+
|  3 | ���f%u�
              0ԙ�        |
+----+------------------+

查看解密后的结果

mysql> SELECT AES_DECRYPT(content,'cc1') FROM test WHERE id='3';
+----------------------------+
| AES_DECRYPT(content,'cc1') |
+----------------------------+
| cc                         |
+----------------------------+

5、ENCODE/DECODE加密解密

insert into `test`(content) values(ENCODE('dd', 'dd1'));

查看加密结果

mysql> select * from test where id=4;
+----+---------+
| id | content |
+----+---------+
|  4 | }�       |
+----+---------+

查看解密后的结果

mysql> SELECT content, DECODE(content, 'dd1') as content FROM test WHERE id='4';
+---------+---------+
| content | content |
+---------+---------+
| }�       | dd      |
+---------+---------+

6、HEX/UNHEX结合AES_ENCRYPT的加密解密

insert into `test`(content) values(HEX(AES_ENCRYPT("yj","yj1")));

查看加密结果

mysql> select * from test where id=6;
+----+----------------------------------+
| id | content                          |
+----+----------------------------------+
|  6 | BE4A93F62A197358552E4D99C15D979B |
+----+----------------------------------+

查看解密后的结果

mysql> SELECT AES_DECRYPT(UNHEX('BE4A93F62A197358552E4D99C15D979B'),'yj1') FROM test where id=6;
+--------------------------------------------------------------+
| AES_DECRYPT(UNHEX('BE4A93F62A197358552E4D99C15D979B'),'yj1') |
+--------------------------------------------------------------+
| yj                                                           |                                                           |
+--------------------------------------------------------------+

7、to_base64/to_base64加密解密

insert into `test`(content) values(to_base64("xwh"));

查看加密后的结果

mysql> select * from test where id=8;
+----+---------+
| id | content |
+----+---------+
|  8 | eHdo    |
+----+---------+

查看解密后的结果

SELECT from_base64("eHdo") from test where id=8;
+---------------------+
| from_base64("eHdo") |
+---------------------+
| xwh                 |
+---------------------+

 

加密总结

1. **签名** :对加密字段签名,如MD5,但是这种是不可逆的,一般用于校验密码或者其它需要校验的信息,不适用常规业务;
2. **DECODE/ENCODE**:这个貌似没问题,实现方式也比较简单,select ENCODE('my program','1');
SELECT DECODE((select ENCODE('my program','1')),'1');;但是有个问题,要求数据库字段是二进制类型,不喜欢用大数据类型,java代码存储层需要翻新
3. **编码/解码**:如base64、HEX()函数,只需要将字段进行编码/解码,存储的时候还是字符串,当然也有缺点,没有加密秘钥,如果知道用的什么编码方式就可以解码;

 

posted @ 2022-08-02 17:42  Syw_文  阅读(1427)  评论(0编辑  收藏  举报