要在MySQL中加密和解密,请在MySQL中使用AES_ENCRYPT()和AES_DECRYPT()-
insert into yourTableName values(AES_ENCRYPT(yourValue,yourSecretKey)); select cast(AES_DECRYPT(yourColumnName, yourSecretKey) as char) from yourTableName;
要了解上述语法,让我们首先创建一个表-
mysql> create table demo63 −> ( −> value blob −> );
借助insert命令将一些记录插入表中。我们在插入时正在加密-
mysql> insert into demo63 values(AES_ENCRYPT('John','PASS')); mysql> insert into demo63 values(AES_ENCRYPT('David','PASS')); mysql> insert into demo63 values(AES_ENCRYPT('123456','PASS'));
使用select语句显示表中的记录-
mysql> select *from demo63;
这将产生以下输出-
+------------------------------------+ | value | +------------------------------------+ | 0x16D8A4F11321D761920783BF96BB8314 | | 0x0A85099F705F21D27B0129C54C4473AA | | 0xAB7C6D6068BC100B0F04D1C4EA068AC9 | +------------------------------------+ 3 rows in set (0.00 sec)
以下是要解密的查询-
mysql> select cast(aes_decrypt(value, 'PASS') AS char) from demo63;
这将产生以下输出-
+------------------------------------------+ | cast(aes_decrypt(value, 'PASS') AS char) | +------------------------------------------+ | John | | David | | 123456 | +------------------------------------------+ 3 rows in set (0.00 sec)