官方文档:

https://dev.mysql.com/doc/refman/5.7/en/encryption-functions.html#function_aes-encrypt

 

加密函数

AES_ENCRYPT(str,key)

例:select AES_ENCRYPT('字符串','mima');

mysql> select AES_ENCRYPT('字符串','mima');
+---------------------------------+
| AES_ENCRYPT('字符串','mima')    |
+---------------------------------+
| 6                     |
+---------------------------------+
1 row in set (0.00 sec)

在存入数据库的时候,转成十六进制

create table t2(c1 varchar(64);
insert into t2 select hex(aes_encrypt(('字符串'),'mima'));

mysql> select * from t2;
+----------------------------------+
| c1 |
+----------------------------------+
| 36CD256BB4BD99CB184D089408954681 |
+----------------------------------+
1 row in set (0.00 sec)

 

 

解密函数 AES_DECRYPT(str,key)

解密之前先用huhex函数转一次

mysql> select aes_decrypt(unhex(c1),'mima') from t2;
+-------------------------------+
| aes_decrypt(unhex(c1),'mima') |
+-------------------------------+
| 字符串                        |
+-------------------------------+
1 row in set (0.00 sec)