0

I need to store data in mysql database using aes_encrypt() function.

The key in the case of above mentioned function is padded with zero when its length is less than 16 bytes.

My question is: How can I perform a key stretching with something like PBKDF2 in mysql to enhance the strength of weak keys.

Also I would like to know that if I use a hex value directly from a function like SHA2('Key', 512) as my key for the aes_encrypt() function, will it reduce the strength of the key because there will only be 16 characters in the character set every time a key is generated.

1

The key in the case of above mentioned function is padded with zero when its length is less than 16 bytes.

That's indeed something you need to address. AES keys should always be 128, 192 or 256 bits in size, and indistinguishable from random to an attacker.

My question is: How can I perform a key stretching with something like PBKDF2 in mysql to enhance the strength of weak keys.

I'd strongly suggest not to perform such a function within the database queries itself. PBKDF2 (or any other function) needs to be as fast as possible to make the possible advantage of attackers as small as possible. Otherwise the work factor (the iteration count for PBKDF2) cannot be large enough.

Try and find an optimized version of PBKDF2 and integrate it with your software.

Also I would like to know that if I use a hex value directly from a function like SHA2('Key', 512) as my key for the aes_encrypt() function, will it reduce the strength of the key because there will only be 16 characters in the character set every time a key is generated.

The output of SHA-2 functions consist of bytes, not hexadecimals. It might be that your implementation automatically translates this to hexadecimals. In that case you need to specify raw output explicitly or you need to decode the hexadecimals to use it as AES key.

The problem with just SHA-2 is that it doesn't expect a salt nor a work factor. This means that dictionary attacks and rainbow tables are valid attack vectors. This is why you need a Password Based Key Derivation Function such as PBKDF2.

PBKDF2 is only not needed if the input "password" has sufficient strength. But for AES encryption, you'd need something close to 128 bits of randomness, and that's unlikely to be provided by any password in itsef.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.