Bug #14055 AES_DECRYPT returns NULL on valid input
Submitted: 16 Oct 2005 4:18 Modified: 18 Oct 2005 22:02
Reporter: Timothy Smith Email Updates:
Status: Won't fix Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.1.16 OS:Any (any)
Assigned to: Jim Winstead CPU Architecture:Any

[16 Oct 2005 4:18] Timothy Smith
Description:
Hi.  I tested this on FreeBSD with a current pull of BK 4.1.16.  The customer reported it on Linux.

I change a single bit in the key I'm using in the AES_ENCRYPT and AES_DECRYPT function pair, and I get NULL back from AES_DECRYPT instead of the correct results:

mysql> set @key = 'corydog2004vcruler';
Query OK, 0 rows affected (0.00 sec)

mysql> UPDATE tbd_test SET user_password=AES_ENCRYPT('movitz75', @key);
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> SELECT AES_DECRYPT(user_password, @key) from tbd_test;
+----------------------------------+
| AES_DECRYPT(user_password, @key) |
+----------------------------------+
| movitz75                         |
+----------------------------------+
1 row in set (0.00 sec)

mysql> set @key = 'corydog2004vcrules';
Query OK, 0 rows affected (0.00 sec)

mysql> UPDATE tbd_test SET user_password=AES_ENCRYPT('movitz75', @key);
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT AES_DECRYPT(user_password, @key) from tbd_test;
+----------------------------------+
| AES_DECRYPT(user_password, @key) |
+----------------------------------+
| NULL                             |
+----------------------------------+
1 row in set (0.00 sec)

Aside from 'corydog2004vcrules', I have not found another key which causes the problem.  :)  But I'm sure there must be some others as well.

How to repeat:

CREATE TABLE tbd_test (user_password varchar(255));
INSERT INTO tbd_test VALUES ('one');

set @key = 'corydog2004vcruler';

UPDATE tbd_test SET user_password=AES_ENCRYPT('movitz75', @key);
SELECT AES_DECRYPT(user_password, @key) from tbd_test;

set @key = 'corydog2004vcrules';

UPDATE tbd_test SET user_password=AES_ENCRYPT('movitz75', @key);
SELECT AES_DECRYPT(user_password, @key) from tbd_test;

Suggested fix:
Unknown.
[18 Oct 2005 22:02] Jim Winstead
This is happening because of space-stripping in the VARCHAR field. This can be worked around by using a space-preserving field type, such as TEXT or BLOB, or upgrading to 5.0.