Why is AES_DECRYPT returning null?

流过昼夜 提交于 2019-11-29 07:30:00

The answer is that the columns are binary when they should be varbinary. This article explains it:

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

With binary column types being fixed length, the length of the input value must be known to ensure correct padding. For unknown length values, use varbinary to avoid issues with incorrect padding resulting from differing value lengths.

When you insert binary data into a VARCHAR field there are some binary characters that a VARCHAR can't handle and they will mess up in the inserted value. And then the inserted value will not be the same when you retrieve it. 1.select hex(aes_encrypt(file,'key')); 2.select aes_decrypt(unhex(file),'key');

Check if type of your field is blob instead binary(32)

Did you try different values other than 'Testname'? Do other values work?

I ask because I had a situation while testing 2 test credit card numbers where one decrypted fine and the other returned null.

The answer was to hex and unhex as suggested by "abhinai raj"

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!