MySQL加密和解密案例

时间:2023-12-24 17:29:13
 define("ENCRYPT",'2998a15c0cd853edad7');//密钥key

 被存储的数据库的字段设置成blob格式(二进制)
  //AES_ENCRYPT  加密
 insert into `ks_user`(user_name,user_pwd) values ('kstest',AES_ENCRYPT('aa123456','2998a15c0cd853edad7'));

  //AES_DECRYPT  解密
  select id,user_name, AES_DECRYPT(user_pwd,'2998a15c0cd853edad7') AS user_pwd from `ks_user`;
 +----+-----------+----------+
 | id | user_name | user_pwd |
 +----+-----------+----------+
  | kstest    | aa123456 |
  | hanks     | aa123456 |
 +----+-----------+----------+
  rows in set (0.16 sec)

 select * from `ks_user`;//原始数据存储的是二进制
 +----+-----------+------------------+
 | id | user_name | user_pwd         |
 +----+-----------+------------------+
  ?f
  ?f
 +----+-----------+------------------+
  rows in set (0.15 sec)