Why does AES_DECRYPT return null?

I found similar questions, but I did not find a clear answer to this question. I have this table:

CREATE DATABASE testDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 

CREATE TABLE testTable
(
firstName binary(32) not null,
lastName binary(32) not null
/* Other non-binary fields omitted */
)
engine=INNODB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

This statement runs just fine:

INSERT INTO testTable (firstName) VALUES (AES_ENCRYPT('Testname', 'test'));

But this returns NULL:

SELECT AES_DECRYPT(firstName, 'test') FROM testTable;

Why does this return NULL?

Fwiw, this returns "testValue" as expected:

SELECT AES_DECRYPT(AES_ENCRYPT('testValue','thekey'), 'thekey');
+5
source share
3 answers

The answer is what columns binaryare when they should be varbinary. This article explains this:

Because if AES_DECRYPT () detects invalid data or incorrect padding , it will return NULL.

binary , , . varbinary, .

+9

, (32)

0

VARCHAR, , VARCHAR , . , . 1.select hex (aes_encrypt (, "" )); 2.select aes_decrypt (unhex (), 'key');

-1
source

All Articles