mysql 中的 AES_ENCRYPT 后无法使用 AES_DECRYPT

2021-11-20 00:00:00 encryption mysql

我创建了用户表

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;

我插入了一行:

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.

推荐答案

根据手册:

AES_ENCRYPT() 加密一个字符串并返回一个二进制字符串.AES_DECRYPT() 解密加密后的字符串并返回原始字符串.

AES_ENCRYPT() encrypts a string and returns a binary string. AES_DECRYPT() decrypts the encrypted string and returns the original string.

  • MySQL 5.1 文档:AES_ENCRYPT()/AES_DECRYPT()
  • 我不知道为什么在你的情况下它仍然返回一个二进制字符串.不管怎样,试试这个:

    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
    

    并使用first_name_decrypt 而不是first_name.

相关文章