《Mysql实例MySQL利用AES_ENCRYPT()与AES_DECRYPT()加解密的正确方法示例》要点:
本文介绍了Mysql实例MySQL利用AES_ENCRYPT()与AES_DECRYPT()加解密的正确方法示例,希望对您有用。如果有疑问,可以联系我们。
MYSQL实例前言
MYSQL实例最近在工作中遇到一个需求是这样的:需要在使用AES_ENCRYPT()
函数将明文加密,存储在MySQL中,但是遇到了一些问题……下面就来详细介绍下.
MYSQL实例说将加密后的密文,解密取出来是NULL.
MYSQL实例看了一下,她发过来的表结构:
MYSQL实例
MYSQL实例再看了她通过AES_DECRYPT()函数加密了一个字符串,然后insert进去了,执行成功后,显示了一个warning:
Query OK, 1 row affected, 1 warning (0.00 sec)
MYSQL实例(没有报错而是warning,大概是sql_mode的缘故)
MYSQL实例此时她忽略了这个warning,再通过AES_DECRYPT()
解密后,发现取出来的明文为NULL.
MYSQL实例再回看表结构,发现其字段属性为“varchar” && 字符集是ut8,检查warning为下:
MYSQL实例
mysql> show warnings;
+---------+------+------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------------------------------+
| Warning | 1366 | Incorrect string value: '\xE3f767\x12...' for column 'passwd' at row 1 |
+---------+------+------------------------------------------------------------------------+
1 row in set (0.00 sec)
MYSQL实例查了一下文档,看一下这两个函数的使用:
MYSQL实例
-- 将'hello world'加密,密钥为'key',加密后的串存在@pass中
mysql> SET @pass=AES_ENCRYPT('hello world', 'key');
Query OK, 0 rows affected (0.00 sec)
-- 看一下加密后串的长度(都为2的整数次方)
mysql> SELECT CHAR_LENGTH(@pass);
+--------------------+
| CHAR_LENGTH(@pass) |
+--------------------+
| 16 |
+--------------------+
1 row in set (0.00 sec)
-- 使用AES_DECRYPT()解密
mysql> SELECT AES_DECRYPT(@pass, 'key');
+---------------------------+
| AES_DECRYPT(@pass, 'key') |
+---------------------------+
| hello world |
+---------------------------+
1 row in set (0.00 sec)
MYSQL实例那么到底该如何存呢?
MYSQL实例方法①:
MYSQL实例将字段属性设置为varbinary/binary/四个blob类型,等二进制字段属性.
MYSQL实例创建三个字段,属性分别为varbinary、binary、blob.
MYSQL实例并将'明文1','text2','明文_text3'加密,密钥为key,存入表中.
MYSQL实例最后取出.
MYSQL实例
mysql> CREATE TABLE t_passwd (pass1 varbinary(16), pass2 binary(16), pass3 blob);
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO t_passwd VALUES (AES_ENCRYPT('明文1', 'key'), AES_ENCRYPT('text2', 'key'), AES_ENCRYPT('明文_text3', 'key'));
Query OK, 1 row affected (0.01 sec)
mysql> SELECT AES_DECRYPT(pass1, 'key'), AES_DECRYPT(pass2, 'key'), AES_DECRYPT(pass3, 'key') FROM t_passwd;
+---------------------------+---------------------------+---------------------------+
| AES_DECRYPT(pass1, 'key') | AES_DECRYPT(pass2, 'key') | AES_DECRYPT(pass3, 'key') |
+---------------------------+---------------------------+---------------------------+
| 明文1 | text2 | 明文_text3 |
+---------------------------+---------------------------+---------------------------+
1 row in set (0.00 sec)
MYSQL实例当然,属性括号内的长度要取决于明文的长度,此处明文较短,故只给了16.
MYSQL实例方法②:
MYSQL实例将密文十六进制化,再存入varchar/char列.
MYSQL实例此处需要用到HEX()来存入,用UNHEX()
取出.
MYSQL实例创建一个字符串属性的字段.
MYSQL实例将'hello world'先用密钥'key2'进行AES加密,再将加密后的串通过HEX函数十六进制化.
MYSQL实例最后先将加密后的串通过UNHEX取出,再通过AES据密钥'key2'解密:
MYSQL实例
mysql> CREATE TABLE t_passwd_2(pass1 char(32));
Query OK, 0 rows affected (0.01 sec)
mysql> INSERT INTO t_passwd_2 VALUES (HEX(AES_ENCRYPT('hello world', 'key2')));
Query OK, 1 row affected (0.00 sec)
mysql> SELECT AES_DECRYPT(UNHEX(pass1), 'key2') FROM t_passwd_2;
+-----------------------------------+
| AES_DECRYPT(UNHEX(pass1), 'key2') |
+-----------------------------------+
| hello world |
+-----------------------------------+
1 row in set (0.00 sec)
MYSQL实例同样,根据明文的长度不同,AES_ENCRYPT加密后的串长度也会有所变化,所以HEX后的字符串长度也会有所变化.
实际使用时,需要据业务评估出一个合理值即可.
MYSQL实例方法③:
MYSQL实例直接存入varchar中,不做十六进制化.
MYSQL实例回溯到问题的一开始,将加密后的串,存到utf8字符集并且属性为varchar中,是不行的.
MYSQL实例实际上,将字符集改成latin1就可以了:
MYSQL实例在insert的时候也不会报warning了.
MYSQL实例
mysql> CREATE TABLE t_passwd_3(pass varchar(32)) CHARSET latin1;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO t_passwd_3 SELECT AES_ENCRYPT('text', 'key3');
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT AES_DECRYPT(pass, 'key3') FROM t_passwd_3;
+---------------------------+
| AES_DECRYPT(pass, 'key3') |
+---------------------------+
| text |
+---------------------------+
1 row in set (0.00 sec)
MYSQL实例这样的方法虽然美,只需将字段字符集设置为latin1就可以了,但可能会带来隐患:
MYSQL实例文档上写了这样的一句:
MYSQL实例Many encryption and compression functions return strings for which the result might contain arbitrary byte values. If you want to store these results, use a column with a VARBINARY or BLOB binary string data type. This will avoid potential problems with trailing space removal or character set conversion that would change data values, such as may occur if you use a nonbinary string data type (CHAR, VARCHAR, TEXT).
MYSQL实例大意是,如果用方法③那样,直接将加密后的串存入char/varchar/text类型中,在做字符转换的时或空格被删除时,可能会带来潜在的影响.
MYSQL实例所以如果一定要存在char/varchar/text中,那么还是参考方法②,十六进制化一下吧.
MYSQL实例或者如同方法①,直接存在二进制字段中.
MYSQL实例总结
MYSQL实例以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对维易PHP的支持.
MYSQL实例参考文档:
MYSQL实例Chapter 12 Functions and Operators - 12.13 Encryption and Compression Functions
转载请注明本页网址:
http://www.vephp.com/jiaocheng/4847.html