Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to AES_DECRYPT after AES_ENCRYPT in mysql

Tags:

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.

I am unable to see my data

like image 348
PSR Avatar asked May 15 '13 03:05

PSR


People also ask

How to DECRYPT AES encryption in MySQL?

The MySQL AES_DECRYPT function returns the original string after decrypting an encrypted string. It uses AES(Advanced Encryption Standard) algorithm to perform the decryption. The AES_DECRYPT function returns the decrypted string or NULL if it detects invalid data.

How to ENCRYPT field in MySQL?

When encrypting a column you can use the ENCRYPT function, AES_ ENCRYPT function, the older DES_ENCRYPT function, or the encoding or compression algorithms. If you want to use this approach to encryption and decryption, I would recommend that you use AES_ENCRYPT and AES_DECRYPT.

Does mysql support encryption?

MySQL Enterprise Encryption provides industry standard functionality for asymmetric encryption. MySQL Enterprise Encryption allows your enterprise to: Secure data using combination of public, private, and symmetric keys to encrypt and decrypt data.


2 Answers

According to the Manual:

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

  • MySQL 5.1 Doc: 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 

And use first_name_decrypt instead of first_name.

like image 145
John Woo Avatar answered Dec 09 '22 18:12

John Woo


From mysql command line client there is no need to use CAST:

mysql> SELECT AES_DECRYPT(AES_ENCRYPT('admin','abc'),'abc');  +-----------------------------------------------+ | AES_DECRYPT(AES_ENCRYPT('admin','abc'),'abc') | +-----------------------------------------------+ | admin                                         | +-----------------------------------------------+ 1 row in set (0.00 sec)  mysql> SELECT CAST(AES_DECRYPT(AES_ENCRYPT('admin','abc'),'abc') AS CHAR (50));  +------------------------------------------------------------------+ | CAST(AES_DECRYPT(AES_ENCRYPT('admin','abc'),'abc') AS CHAR (50)) | +------------------------------------------------------------------+ | admin                                                            | +------------------------------------------------------------------+ 1 row in set (0.02 sec) 

As you can see using cast in command line is little bit slower. But I have noticed that if you use some tools like phpmyadmin, then you need to use CAST, otherwise result will be wrong.

like image 41
Viktor Avatar answered Dec 09 '22 18:12

Viktor