本文介绍了如何在mysql中使用AES_ENCRYPT和AES_DECRYPT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了用户表

  CREATE TABLE`user`(
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` VARBINARY(100)NULL,
`address` VARBINARY(200)NOT NULL,
PRIMARY KEY(`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

我插入一行:



<$ p $ INSERT into user(first_name,address)VALUES(AES_ENCRYPT('Obama','usa2010'),AES_ENCRYPT('Obama','usa2010'));

要选择我使用的这一行:

  SELECT AES_DECRYPT(first_name,'usa2010'),AES_DECRYPT(address,'usa2010')from user; 

我得到以下结果。我需要看到我的数据。没有数据可见



解决方案

根据手册:







我不知道为什么它仍然返回一个二进制字符串在你的情况。无论如何,尝试这样:

  SELECT *,
CAST(AES_DECRYPT(first_name,'usa2010')AS CHAR ))first_name_decrypt
FROM user

并使用 first_name_decrypt 而不是 first_name


I created user table

CREATE  TABLE `user` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`first_name` VARBINARY(100) NULL ,
`address` VARBINARY(200) NOT NULL ,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

I inserted one row:

INSERT into user (first_name, address) VALUES (AES_ENCRYPT('Obama', 'usa2010'),AES_ENCRYPT('Obama', 'usa2010'));

To select this row i used:

SELECT AES_DECRYPT(first_name, 'usa2010'), AES_DECRYPT(address, 'usa2010') from user;

I am getting the following result.What i need to do see my data.No data is visible for me.

解决方案

According to the Manual:

I don't know why it is still returning a binary string in your case. Anyway, try this:

SELECT *,
       CAST(AES_DECRYPT(first_name, 'usa2010') AS CHAR(50)) first_name_decrypt
FROM   user

And use first_name_decrypt instead of first_name.

这篇关于如何在mysql中使用AES_ENCRYPT和AES_DECRYPT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 20:19