How to pad short strings for use as an AES key?

327 Views Asked by At

I have 5 different strings that I want to use as a key. They are:

asuspcgame, dragonZ, whiterice, ball, document

I want to perform an encryption using EVP OpenSSL like this: https://www.openssl.org/docs/man1.0.2/crypto/EVP_EncryptInit.html

However, I realized that the example is using a fixed size key (16 characters for AES_128).

unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
EVP_EncryptInit_ex(&ctx, EVP_idea_cbc(), NULL, key, iv);

My question is: How do I make my strings to always have a fixed size 16 characters?

I didn't see any function from EVP that could make my strings become a fixed size key.

1

There are 1 best solutions below

0
On

Do not use strings as encryption keys, use a password derivation function such as PBKDF2 with a rounds count such that the function takes ~100ms. That will produce am output of bytes to use as a key.

For the IV use a cryptographic pseudo-random number generator and prefix the encrypted data with the IV, there is no need to keep the IV secret.

Or better yet, use RNCryptor; it does all of this for you and adds encryption authentication, versioning and is cross platform.